Skip to content
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

ResourceBuilder API #1533

Merged
merged 21 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Console/TestConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static object Run(ConsoleOptions options)
// and use a custom MyProcessor, along with Console exporter.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.SetResource(Resource.Create("MyServiceName"))
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
.AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter
.AddConsoleExporter()
.Build();
Expand Down
2 changes: 1 addition & 1 deletion examples/Console/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static object Run()

using var openTelemetry = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.SetResource(Resources.CreateServiceResource("http-service-example"))
.SetResource(Resource.Create("http-service-example"))
.AddSource("http-client-test")
.AddConsoleExporter()
.Build();
Expand Down
2 changes: 1 addition & 1 deletion examples/Console/TestOTelShimWithConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static object Run(OpenTelemetryShimOptions options)
// and use a single pipeline with a custom MyProcessor, and Console exporter.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.SetResource(Resource.Create("MyServiceName"))
.AddConsoleExporter()
.Build();

Expand Down
2 changes: 1 addition & 1 deletion examples/Console/TestOpenTracingWithConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static object Run(OpenTracingShimOptions options)
// and use Console exporter.
using var openTelemetry = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyWebServer")
.SetResource(Resources.CreateServiceResource("MyServiceName"))
.SetResource(Resource.Create("MyServiceName"))
.AddConsoleExporter()
.Build();

