Skip to content

Commit fa9a6dd

Browse files
[AWS] Use factories
- Use `ActivitySourceFactory` and `MeterFactory`. - Add support for tags to `MeterFactory`. - Fix `MeterFactory` namespace.
1 parent b84422b commit fa9a6dd

10 files changed

Lines changed: 38 additions & 79 deletions

File tree

src/OpenTelemetry.Instrumentation.AWS/Implementation/Metrics/AWSMeterProvider.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,30 @@
55
using Amazon.Runtime.Telemetry;
66
using Amazon.Runtime.Telemetry.Metrics;
77
using OpenTelemetry.AWS;
8-
using OpenTelemetry.Internal;
98

109
namespace OpenTelemetry.Instrumentation.AWS.Implementation.Metrics;
1110

1211
internal sealed class AWSMeterProvider(SemanticConventionVersion version) : MeterProvider
1312
{
14-
private static readonly string MeterVersion = GetMeterVersion();
15-
1613
private readonly ConcurrentDictionary<string, AWSMeter> meters = new();
17-
private readonly string telemetrySchemaUrl = AWSSemanticConventions.GetTelemetrySchemaUrl(version);
14+
private readonly Version semanticConventionVersion = AWSSemanticConventions.GetVersion(version);
1815

1916
public override Meter GetMeter(string scope, Attributes? attributes = null)
2017
{
2118
if (!this.meters.TryGetValue(scope, out var meter))
2219
{
2320
#if NET
24-
meter = this.meters.GetOrAdd(scope, static (name, state) => CreateMeter(name, state), (this.telemetrySchemaUrl, attributes?.AllAttributes));
21+
meter = this.meters.GetOrAdd(scope, static (name, state) => CreateMeter(name, state), (this.semanticConventionVersion, attributes?.AllAttributes));
2522
#else
26-
meter = this.meters.GetOrAdd(scope, (name) => CreateMeter(name, (this.telemetrySchemaUrl, attributes?.AllAttributes)));
23+
meter = this.meters.GetOrAdd(scope, (name) => CreateMeter(name, (this.semanticConventionVersion, attributes?.AllAttributes)));
2724
#endif
2825
}
2926

3027
return meter;
3128

32-
static AWSMeter CreateMeter(string name, (string SchemaUrl, IEnumerable<KeyValuePair<string, object?>>? Attributes) state)
29+
static AWSMeter CreateMeter(string name, (Version Version, IEnumerable<KeyValuePair<string, object?>>? Attributes) state)
3330
{
34-
var options = new System.Diagnostics.Metrics.MeterOptions(name)
35-
{
36-
TelemetrySchemaUrl = state.SchemaUrl,
37-
Version = MeterVersion,
38-
};
39-
40-
if (state.Attributes is { } attributes)
41-
{
42-
options.Tags = attributes;
43-
}
44-
45-
return new AWSMeter(new System.Diagnostics.Metrics.Meter(options));
31+
return new AWSMeter(OpenTelemetry.Metrics.MeterFactory.Create<AWSMeterProvider>(state.Version, state.Attributes));
4632
}
4733
}
48-
49-
private static string GetMeterVersion()
50-
{
51-
var assembly = typeof(AWSMeterProvider).Assembly;
52-
return assembly.GetPackageVersion();
53-
}
5434
}

src/OpenTelemetry.Instrumentation.AWS/Implementation/Tracing/AWSTracerProvider.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,32 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Collections.Concurrent;
5-
using System.Diagnostics;
65
using Amazon.Runtime.Telemetry.Tracing;
76
using OpenTelemetry.AWS;
8-
using OpenTelemetry.Internal;
97

108
namespace OpenTelemetry.Instrumentation.AWS.Implementation.Tracing;
119

