Skip to content

Commit 33f269f

Browse files
authored
[Api] GetTracer supports schema URL (#6736)
1 parent 2a288c3 commit 33f269f

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OpenTelemetry.Trace.TracerProvider.GetTracer(string! name, string? version = null, string? schemaUrl = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? tags = null) -> OpenTelemetry.Trace.Tracer!
2+
OpenTelemetry.Trace.TracerProvider.GetTracer(string! name, string? version, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? tags) -> OpenTelemetry.Trace.Tracer!
3+
*REMOVED*OpenTelemetry.Trace.TracerProvider.GetTracer(string! name, string? version = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? tags = null) -> OpenTelemetry.Trace.Tracer!

src/OpenTelemetry.Api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Notes](../../RELEASENOTES.md).
66

77
## Unreleased
88

9+
* Added a new overload for `TracerProvider.GetTracer` which accepts an optional
10+
`string? schemaUrl` parameter, allowing a schema URL to be set on the `Tracer`.
11+
([#6736](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6736))
12+
913
## 1.14.0
1014

1115
Released 2025-Nov-12

src/OpenTelemetry.Api/Trace/TracerProvider.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Tracer GetTracer(
4040
#endif
4141
string name,
4242
string? version) =>
43-
this.GetTracer(name, version, null);
43+
this.GetTracer(name, version, null, null);
4444

4545
/// <summary>
4646
/// Gets a tracer with given name, version and tags.
@@ -49,12 +49,35 @@ public Tracer GetTracer(
4949
/// <param name="version">Version of the instrumentation library.</param>
5050
/// <param name="tags">Tags associated with the tracer.</param>
5151
/// <returns>Tracer instance.</returns>
52+
// 1.14.0 BACKCOMPAT OVERLOAD -- DO NOT TOUCH
53+
public Tracer GetTracer(
54+
#if NET
55+
[AllowNull]
56+
#endif
57+
string name,
58+
string? version,
59+
IEnumerable<KeyValuePair<string, object?>>? tags)
60+
{
61+
return this.GetTracer(name, version, null, tags);
62+
}
63+
64+
/// <summary>
65+
/// Gets a tracer with given name, version and tags.
66+
/// </summary>
67+
/// <param name="name">Name identifying the instrumentation library.</param>
68+
/// <param name="version">Version of the instrumentation library.</param>
69+
/// <param name="schemaUrl">Schema URL associated with the tracer.</param>
70+
/// <param name="tags">Tags associated with the tracer.</param>
71+
/// <returns>Tracer instance.</returns>
5272
public Tracer GetTracer(
5373
#if NET
5474
[AllowNull]
5575
#endif
5676
string name,
5777
string? version = null,
78+
#pragma warning disable CA1054 // Change the type of attribute from 'string' to 'System.Uri'
79+
string? schemaUrl = null,
80+
#pragma warning restore CA1054 // Change the type of attribute from 'string' to 'System.Uri'
5881
IEnumerable<KeyValuePair<string, object?>>? tags = null)
5982
{
6083
var tracers = this.Tracers;
@@ -64,7 +87,7 @@ public Tracer GetTracer(
6487
return new(activitySource: null);
6588
}
6689

67-
var key = new TracerKey(name, version, tags);
90+
var key = new TracerKey(name, version, schemaUrl, tags);
6891

6992
if (!tracers.TryGetValue(key, out var tracer))
7093
{
@@ -77,7 +100,14 @@ public Tracer GetTracer(
77100
return new(activitySource: null);
78101
}
79102

80-
tracer = new(new(key.Name, key.Version, key.Tags));
103+
var activitySourceOptions = new System.Diagnostics.ActivitySourceOptions(key.Name)
104+
{
105+
Version = key.Version,
106+
Tags = key.Tags,
107+
TelemetrySchemaUrl = key.SchemaUrl,
108+
};
109+
110+
tracer = new(new(activitySourceOptions));
81111
bool result = tracers.TryAdd(key, tracer);
82112
#if DEBUG
83113
System.Diagnostics.Debug.Assert(result, "Write into tracers cache failed");
@@ -118,19 +148,22 @@ internal readonly record struct TracerKey
118148
{
119149
public readonly string Name;
120150
public readonly string? Version;
151+
public readonly string? SchemaUrl;
121152
public readonly KeyValuePair<string, object?>[]? Tags;
122153

123-
public TracerKey(string? name, string? version, IEnumerable<KeyValuePair<string, object?>>? tags)
154+
public TracerKey(string? name, string? version, string? schemaUrl, IEnumerable<KeyValuePair<string, object?>>? tags)
124155
{
125156
this.Name = name ?? string.Empty;
126157
this.Version = version;
158+
this.SchemaUrl = schemaUrl;
127159
this.Tags = GetOrderedTags(tags);
128160
}
129161

130162
public bool Equals(TracerKey other)
131163
{
132164
if (!string.Equals(this.Name, other.Name, StringComparison.Ordinal) ||
133-
!string.Equals(this.Version, other.Version, StringComparison.Ordinal))
165+
!string.Equals(this.Version, other.Version, StringComparison.Ordinal) ||
166+
!string.Equals(this.SchemaUrl, other.SchemaUrl, StringComparison.Ordinal))
134167
{
135168
return false;
136169
}
@@ -146,9 +179,11 @@ public override int GetHashCode()
146179
#if NET
147180
hash = (hash * 31) + (this.Name?.GetHashCode(StringComparison.Ordinal) ?? 0);
148181
hash = (hash * 31) + (this.Version?.GetHashCode(StringComparison.Ordinal) ?? 0);
182+
hash = (hash * 31) + (this.SchemaUrl?.GetHashCode(StringComparison.Ordinal) ?? 0);
149183
#else
150184
hash = (hash * 31) + (this.Name?.GetHashCode() ?? 0);
151185
hash = (hash * 31) + (this.Version?.GetHashCode() ?? 0);
186+
hash = (hash * 31) + (this.SchemaUrl?.GetHashCode() ?? 0);
152187
#endif
153188

154189
hash = (hash * 31) + GetTagsHashCode(this.Tags);

test/OpenTelemetry.Api.Tests/Trace/TracerTests.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,55 @@ static void InnerTest()
408408
}
409409
}
410410

411+
[Fact]
412+
public void GetTracer_WithSameSchemaUrl_ReturnsSameInstance()
413+
{
414+
var schemaUrl = "http://schema";
415+
416+
using var tracerProvider = new TestTracerProvider();
417+
var tracer1 = tracerProvider.GetTracer("test", "1.0.0", schemaUrl);
418+
var tracer2 = tracerProvider.GetTracer("test", "1.0.0", schemaUrl);
419+
420+
Assert.Same(tracer1, tracer2);
421+
}
422+
423+
[Fact]
424+
public void GetTracer_WithDifferentSchemaUrls_ReturnsDifferentInstances()
425+
{
426+
var schemaUrl1 = "http://schema1";
427+
var schemaUrl2 = "http://schema2";
428+
429+
using var tracerProvider = new TestTracerProvider();
430+
var tracer1 = tracerProvider.GetTracer("test", "1.0.0", schemaUrl1);
431+
var tracer2 = tracerProvider.GetTracer("test", "1.0.0", schemaUrl2);
432+
433+
Assert.NotSame(tracer1, tracer2);
434+
}
435+
436+
[Fact]
437+
public void GetTracer_WithSchemaUrl_AppliesSchemaUrlToActivities()
438+
{
439+
var exportedItems = new List<Activity>();
440+
var schemaUrl = "http://schema";
441+
442+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
443+
.AddSource("test")
444+
.AddInMemoryExporter(exportedItems)
445+
.SetSampler(new AlwaysOnSampler())
446+
.Build();
447+
448+
var tracer = tracerProvider.GetTracer("test", "1.0.0", schemaUrl);
449+
450+
using (tracer.StartActiveSpan("TestSpan"))
451+
{
452+
// Activity started by the tracer with schema URL
453+
}
454+
455+
var activity = Assert.Single(exportedItems);
456+
457+
Assert.Equal(schemaUrl, activity.Source.TelemetrySchemaUrl);
458+
}
459+
411460
[Fact]
412461
public void GetTracer_WithSameTags_ReturnsSameInstance()
413462
{
@@ -535,7 +584,7 @@ public void GetTracer_WithTags_AppliesTagsToActivities()
535584

536585
var tracer = tracerProvider.GetTracer("test", "1.0.0", tags);
537586

538-
using (var span = tracer.StartActiveSpan("TestSpan"))
587+
using (tracer.StartActiveSpan("TestSpan"))
539588
{
540589
// Activity started by the tracer with tags
541590
}

0 commit comments

Comments
 (0)