Skip to content

Commit

Permalink
Merge branch 'main' into feature/prometheus-scope-info
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart authored Nov 29, 2023
2 parents f8ee51b + 4e390e3 commit 395a7fc
Show file tree
Hide file tree
Showing 34 changed files with 401 additions and 261 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
<PackageVersion Include="Swashbuckle.AspNetCore" Version="[6.5.0,)" />
<PackageVersion Include="Testcontainers.MsSql" Version="3.6.0" />
<PackageVersion Include="Testcontainers.SqlEdge" Version="3.6.0" />
<PackageVersion Include="xunit" Version="[2.6.1,3.0)" />
<PackageVersion Include="xunit.runner.visualstudio" Version="[2.5.3,3.0)" />
<PackageVersion Include="xunit" Version="[2.6.2,3.0)" />
<PackageVersion Include="xunit.runner.visualstudio" Version="[2.5.4,3.0)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ If you have trouble accessing the doc, please get in touch on
* [Reiley Yang](https://github.com/reyang), Microsoft
* [Vishwesh Bankwar](https://github.com/vishweshbankwar), Microsoft

[Triagers](https://github.com/open-telemetry/community/blob/main/community-membership.md#triager)
([@open-telemetry/dotnet-triagers](https://github.com/orgs/open-telemetry/teams/dotnet-triagers)):

* [Martin Thwaites](https://github.com/martinjt), Honeycomb

[Emeritus
Maintainer/Approver/Triager](https://github.com/open-telemetry/community/blob/main/community-membership.md#emeritus-maintainerapprovertriager):

Expand Down
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
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Add support for Instrumentation Scope Attributes (i.e [Meter
Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)),
fixing issue
[#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).
([#5089](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5089))

## 1.7.0-alpha.1

Released 2023-Oct-16
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)
{
this.WriteLine("\tMeter Tags:");
if (ConsoleTagTransformer.Instance.TryTransformTag(meterTag, out var result))
{
this.WriteLine($"\t\t{result}");
}
}

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
string valueDisplay = string.Empty;
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
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](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)),
fixing issue
[#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).
([#5089](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5089))

## 1.7.0-alpha.1

Released 2023-Oct-16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
using Google.Protobuf;
using Google.Protobuf.Collections;
using OpenTelemetry.Metrics;
using OpenTelemetry.Proto.Metrics.V1;
using AggregationTemporality = OpenTelemetry.Metrics.AggregationTemporality;
using Metric = OpenTelemetry.Metrics.Metric;
using OtlpCollector = OpenTelemetry.Proto.Collector.Metrics.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpMetrics = OpenTelemetry.Proto.Metrics.V1;
Expand All @@ -28,15 +31,15 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;

internal static class MetricItemExtensions
{
private static readonly ConcurrentBag<OtlpMetrics.ScopeMetrics> MetricListPool = new();
private static readonly ConcurrentBag<ScopeMetrics> MetricListPool = new();

internal static void AddMetrics(
this OtlpCollector.ExportMetricsServiceRequest request,
OtlpResource.Resource processResource,
in Batch<Metric> metrics)
{
var metricsByLibrary = new Dictionary<string, OtlpMetrics.ScopeMetrics>();
var resourceMetrics = new OtlpMetrics.ResourceMetrics
var metricsByLibrary = new Dictionary<string, ScopeMetrics>();
var resourceMetrics = new ResourceMetrics
{
Resource = processResource,
};
Expand All @@ -58,7 +61,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 @@ -77,34 +80,44 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque
return;
}

foreach (var scope in resourceMetrics.ScopeMetrics)
foreach (var scopeMetrics in resourceMetrics.ScopeMetrics)
{
scope.Metrics.Clear();
MetricListPool.Add(scope);
scopeMetrics.Metrics.Clear();
scopeMetrics.Scope.Attributes.Clear();
MetricListPool.Add(scopeMetrics);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version)
internal static ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable<KeyValuePair<string, object>> meterTags)
{
if (!MetricListPool.TryTake(out var metrics))
if (!MetricListPool.TryTake(out var scopeMetrics))
{
metrics = new OtlpMetrics.ScopeMetrics
scopeMetrics = new ScopeMetrics
{
Scope = new OtlpCommon.InstrumentationScope
{
Name = name, // Name is enforced to not be null, but it can be empty.
Version = version ?? string.Empty, // NRE throw by proto
},
};

if (meterTags != null)
{
AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes);
}
}
else
{
metrics.Scope.Name = name;
metrics.Scope.Version = version ?? string.Empty;
scopeMetrics.Scope.Name = name;
scopeMetrics.Scope.Version = version ?? string.Empty;
if (meterTags != null)
{
AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes);
}
}

return metrics;
return scopeMetrics;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -140,15 +153,15 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
case MetricType.LongSum:
case MetricType.LongSumNonMonotonic:
{
var sum = new OtlpMetrics.Sum
var sum = new Sum
{
IsMonotonic = metric.MetricType == MetricType.LongSum,
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -167,15 +180,15 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
case MetricType.DoubleSum:
case MetricType.DoubleSumNonMonotonic:
{
var sum = new OtlpMetrics.Sum
var sum = new Sum
{
IsMonotonic = metric.MetricType == MetricType.DoubleSum,
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -193,10 +206,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.LongGauge:
{
var gauge = new OtlpMetrics.Gauge();
var gauge = new Gauge();
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -214,10 +227,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.DoubleGauge:
{
var gauge = new OtlpMetrics.Gauge();
var gauge = new Gauge();
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -235,14 +248,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.Histogram:
{
var histogram = new OtlpMetrics.Histogram
var histogram = new Histogram
{
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.HistogramDataPoint
var dataPoint = new HistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand Down Expand Up @@ -310,14 +323,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.ExponentialHistogram:
{
var histogram = new OtlpMetrics.ExponentialHistogram
var histogram = new ExponentialHistogram
{
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.ExponentialHistogramDataPoint
var dataPoint = new ExponentialHistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -337,7 +350,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
dataPoint.Scale = exponentialHistogramData.Scale;
dataPoint.ZeroCount = (ulong)exponentialHistogramData.ZeroCount;

dataPoint.Positive = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive = new ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive.Offset = exponentialHistogramData.PositiveBuckets.Offset;
foreach (var bucketCount in exponentialHistogramData.PositiveBuckets)
{
Expand Down Expand Up @@ -368,6 +381,17 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
}
}

private static void AddScopeAttributes(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
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
semantic conventions.
([#5068](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5068))

* Update activity DisplayName as per the specification.
([#5078](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5078))

## 1.6.0-beta.3

Released 2023-Nov-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void OnStartActivity(Activity activity, object payload)
return;
}

activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);
RequestMethodHelper.SetHttpClientActivityDisplayName(activity, request.Method.Method);

if (!IsNet7OrGreater)
{
Expand All @@ -162,17 +162,7 @@ public void OnStartActivity(Activity activity, object payload)
}

// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md
if (RequestMethodHelper.KnownMethods.TryGetValue(request.Method.Method, out var httpMethod))
{
activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, httpMethod);
}
else
{
// Set to default "_OTHER" as per spec.
// https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-spans.md#common-attributes
activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, "_OTHER");
activity.SetTag(SemanticConventions.AttributeHttpRequestMethodOriginal, request.Method.Method);
}
RequestMethodHelper.SetHttpMethodTag(activity, request.Method.Method);

activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
if (!request.RequestUri.IsDefaultPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,12 @@ internal static HttpClientInstrumentationOptions TracingOptions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, Activity activity)
{
activity.DisplayName = HttpTagHelper.GetOperationNameForHttpMethod(request.Method);
RequestMethodHelper.SetHttpClientActivityDisplayName(activity, request.Method);

if (activity.IsAllDataRequested)
{
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md
if (RequestMethodHelper.KnownMethods.TryGetValue(request.Method, out var httpMethod))
{
activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, httpMethod);
}
else
{
// Set to default "_OTHER" as per spec.
// https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-spans.md#common-attributes
activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, "_OTHER");
activity.SetTag(SemanticConventions.AttributeHttpRequestMethodOriginal, request.Method);
}
RequestMethodHelper.SetHttpMethodTag(activity, request.Method);

activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
if (!request.RequestUri.IsDefaultPort)
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?>>?
6 changes: 6 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
implementationFactory)`.
([#4916](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4916))

* Add support for Instrumentation Scope Attributes (i.e [Meter
Tags](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter.tags)),
fixing issue
[#4563](https://github.com/open-telemetry/opentelemetry-dotnet/issues/4563).
([#5089](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5089))

## 1.7.0-alpha.1

Released 2023-Oct-16
Expand Down
Loading

0 comments on commit 395a7fc

Please sign in to comment.