1210
internal sealed class AWSTracerProvider(SemanticConventionVersion version) : TracerProvider
1311
{
14-
private static readonly string ActivitySourceVersion = GetActivitySourceVersion();
15-
1612
private readonly ConcurrentDictionary<string, AWSTracer> tracers = new();
17-
private readonly string telemetrySchemaUrl = AWSSemanticConventions.GetTelemetrySchemaUrl(version);
13+
private readonly Version semanticConventionVersion = AWSSemanticConventions.GetVersion(version);
1814

1915
public override Tracer GetTracer(string scope)
2016
{
2117
if (!this.tracers.TryGetValue(scope, out var awsTracer))
2218
{
2319
#if NET
24-
awsTracer = this.tracers.GetOrAdd(scope, static (name, schemaUrl) => CreateTracer(name, schemaUrl), this.telemetrySchemaUrl);
20+
awsTracer = this.tracers.GetOrAdd(scope, static (name, version) => CreateTracer(name, version), this.semanticConventionVersion);
2521
#else
26-
awsTracer = this.tracers.GetOrAdd(scope, (name) => CreateTracer(name, this.telemetrySchemaUrl));
22+
awsTracer = this.tracers.GetOrAdd(scope, (name) => CreateTracer(name, this.semanticConventionVersion));
2723
#endif
2824
}
2925

3026
return awsTracer;
3127

32-
static AWSTracer CreateTracer(string name, string telemetrySchemaUrl)
28+
static AWSTracer CreateTracer(string name, Version version)
3329
{
34-
var options = new ActivitySourceOptions(name)
35-
{
36-
TelemetrySchemaUrl = telemetrySchemaUrl,
37-
Version = ActivitySourceVersion,
38-
};
39-
40-
return new AWSTracer(new ActivitySource(options));
30+
return new AWSTracer(Trace.ActivitySourceFactory.Create<AWSTracerProvider>(version));
4131
}
4232
}
43-
44-
private static string GetActivitySourceVersion()
45-
{
46-
var assembly = typeof(AWSTracerProvider).Assembly;
47-
return assembly.GetPackageVersion();
48-
}
4933
}

src/OpenTelemetry.Instrumentation.AWS/OpenTelemetry.Instrumentation.AWS.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<Compile Include="$(RepoRoot)\src\Shared\AWS\*.cs" Link="Includes\AWS\%(Filename).cs" />
35+
<Compile Include="$(RepoRoot)\src\Shared\ActivitySourceFactory.cs" Link="Includes\ActivitySourceFactory.cs" />
3636
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
37+
<Compile Include="$(RepoRoot)\src\Shared\AWS\*.cs" Link="Includes\AWS\%(Filename).cs" />
3738
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
39+
<Compile Include="$(RepoRoot)\src\Shared\MeterFactory.cs" Link="Includes\MeterFactory.cs" />
3840
</ItemGroup>
3941

4042
</Project>

src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,6 @@ private static async Task<TResult> TraceInternalAsync<TInput, TResult>(
256256
}
257257
}
258258

259-
private static ActivitySource CreateActivitySource()
260-
{
261-
var assembly = typeof(AWSLambdaWrapper).Assembly;
262-
var version = assembly.GetPackageVersion();
263-
264-
var activitySourceOptions = new ActivitySourceOptions(ActivitySourceName)
265-
{
266-
TelemetrySchemaUrl = AWSSemanticConventions.SchemaUrl,
267-
Version = version,
268-
};
269-
270-
return new(activitySourceOptions);
271-
}
259+
private static ActivitySource CreateActivitySource() =>
260+
ActivitySourceFactory.Create(typeof(AWSLambdaWrapper), AWSSemanticConventions.Version);
272261
}

src/OpenTelemetry.Instrumentation.AWSLambda/OpenTelemetry.Instrumentation.AWSLambda.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37-
<Compile Include="$(RepoRoot)\src\Shared\AWS\*.cs" Link="Includes\AWS\%(Filename).cs" />
37+
<Compile Include="$(RepoRoot)\src\Shared\ActivitySourceFactory.cs" Link="Includes\ActivitySourceFactory.cs" />
3838
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
39+
<Compile Include="$(RepoRoot)\src\Shared\AWS\*.cs" Link="Includes\AWS\%(Filename).cs" />
3940
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
4041
</ItemGroup>
4142

src/OpenTelemetry.Instrumentation.AspNet/AspNetInstrumentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed class AspNetInstrumentation : IDisposable
1717

1818
public static readonly Version SemanticConventionsVersion = new(1, 36, 0);
1919
public static readonly ActivitySource ActivitySource = ActivitySourceFactory.Create<AspNetInstrumentation>(SemanticConventionsVersion);
20-
public static readonly Meter Meter = MeterFactory.Create<AspNetInstrumentation>(SemanticConventionsVersion);
20+
public static readonly Meter Meter = Metrics.MeterFactory.Create<AspNetInstrumentation>(SemanticConventionsVersion);
2121

2222
public static readonly Histogram<double> HttpServerDuration = Meter.CreateHistogram(
2323
"http.server.request.duration",

src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlTelemetryHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.Metrics;
6+
using OpenTelemetry.Metrics;
67
using OpenTelemetry.Trace;
78

89
namespace OpenTelemetry.Instrumentation.SqlClient.Implementation;

src/Shared/AWS/AWSSemanticConventions.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal partial class AWSSemanticConventions
7979
/// <inheritdoc cref="TagBuilderImpl"/>
8080
public TagBuilderImpl TagBuilder { get; }
8181

82-
public string SchemaUrl { get; }
82+
public Version Version { get; }
8383

8484
/// <summary>
8585
/// Returns attribute names whose values must be reported as a <c>string[]</c>
@@ -100,7 +100,7 @@ public AWSSemanticConventions(SemanticConventionVersion semanticConventionVersio
100100
this.AttributeBuilder = new(this);
101101
this.ParameterMappingBuilder = new(this);
102102
this.TagBuilder = new(this);
103-
this.SchemaUrl = GetTelemetrySchemaUrl(this.semanticConventionVersion);
103+
this.Version = GetVersion(this.semanticConventionVersion);
104104
}
105105

106106
/// <summary>
@@ -454,17 +454,12 @@ public TagBuilderImpl(AWSSemanticConventions semanticConventions)
454454
#endregion
455455
}
456456

