-
Notifications
You must be signed in to change notification settings - Fork 378
[Infra] Add factory for ActivitySource and Meter #4079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martincostello
merged 2 commits into
open-telemetry:main
from
martincostello:gh-4064-add-telemetry-factories
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Trace; | ||
|
|
||
| internal static class ActivitySourceFactory | ||
| { | ||
| /// <summary> | ||
| /// Creates a new <see cref="ActivitySource"/> for the assembly associated | ||
| /// with the specified type and semantic conventions version. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type for which the <see cref="ActivitySource"/> is created for its assembly.</typeparam> | ||
| /// <param name="semanticConventionsVersion">The version of the semantic conventions.</param> | ||
| /// <returns>A new <see cref="ActivitySource"/> instance.</returns> | ||
| public static ActivitySource Create<T>(Version semanticConventionsVersion) | ||
| => Create(typeof(T), semanticConventionsVersion); | ||
|
|
||
| /// <summary> | ||
| /// Creates a new <see cref="ActivitySource"/> for the assembly associated | ||
| /// with the specified type and semantic conventions version. | ||
| /// </summary> | ||
| /// <param name="type">The type for which the <see cref="ActivitySource"/> is created for its assembly.</param> | ||
| /// <param name="semanticConventionsVersion">The version of the semantic conventions.</param> | ||
| /// <returns>A new <see cref="ActivitySource"/> instance.</returns> | ||
| public static ActivitySource Create(Type type, Version semanticConventionsVersion) | ||
| { | ||
| Guard.ThrowIfNull(type); | ||
| Guard.ThrowIfNull(semanticConventionsVersion); | ||
|
|
||
| string telemetrySchemaUrl = $"https://opentelemetry.io/schemas/{semanticConventionsVersion.ToString(3)}"; | ||
|
|
||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var assembly = type.Assembly; | ||
| var assemblyName = assembly.GetName(); | ||
| #pragma warning disable IDE0370 // Suppression is unnecessary | ||
| var name = assemblyName.Name!; | ||
| #pragma warning restore IDE0370 // Suppression is unnecessary | ||
| var version = assembly.GetPackageVersion(); | ||
|
|
||
| var options = new ActivitySourceOptions(name) | ||
| { | ||
| TelemetrySchemaUrl = telemetrySchemaUrl, | ||
| Version = version, | ||
| }; | ||
|
|
||
| return new(options); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics.Metrics; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Trace; | ||
|
|
||
| internal static class MeterFactory | ||
| { | ||
| /// <summary> | ||
| /// Creates a new <see cref="Meter"/> for the assembly associated | ||
| /// with the specified type and semantic conventions version. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type for which the <see cref="Meter"/> is created for its assembly.</typeparam> | ||
| /// <param name="semanticConventionsVersion">The version of the semantic conventions.</param> | ||
| /// <returns>A new <see cref="Meter"/> instance.</returns> | ||
| public static Meter Create<T>(Version semanticConventionsVersion) | ||
| => Create(typeof(T), semanticConventionsVersion); | ||
|
|
||
| /// <summary> | ||
| /// Creates a new <see cref="Meter"/> for the assembly associated | ||
| /// with the specified type and semantic conventions version. | ||
| /// </summary> | ||
| /// <param name="type">The type for which the <see cref="Meter"/> is created for its assembly.</param> | ||
| /// <param name="semanticConventionsVersion">The version of the semantic conventions.</param> | ||
| /// <returns>A new <see cref="Meter"/> instance.</returns> | ||
| public static Meter Create(Type type, Version semanticConventionsVersion) | ||
| { | ||
| Guard.ThrowIfNull(type); | ||
| Guard.ThrowIfNull(semanticConventionsVersion); | ||
|
|
||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| string telemetrySchemaUrl = $"https://opentelemetry.io/schemas/{semanticConventionsVersion.ToString(3)}"; | ||
|
|
||
| var assembly = type.Assembly; | ||
| var assemblyName = assembly.GetName(); | ||
| #pragma warning disable IDE0370 // Suppression is unnecessary | ||
| var name = assemblyName.Name!; | ||
| #pragma warning restore IDE0370 // Suppression is unnecessary | ||
| var version = assembly.GetPackageVersion(); | ||
|
|
||
| var options = new MeterOptions(name) | ||
| { | ||
| TelemetrySchemaUrl = telemetrySchemaUrl, | ||
| Version = version, | ||
| }; | ||
|
|
||
| return new(options); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
test/OpenTelemetry.Contrib.Shared.Tests/ActivitySourceFactoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using OpenTelemetry.Trace; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.Tests; | ||
|
|
||
| public class ActivitySourceFactoryTests | ||
| { | ||
| [Fact] | ||
| public void Create_ReturnsActivitySourceWithGivenName() | ||
| { | ||
| // Arrange | ||
| var semanticConventionsVersion = new Version(1, 2, 3, 4); | ||
|
|
||
| // Act | ||
| var actual = ActivitySourceFactory.Create<ActivitySourceFactoryTests>(semanticConventionsVersion); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(actual); | ||
| Assert.Equal("OpenTelemetry.Contrib.Shared.Tests", actual.Name); | ||
| Assert.NotNull(actual.Version); | ||
| Assert.NotEmpty(actual.Version); | ||
| Assert.Equal("https://opentelemetry.io/schemas/1.2.3", actual.TelemetrySchemaUrl); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
test/OpenTelemetry.Contrib.Shared.Tests/MeterFactoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using OpenTelemetry.Trace; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.Tests; | ||
|
|
||
| public class MeterFactoryTests | ||
| { | ||
| [Fact] | ||
| public void Create_ReturnsMeterWithGivenName() | ||
| { | ||
| // Arrange | ||
| var semanticConventionsVersion = new Version(1, 2, 3, 4); | ||
|
|
||
| // Act | ||
| var actual = MeterFactory.Create<MeterFactoryTests>(semanticConventionsVersion); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(actual); | ||
| Assert.Equal("OpenTelemetry.Contrib.Shared.Tests", actual.Name); | ||
| Assert.NotNull(actual.Version); | ||
| Assert.NotEmpty(actual.Version); | ||
| Assert.Equal("https://opentelemetry.io/schemas/1.2.3", actual.TelemetrySchemaUrl); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.