Skip to content

Commit b84422b

Browse files
[Instrumentation.AWS] Avoid closures
Use `TryAdd()` overload that avoids allocating closures.
1 parent 276a46e commit b84422b

3 files changed

Lines changed: 25 additions & 18 deletions

File tree

src/OpenTelemetry.Instrumentation.AWS/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Add instrumentation scope version and schema URL to metrics and traces.
1212
([#4063](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4063))
1313

14+
* Pass AWS attribute values to created meters as tags.
15+
([#4063](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4063))
16+
1417
## 1.15.0
1518

1619
Released 2026-Jan-21

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ public override Meter GetMeter(string scope, Attributes? attributes = null)
2121
if (!this.meters.TryGetValue(scope, out var meter))
2222
{
2323
#if NET
24-
meter = this.meters.GetOrAdd(scope, CreateMeter, attributes);
24+
meter = this.meters.GetOrAdd(scope, static (name, state) => CreateMeter(name, state), (this.telemetrySchemaUrl, attributes?.AllAttributes));
2525
#else
26-
meter = this.meters.GetOrAdd(scope, (name) => CreateMeter(name, attributes));
26+
meter = this.meters.GetOrAdd(scope, (name) => CreateMeter(name, (this.telemetrySchemaUrl, attributes?.AllAttributes)));
2727
#endif
2828
}
2929

3030
return meter;
3131

32-
AWSMeter CreateMeter(string name, Attributes? attributes)
32+
static AWSMeter CreateMeter(string name, (string SchemaUrl, IEnumerable<KeyValuePair<string, object?>>? Attributes) state)
3333
{
3434
var options = new System.Diagnostics.Metrics.MeterOptions(name)
3535
{
36-
TelemetrySchemaUrl = this.telemetrySchemaUrl,
36+
TelemetrySchemaUrl = state.SchemaUrl,
3737
Version = MeterVersion,
3838
};
3939

40-
if (attributes?.AllAttributes is { } allAttributes)
40+
if (state.Attributes is { } attributes)
4141
{
42-
options.Tags = allAttributes;
42+
options.Tags = attributes;
4343
}
4444

4545
return new AWSMeter(new System.Diagnostics.Metrics.Meter(options));

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ public override Tracer GetTracer(string scope)
2020
{
2121
if (!this.tracers.TryGetValue(scope, out var awsTracer))
2222
{
23-
awsTracer = this.tracers.GetOrAdd(
24-
scope,
25-
(name) =>
26-
{
27-
var options = new ActivitySourceOptions(name)
28-
{
29-
TelemetrySchemaUrl = this.telemetrySchemaUrl,
30-
Version = ActivitySourceVersion,
31-
};
32-
33-
return new AWSTracer(new ActivitySource(options));
34-
});
23+
#if NET
24+
awsTracer = this.tracers.GetOrAdd(scope, static (name, schemaUrl) => CreateTracer(name, schemaUrl), this.telemetrySchemaUrl);
25+
#else
26+
awsTracer = this.tracers.GetOrAdd(scope, (name) => CreateTracer(name, this.telemetrySchemaUrl));
27+
#endif
3528
}
3629

3730
return awsTracer;
31+
32+
static AWSTracer CreateTracer(string name, string telemetrySchemaUrl)
33+
{
34+
var options = new ActivitySourceOptions(name)
35+
{
36+
TelemetrySchemaUrl = telemetrySchemaUrl,
37+
Version = ActivitySourceVersion,
38+
};
39+
40+
return new AWSTracer(new ActivitySource(options));
41+
}
3842
}
3943

4044
private static string GetActivitySourceVersion()

0 commit comments

Comments
 (0)