457-
internal static string GetTelemetrySchemaUrl(SemanticConventionVersion semanticConventionVersion)
457+
internal static Version GetVersion(SemanticConventionVersion semanticConventionVersion) => semanticConventionVersion switch
458458
{
459-
var versionString = semanticConventionVersion switch
460-
{
461-
SemanticConventionVersion.Latest or SemanticConventionVersion.V1_29_0 => "1.29.0",
462-
SemanticConventionVersion.V1_28_0 => "1.28.0",
463-
_ => throw new InvalidEnumArgumentException(nameof(semanticConventionVersion), (int)semanticConventionVersion, typeof(SemanticConventionVersion)),
464-
};
465-
466-
return $"https://opentelemetry.io/schemas/{versionString}";
467-
}
459+
SemanticConventionVersion.Latest or SemanticConventionVersion.V1_29_0 => new(1, 29, 0),
460+
SemanticConventionVersion.V1_28_0 => new(1, 28, 0),
461+
_ => throw new InvalidEnumArgumentException(nameof(semanticConventionVersion), (int)semanticConventionVersion, typeof(SemanticConventionVersion)),
462+
};
468463

469464
private AttributeBuilderImpl Add(AttributeBuilderImpl attributes, Func<AWSSemanticConventionsBase, string> attributeNameFunc, Func<AWSSemanticConventionsBase, string> valueFunc) =>
470465
this.Add(attributes, attributeNameFunc, valueFunc(this.GetSemanticConventionVersion()));

src/Shared/MeterFactory.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Diagnostics.Metrics;
55
using OpenTelemetry.Internal;
66

7-
namespace OpenTelemetry.Trace;
7+
namespace OpenTelemetry.Metrics;
88

99
internal static class MeterFactory
1010
{
@@ -14,18 +14,20 @@ internal static class MeterFactory
1414
/// </summary>
1515
/// <typeparam name="T">The type for which the <see cref="Meter"/> is created for its assembly.</typeparam>
1616
/// <param name="semanticConventionsVersion">The version of the semantic conventions.</param>
17+
/// <param name="tags">The optional tags to associate with the created <see cref="Meter"/>.</param>
1718
/// <returns>A new <see cref="Meter"/> instance.</returns>
18-
public static Meter Create<T>(Version semanticConventionsVersion)
19-
=> Create(typeof(T), semanticConventionsVersion);
19+
public static Meter Create<T>(Version semanticConventionsVersion, IEnumerable<KeyValuePair<string, object?>>? tags = null)
20+
=> Create(typeof(T), semanticConventionsVersion, tags);
2021

2122
/// <summary>
2223
/// Creates a new <see cref="Meter"/> for the assembly associated
2324
/// with the specified type and semantic conventions version.
2425
/// </summary>
2526
/// <param name="type">The type for which the <see cref="Meter"/> is created for its assembly.</param>
2627
/// <param name="semanticConventionsVersion">The version of the semantic conventions.</param>
28+
/// <param name="tags">The optional tags to associate with the created <see cref="Meter"/>.</param>
2729
/// <returns>A new <see cref="Meter"/> instance.</returns>
28-
public static Meter Create(Type type, Version semanticConventionsVersion)
30+
public static Meter Create(Type type, Version semanticConventionsVersion, IEnumerable<KeyValuePair<string, object?>>? tags = null)
2931
{
3032
Guard.ThrowIfNull(type);
3133
Guard.ThrowIfNull(semanticConventionsVersion);
@@ -45,6 +47,11 @@ public static Meter Create(Type type, Version semanticConventionsVersion)
4547
Version = version,
4648
};
4749

50+
if (tags is not null)
51+
{
52+
options.Tags = tags;
53+
}
54+
4855
return new(options);
4956
}
5057
}

test/OpenTelemetry.Contrib.Shared.Tests/MeterFactoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using OpenTelemetry.Trace;
4+
using OpenTelemetry.Metrics;
55
using Xunit;
66

77
namespace OpenTelemetry.Instrumentation.Tests;

0 commit comments

Comments
 (0)