Expand Down
3 changes: 1 addition & 2 deletions src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.Resources
OpenTelemetry.Sdk
OpenTelemetry.SimpleExportProcessor<T>
OpenTelemetry.SimpleExportProcessor<T>.SimpleExportProcessor(OpenTelemetry.BaseExporter<T> exporter) -> void
Expand Down Expand Up @@ -233,8 +232,8 @@ override abstract OpenTelemetry.BaseExportProcessor<T>.OnEnd(T data) -> void
override sealed OpenTelemetry.BaseExportProcessor<T>.OnStart(T data) -> void
readonly OpenTelemetry.BaseExportProcessor<T>.exporter -> OpenTelemetry.BaseExporter<T>
static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Create(string serviceName = null, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Empty.get -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resources.CreateServiceResource(string serviceName, string serviceInstanceId = null, string serviceNamespace = null, string serviceVersion = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void
Expand Down
3 changes: 1 addition & 2 deletions src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.Resources
OpenTelemetry.Sdk
OpenTelemetry.SimpleExportProcessor<T>
OpenTelemetry.SimpleExportProcessor<T>.SimpleExportProcessor(OpenTelemetry.BaseExporter<T> exporter) -> void
Expand Down Expand Up @@ -233,8 +232,8 @@ override abstract OpenTelemetry.BaseExportProcessor<T>.OnEnd(T data) -> void
override sealed OpenTelemetry.BaseExportProcessor<T>.OnStart(T data) -> void
readonly OpenTelemetry.BaseExportProcessor<T>.exporter -> OpenTelemetry.BaseExporter<T>
static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Create(string serviceName = null, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Empty.get -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resources.CreateServiceResource(string serviceName, string serviceInstanceId = null, string serviceNamespace = null, string serviceVersion = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void
Expand Down
3 changes: 1 addition & 2 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.Resources
OpenTelemetry.Sdk
OpenTelemetry.SimpleExportProcessor<T>
OpenTelemetry.SimpleExportProcessor<T>.SimpleExportProcessor(OpenTelemetry.BaseExporter<T> exporter) -> void
Expand Down Expand Up @@ -253,8 +252,8 @@ override sealed OpenTelemetry.BaseExportProcessor<T>.OnStart(T data) -> void
readonly OpenTelemetry.BaseExportProcessor<T>.exporter -> OpenTelemetry.BaseExporter<T>
static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddOpenTelemetry(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<OpenTelemetry.Logs.OpenTelemetryLoggerOptions> configure = null) -> Microsoft.Extensions.Logging.ILoggingBuilder
static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Create(string serviceName = null, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Empty.get -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resources.CreateServiceResource(string serviceName, string serviceInstanceId = null, string serviceNamespace = null, string serviceVersion = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource
OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) -> void
OpenTelemetry.Resources.Resources
OpenTelemetry.Sdk
OpenTelemetry.SimpleExportProcessor<T>
OpenTelemetry.SimpleExportProcessor<T>.SimpleExportProcessor(OpenTelemetry.BaseExporter<T> exporter) -> void
Expand Down Expand Up @@ -253,8 +252,8 @@ override sealed OpenTelemetry.BaseExportProcessor<T>.OnStart(T data) -> void
readonly OpenTelemetry.BaseExportProcessor<T>.exporter -> OpenTelemetry.BaseExporter<T>
static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddOpenTelemetry(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<OpenTelemetry.Logs.OpenTelemetryLoggerOptions> configure = null) -> Microsoft.Extensions.Logging.ILoggingBuilder
static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Create(string serviceName = null, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resource.Empty.get -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Resources.Resources.CreateServiceResource(string serviceName, string serviceInstanceId = null, string serviceNamespace = null, string serviceVersion = null) -> OpenTelemetry.Resources.Resource
static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void
Expand Down
59 changes: 58 additions & 1 deletion src/OpenTelemetry/Resources/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Resource
/// <summary>
/// Initializes a new instance of the <see cref="Resource"/> class.
/// </summary>
/// <param name="attributes">An <see cref="IDictionary{String, Object}"/> of attributes that describe the resource.</param>
/// <param name="attributes">An <see cref="IEnumerable{T}"/> of attributes that describe the resource.</param>
public Resource(IEnumerable<KeyValuePair<string, object>> attributes)
{
if (attributes == null)
Expand Down Expand Up @@ -73,6 +73,63 @@ public Resource(IEnumerable<KeyValuePair<string, object>> attributes)
new KeyValuePair<string, object>(TelemetrySdkVersionKey, Version.ToString()),
});

/// <summary>
/// Creates a new <see cref="Resource"/> from service information following standard convention
/// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-resource-semantic-conventions.md#service.
/// </summary>
/// <param name="serviceName">Optional name of the service.</param>
/// <param name="serviceNamespace">Optional namespace of the service.</param>
/// <param name="serviceVersion">Optional version of the service.</param>
/// <param name="autoGenerateServiceInstanceId">Specify <see langword="true"/> to automatically generate a <see cref="Guid"/> for <paramref name="serviceInstanceId"/> if not supplied.</param>
/// <param name="serviceInstanceId">Optional unique identifier of the service instance.</param>
/// <param name="attributes">Optional <see cref="IEnumerable{T}"/> of attributes that describe the service.</param>
/// <returns>Returns a new <see cref="Resource"/>.</returns>
public static Resource Create(
string serviceName = null,
string serviceNamespace = null,
string serviceVersion = null,
bool autoGenerateServiceInstanceId = true,
string serviceInstanceId = null,
IEnumerable<KeyValuePair<string, object>> attributes = null)
{
Dictionary<string, object> resourceAttributes = new Dictionary<string, object>();

if (attributes != null)
{
foreach (KeyValuePair<string, object> attribute in attributes.Select(SanitizeAttribute).ToList())
{
resourceAttributes[attribute.Key] = attribute.Value;
}
}

if (!string.IsNullOrEmpty(serviceName))
{
resourceAttributes.Add(ServiceNameKey, serviceName);
}

if (!string.IsNullOrEmpty(serviceNamespace))
{
resourceAttributes.Add(ServiceNamespaceKey, serviceNamespace);
}

if (!string.IsNullOrEmpty(serviceVersion))
{
resourceAttributes.Add(ServiceVersionKey, serviceVersion);
}

if (serviceInstanceId == null && autoGenerateServiceInstanceId)
{
serviceInstanceId = Guid.NewGuid().ToString();
}

if (serviceInstanceId != null)
{
resourceAttributes.Add(ServiceInstanceIdKey, serviceInstanceId);
}

return new Resource(resourceAttributes).GetResourceWithDefaultAttributes();
}

/// <summary>
/// Returns a new, merged <see cref="Resource"/> by merging the current <see cref="Resource"/> with the.
/// <code>other</code> <see cref="Resource"/>. In case of a collision the current <see cref="Resource"/> takes precedence.
Expand Down
64 changes: 0 additions & 64 deletions src/OpenTelemetry/Resources/Resources.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Benchmarks.Instrumentation
public class InstrumentedHttpClientBenchmark
{
private const string ActivityName = "incoming request";
private const string ResourceName = "http-service-example";
private const string ServiceName = "http-service-example";
private const string SourceName = "http-client-test";

private HttpClient httpClient;
Expand All @@ -53,7 +53,7 @@ public void GlobalSetup()

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.SetResource(Resources.CreateServiceResource(ResourceName))
.SetResource(Resource.Create(ServiceName))
.AddSource(SourceName)
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public void JaegerTraceExporter_SetResource_UpdatesServiceName()

Assert.Equal("TestService", process.ServiceName);

jaegerTraceExporter.SetResource(Resources.Resources.CreateServiceResource("MyService"));
jaegerTraceExporter.SetResource(Resource.Create("MyService"));

Assert.Equal("MyService", process.ServiceName);

jaegerTraceExporter.SetResource(Resources.Resources.CreateServiceResource("MyService", serviceNamespace: "MyNamespace"));
jaegerTraceExporter.SetResource(Resource.Create("MyService", "MyNamespace"));

Assert.Equal("MyNamespace.MyService", process.ServiceName);
}
Expand Down
Loading