Skip to content

Commit 26d2131

Browse files
committed
Merge remote-tracking branch 'upstream/main' into di-configuretracing-withservices
2 parents a022ff6 + eb12f9b commit 26d2131

File tree

8 files changed

+160
-5
lines changed

8 files changed

+160
-5
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <copyright file="HttpSemanticConventionHelper.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenTelemetry.Internal;
18+
19+
/// <summary>
20+
/// Helper class for Http Semantic Conventions.
21+
/// </summary>
22+
/// <remarks>
23+
/// Due to a breaking change in the semantic convention, affected instrumentation libraries
24+
/// must inspect an environment variable to determine which attributes to emit.
25+
/// This is expected to be removed when the instrumentation libraries reach Stable.
26+
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md"/>.
27+
/// </remarks>
28+
internal static class HttpSemanticConventionHelper
29+
{
30+
[Flags]
31+
internal enum HttpSemanticConvention
32+
{
33+
/// <summary>
34+
/// Instructs an instrumentation library to emit the old experimental HTTP attributes.
35+
/// </summary>
36+
Old = 0x1,
37+
38+
/// <summary>
39+
/// Instructs an instrumentation library to emit the new, stable Http attributes.
40+
/// </summary>
41+
New = 0x2,
42+
43+
/// <summary>
44+
/// Instructs an instrumentation library to emit both the old and new attributes.
45+
/// </summary>
46+
Dupe = Old | New,
47+
}
48+
49+
public static HttpSemanticConvention GetSemanticConventionOptIn()
50+
{
51+
try
52+
{
53+
var envVarValue = Environment.GetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN");
54+
return envVarValue?.ToLowerInvariant() switch
55+
{
56+
"http" => HttpSemanticConvention.New,
57+
"http/dup" => HttpSemanticConvention.Dupe,
58+
_ => HttpSemanticConvention.Old,
59+
};
60+
}
61+
catch
62+
{
63+
return HttpSemanticConvention.Old;
64+
}
65+
}
66+
}

src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,21 @@ public override ExportResult Export(in Batch<Metric> batch)
146146
}
147147
else
148148
{
149-
// TODO: Consider how/if to display buckets for exponential histograms.
150-
bucketsBuilder.AppendLine("Buckets are not displayed for exponential histograms.");
149+
var exponentialHistogramData = metricPoint.GetExponentialHistogramData();
150+
var scale = exponentialHistogramData.Scale;
151+
152+
if (exponentialHistogramData.ZeroCount != 0)
153+
{
154+
bucketsBuilder.AppendLine($"Zero Bucket:{exponentialHistogramData.ZeroCount}");
155+
}
156+
157+
var offset = exponentialHistogramData.PositiveBuckets.Offset;
158+
foreach (var bucketCount in exponentialHistogramData.PositiveBuckets)
159+
{
160+
var lowerBound = Base2ExponentialBucketHistogram.LowerBoundary(offset, scale).ToString(CultureInfo.InvariantCulture);
161+
var upperBound = Base2ExponentialBucketHistogram.LowerBoundary(++offset, scale).ToString(CultureInfo.InvariantCulture);
162+
bucketsBuilder.AppendLine($"({lowerBound}, {upperBound}]:{bucketCount}");
163+
}
151164
}
152165

153166
valueDisplay = bucketsBuilder.ToString();

src/OpenTelemetry.Exporter.Console/OpenTelemetry.Exporter.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
2929
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeriodicExportingMetricReaderHelper.cs" Link="Includes\PeriodicExportingMetricReaderHelper.cs" />
3030
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\TagTransformer.cs" Link="Includes\TagTransformer.cs" />
31+
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Metrics\Base2ExponentialBucketHistogram.LowerBoundary.cs" Link="Includes\Base2ExponentialBucketHistogram.LowerBoundary.cs" />
3132
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
3233
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
3334
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#endif
3030
using OpenTelemetry.Internal;
3131
using OpenTelemetry.Trace;
32+
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
3233

