Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ internal class TestMetrics
{
internal static object Run(MetricsOptions options)
{
using var meter = new Meter("TestMeter");
var meterVersion = "1.0";
var meterTags = new List<KeyValuePair<string, object>>
{
new(
"MeterTagKey",
"MeterTagValue"),
};
using var meter = new Meter("TestMeter", meterVersion, meterTags);

var providerBuilder = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(r => r.AddService("myservice"))
Expand Down
4 changes: 2 additions & 2 deletions examples/Console/TestOtlpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ internal static object Run(string endpoint, string protocol)
* launch the OpenTelemetry Collector with an OTLP receiver, by running:
*
* - On Unix based systems use:
* docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:0.48.0 --config=/cfg/otlp-collector-example/config.yaml
* docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otlp-collector-example/config.yaml
*
* - On Windows use:
* docker run --rm -it -p 4317:4317 -p 4318:4318 -v "%cd%":/cfg otel/opentelemetry-collector:0.48.0 --config=/cfg/otlp-collector-example/config.yaml
* docker run --rm -it -p 4317:4317 -p 4318:4318 -v "%cd%":/cfg otel/opentelemetry-collector:latest --config=/cfg/otlp-collector-example/config.yaml
*
* Open another terminal window at the examples/Console/ directory and
* launch the OTLP example by running:
Expand Down
13 changes: 11 additions & 2 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public override ExportResult Export(in Batch<Metric> batch)

foreach (var metric in batch)
{
var msg = new StringBuilder($"\nExport ");
msg.Append(metric.Name);
var msg = new StringBuilder($"\n");
msg.Append($"Metric Name: {metric.Name}");
if (metric.Description != string.Empty)
{
msg.Append(", ");
Expand All @@ -75,6 +75,15 @@ public override ExportResult Export(in Batch<Metric> batch)

this.WriteLine(msg.ToString());

foreach (var meterTag in metric.MeterTags)
Comment thread
cijothomas marked this conversation as resolved.
{
this.WriteLine("\tMeter Tags:");
if (ConsoleTagTransformer.Instance.TryTransformTag(meterTag, out var result))
{
this.WriteLine($"\t {result}");
Comment thread
cijothomas marked this conversation as resolved.
Outdated
}
}

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
string valueDisplay = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
accepts a `name` parameter to support named options.
([#4916](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4916))

* Add support for Instrumentation Scope Attributes (i.e Meter Tags), fixing
Comment thread
cijothomas marked this conversation as resolved.
Outdated
issue [#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).

## 1.7.0-alpha.1

Released 2023-Oct-16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static void AddMetrics(
var meterName = metric.MeterName;
if (!metricsByLibrary.TryGetValue(meterName, out var scopeMetrics))
{
scopeMetrics = GetMetricListFromPool(meterName, metric.MeterVersion);
scopeMetrics = GetMetricListFromPool(meterName, metric.MeterVersion, metric.MeterTags);

metricsByLibrary.Add(meterName, scopeMetrics);
resourceMetrics.ScopeMetrics.Add(scopeMetrics);
Expand All @@ -85,7 +85,7 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version)
internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable<KeyValuePair<string, object>> meterTags)
{
if (!MetricListPool.TryTake(out var metrics))
{
Expand All @@ -97,11 +97,21 @@ internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, stri
Version = version ?? string.Empty, // NRE throw by proto
},
};

if (meterTags != null)
{
AddAttributes(meterTags, metrics.Scope.Attributes);
}
}
else
{
metrics.Scope.Name = name;
metrics.Scope.Version = version ?? string.Empty;
if (meterTags != null)
{
metrics.Scope.Attributes.Clear();
Comment thread
utpilla marked this conversation as resolved.
AddAttributes(meterTags, metrics.Scope.Attributes);
}
}

return metrics;
Expand Down Expand Up @@ -368,6 +378,17 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
}
}

private static void AddAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
{
foreach (var tag in meterTags)
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
{
attributes.Add(result);
}
}
}

/*
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpMetrics.Exemplar ToOtlpExemplar(this IExemplar exemplar)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Metrics.Metric.MeterTags.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>?
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
implementationFactory)`.
([#4916](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4916))

* Add support for Instrumentation Scope Attributes (i.e Meter Tags), fixing
issue [#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).
Comment thread
cijothomas marked this conversation as resolved.
Outdated

## 1.7.0-alpha.1

Released 2023-Oct-16
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry/Metrics/Metric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ internal Metric(
/// </summary>
public string MeterVersion => this.InstrumentIdentity.MeterVersion;

/// <summary>
/// Gets the attributes (tags) for the metric stream.
/// </summary>
public IEnumerable<KeyValuePair<string, object?>>? MeterTags => this.InstrumentIdentity.MeterTags;
Comment thread
utpilla marked this conversation as resolved.

/// <summary>
/// Gets the <see cref="MetricStreamIdentity"/> for the metric stream.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry/Metrics/MetricStreamIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public MetricStreamIdentity(Instrument instrument, MetricStreamConfiguration? me
{
this.MeterName = instrument.Meter.Name;
this.MeterVersion = instrument.Meter.Version ?? string.Empty;
this.MeterTags = instrument.Meter.Tags;
this.InstrumentName = metricStreamConfiguration?.Name ?? instrument.Name;
this.Unit = instrument.Unit ?? string.Empty;
this.Description = metricStreamConfiguration?.Description ?? instrument.Description ?? string.Empty;
Expand Down Expand Up @@ -75,6 +76,8 @@ public MetricStreamIdentity(Instrument instrument, MetricStreamConfiguration? me
hash = (hash * 31) + this.InstrumentType.GetHashCode();
hash = (hash * 31) + this.MeterName.GetHashCode();
hash = (hash * 31) + this.MeterVersion.GetHashCode();

// MeterTags is not part of identity, so not included here.
hash = (hash * 31) + this.InstrumentName.GetHashCode();
hash = (hash * 31) + this.HistogramRecordMinMax.GetHashCode();
hash = (hash * 31) + this.ExponentialHistogramMaxSize.GetHashCode();
Expand All @@ -101,6 +104,8 @@ public MetricStreamIdentity(Instrument instrument, MetricStreamConfiguration? me

public string MeterVersion { get; }

public IEnumerable<KeyValuePair<string, object?>>? MeterTags { get; }

public string InstrumentName { get; }

public string Unit { get; }
Expand Down
30 changes: 30 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricApiTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ public void MetricDescriptionIsExportedCorrectly(string description)
Assert.Equal(description ?? string.Empty, metric.Description);
}

[Fact]
Comment thread
cijothomas marked this conversation as resolved.
public void MetricInstrumentationScopeIsExportedCorrectly()
{
var exportedItems = new List<Metric>();
var meterName = Utils.GetCurrentMethodName();
var meterVersion = "1.0";
var meterTags = new List<KeyValuePair<string, object>>
{
new(
"MeterTagKey",
"MeterTagValue"),
};
using var meter = new Meter($"{meterName}", meterVersion, meterTags);
using var container = this.BuildMeterProvider(out var meterProvider, builder => builder
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedItems));

var counter = meter.CreateCounter<long>("name1");
counter.Add(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Single(exportedItems);
var metric = exportedItems[0];
Assert.Equal(meterName, metric.MeterName);
Assert.Equal(meterVersion, metric.MeterVersion);

bool containsMeterTags = metric.MeterTags.Any(kvp =>
Comment thread
cijothomas marked this conversation as resolved.
Outdated
kvp.Key == meterTags[0].Key && Equals(kvp.Value, meterTags[0].Value));
Assert.True(containsMeterTags);
}

[Fact]
public void DuplicateInstrumentRegistration_NoViews_IdenticalInstruments()
{
Expand Down