3334
namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
3435
{
@@ -62,13 +63,16 @@ internal class HttpInListener : ListenerHandler
6263
#endif
6364
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
6465
private readonly AspNetCoreInstrumentationOptions options;
66+
private readonly HttpSemanticConvention httpSemanticConvention;
6567

6668
public HttpInListener(AspNetCoreInstrumentationOptions options)
6769
: base(DiagnosticSourceName)
6870
{
6971
Guard.ThrowIfNull(options);
7072

7173
this.options = options;
74+
75+
this.httpSemanticConvention = GetSemanticConventionOptIn();
7276
}
7377

7478
public override void OnEventWritten(string name, object payload)

src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
1515
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
1616
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
17+
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\HttpSemanticConventionHelper.cs" Link="Includes\HttpSemanticConventionHelper.cs" />
1718
</ItemGroup>
1819

1920
<ItemGroup>

src/OpenTelemetry/Internal/ActivityInstrumentationHelper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System.Diagnostics;
1818
using System.Linq.Expressions;
19+
using System.Reflection;
1920
#pragma warning restore IDE0005
2021

2122
namespace OpenTelemetry.Instrumentation
@@ -29,15 +30,17 @@ private static Action<Activity, ActivitySource> CreateActivitySourceSetter()
2930
{
3031
ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance");
3132
ParameterExpression propertyValue = Expression.Parameter(typeof(ActivitySource), "propertyValue");
32-
var body = Expression.Assign(Expression.Property(instance, "Source"), propertyValue);
33+
PropertyInfo sourcePropertyInfo = typeof(Activity).GetProperty("Source");
34+
var body = Expression.Assign(Expression.Property(instance, sourcePropertyInfo), propertyValue);
3335
return Expression.Lambda<Action<Activity, ActivitySource>>(body, instance, propertyValue).Compile();
3436
}
3537

3638
private static Action<Activity, ActivityKind> CreateActivityKindSetter()
3739
{
3840
ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance");
3941
ParameterExpression propertyValue = Expression.Parameter(typeof(ActivityKind), "propertyValue");
40-
var body = Expression.Assign(Expression.Property(instance, "Kind"), propertyValue);
42+
PropertyInfo kindPropertyInfo = typeof(Activity).GetProperty("Kind");
43+
var body = Expression.Assign(Expression.Property(instance, kindPropertyInfo), propertyValue);
4144
return Expression.Lambda<Action<Activity, ActivityKind>>(body, instance, propertyValue).Compile();
4245
}
4346
}

test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void EnsureAotCompatibility()
8585
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");
8686

8787
var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
88-
Assert.Equal(58, warnings.Count());
88+
Assert.Equal(48, warnings.Count());
8989
}
9090
}
9191
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// <copyright file="HttpSemanticConventionHelperTest.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Xunit;
18+
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
19+
20+
namespace OpenTelemetry.Api.Tests.Internal;
21+
22+
public class HttpSemanticConventionHelperTest
23+
{
24+
[Fact]
25+
public void VerifyFlags()
26+
{
27+
var testValue = HttpSemanticConvention.Dupe;
28+
Assert.True(testValue.HasFlag(HttpSemanticConvention.Old));
29+
Assert.True(testValue.HasFlag(HttpSemanticConvention.New));
30+
31+
testValue = HttpSemanticConvention.Old;
32+
Assert.True(testValue.HasFlag(HttpSemanticConvention.Old));
33+
Assert.False(testValue.HasFlag(HttpSemanticConvention.New));
34+
35+
testValue = HttpSemanticConvention.New;
36+
Assert.False(testValue.HasFlag(HttpSemanticConvention.Old));
37+
Assert.True(testValue.HasFlag(HttpSemanticConvention.New));
38+
}
39+
40+
[Fact]
41+
public void VerifyGetSemanticConventionOptIn()
42+
{
43+
this.RunTestWithEnvironmentVariable(null, HttpSemanticConvention.Old);
44+
this.RunTestWithEnvironmentVariable(string.Empty, HttpSemanticConvention.Old);
45+
this.RunTestWithEnvironmentVariable("junk", HttpSemanticConvention.Old);
46+
this.RunTestWithEnvironmentVariable("none", HttpSemanticConvention.Old);
47+
this.RunTestWithEnvironmentVariable("NONE", HttpSemanticConvention.Old);
48+
this.RunTestWithEnvironmentVariable("http", HttpSemanticConvention.New);
49+
this.RunTestWithEnvironmentVariable("HTTP", HttpSemanticConvention.New);
50+
this.RunTestWithEnvironmentVariable("http/dup", HttpSemanticConvention.Dupe);
51+
this.RunTestWithEnvironmentVariable("HTTP/DUP", HttpSemanticConvention.Dupe);
52+
}
53+
54+
private void RunTestWithEnvironmentVariable(string value, HttpSemanticConvention expected)
55+
{
56+
try
57+
{
58+
Environment.SetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN", value);
59+
60+
Assert.Equal(expected, GetSemanticConventionOptIn());
61+
}
62+
finally
63+
{
64+
Environment.SetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN", null);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)