diff --git a/examples/Console/TestConsoleExporter.cs b/examples/Console/TestConsoleExporter.cs index a36e5618662..4a095ef001d 100644 --- a/examples/Console/TestConsoleExporter.cs +++ b/examples/Console/TestConsoleExporter.cs @@ -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")) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) .AddProcessor(new MyProcessor()) // This must be added before ConsoleExporter .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestHttpClient.cs b/examples/Console/TestHttpClient.cs index a570daadbef..1e08e48d5d4 100644 --- a/examples/Console/TestHttpClient.cs +++ b/examples/Console/TestHttpClient.cs @@ -34,7 +34,7 @@ internal static object Run() using var openTelemetry = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation() - .SetResource(Resources.CreateServiceResource("http-service-example")) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("http-service-example")) .AddSource("http-client-test") .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestOTelShimWithConsoleExporter.cs b/examples/Console/TestOTelShimWithConsoleExporter.cs index de4fbc772a9..cb439b8a041 100644 --- a/examples/Console/TestOTelShimWithConsoleExporter.cs +++ b/examples/Console/TestOTelShimWithConsoleExporter.cs @@ -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")) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) .AddConsoleExporter() .Build(); diff --git a/examples/Console/TestOpenTracingWithConsoleExporter.cs b/examples/Console/TestOpenTracingWithConsoleExporter.cs index f8ea8b34591..5758c89640f 100644 --- a/examples/Console/TestOpenTracingWithConsoleExporter.cs +++ b/examples/Console/TestOpenTracingWithConsoleExporter.cs @@ -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")) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyServiceName")) .AddConsoleExporter() .Build(); diff --git a/src/OpenTelemetry.Api/Internal/SemanticConventions.cs b/src/OpenTelemetry.Api/Internal/SemanticConventions.cs index 0f97be39703..dd5ecc7a6c9 100644 --- a/src/OpenTelemetry.Api/Internal/SemanticConventions.cs +++ b/src/OpenTelemetry.Api/Internal/SemanticConventions.cs @@ -26,50 +26,6 @@ internal static class SemanticConventions // https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/exceptions.md #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member - public const string AttributeServiceName = "service.name"; - public const string AttributeServiceNamespace = "service.namespace"; - public const string AttributeServiceInstance = "service.instance.id"; - public const string AttributeServiceVersion = "service.version"; - - public const string AttributeTelemetrySdkName = "telemetry.sdk.name"; - public const string AttributeTelemetrySdkLanguage = "telemetry.sdk.language"; - public const string AttributeTelemetrySdkVersion = "telemetry.sdk.version"; - - public const string AttributeContainerName = "container.name"; - public const string AttributeContainerImage = "container.image.name"; - public const string AttributeContainerTag = "container.image.tag"; - - public const string AttributeFaasName = "faas.name"; - public const string AttributeFaasId = "faas.id"; - public const string AttributeFaasVersion = "faas.version"; - public const string AttributeFaasInstance = "faas.instance"; - - public const string AttributeK8sCluster = "k8s.cluster.name"; - public const string AttributeK8sNamespace = "k8s.namespace.name"; - public const string AttributeK8sPod = "k8s.pod.name"; - public const string AttributeK8sDeployment = "k8s.deployment.name"; - - public const string AttributeHostHostname = "host.hostname"; - public const string AttributeHostId = "host.id"; - public const string AttributeHostName = "host.name"; - public const string AttributeHostType = "host.type"; - public const string AttributeHostImageName = "host.image.name"; - public const string AttributeHostImageId = "host.image.id"; - public const string AttributeHostImageVersion = "host.image.version"; - - public const string AttributeProcessId = "process.id"; - public const string AttributeProcessExecutableName = "process.executable.name"; - public const string AttributeProcessExecutablePath = "process.executable.path"; - public const string AttributeProcessCommand = "process.command"; - public const string AttributeProcessCommandLine = "process.command_line"; - public const string AttributeProcessUsername = "process.username"; - - public const string AttributeCloudProvider = "cloud.provider"; - public const string AttributeCloudAccount = "cloud.account.id"; - public const string AttributeCloudRegion = "cloud.region"; - public const string AttributeCloudZone = "cloud.zone"; - public const string AttributeComponent = "component"; - public const string AttributeNetTransport = "net.transport"; public const string AttributeNetPeerIp = "net.peer.ip"; public const string AttributeNetPeerPort = "net.peer.port"; diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs index 15e7de94b30..5feb47addeb 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs @@ -105,10 +105,10 @@ internal void SetResource(Resource resource) { switch (key) { - case Resource.ServiceNameKey: + case ResourceSemanticConventions.AttributeServiceName: serviceName = strVal; continue; - case Resource.ServiceNamespaceKey: + case ResourceSemanticConventions.AttributeServiceNamespace: serviceNamespace = strVal; continue; } diff --git a/src/OpenTelemetry.Exporter.Jaeger/OpenTelemetry.Exporter.Jaeger.csproj b/src/OpenTelemetry.Exporter.Jaeger/OpenTelemetry.Exporter.Jaeger.csproj index 22b514294de..032b860fa59 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/OpenTelemetry.Exporter.Jaeger.csproj +++ b/src/OpenTelemetry.Exporter.Jaeger/OpenTelemetry.Exporter.Jaeger.csproj @@ -1,4 +1,4 @@ - + net46;netstandard2.0;netstandard2.1 @@ -18,7 +18,8 @@ - + + diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj index 734382fcb49..c6b00e0d914 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj @@ -18,7 +18,8 @@ - + + diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs index 9a6bc7fd4ca..3546073c164 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs @@ -104,7 +104,7 @@ internal void SetResource(Resource resource) } } - if (!processResource.Attributes.Any(kvp => kvp.Key == Resource.ServiceNameKey)) + if (!processResource.Attributes.Any(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceName)) { string serviceName = this.options.ServiceName; if (string.IsNullOrEmpty(serviceName)) @@ -114,7 +114,7 @@ internal void SetResource(Resource resource) processResource.Attributes.Add(new OtlpCommon.KeyValue { - Key = Resource.ServiceNameKey, + Key = ResourceSemanticConventions.AttributeServiceName, Value = new OtlpCommon.AnyValue { StringValue = serviceName }, }); } diff --git a/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj b/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj index 3a86f6d5501..4e39adbcc5e 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj +++ b/src/OpenTelemetry.Exporter.Zipkin/OpenTelemetry.Exporter.Zipkin.csproj @@ -13,7 +13,8 @@ - + + diff --git a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs index a0f8d645710..c73f4976732 100644 --- a/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs +++ b/src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs @@ -115,7 +115,7 @@ internal void SetLocalEndpointFromResource(Resource resource) switch (key) { - case Resource.ServiceNameKey: + case ResourceSemanticConventions.AttributeServiceName: serviceName = label.Value as string; continue; } diff --git a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt index a236ffc3b78..ac025c49982 100644 --- a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt @@ -126,7 +126,11 @@ OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable> OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void -OpenTelemetry.Resources.Resources +OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.AddResource(OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.Build() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.ResourceBuilder.Clear() -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.ResourceBuilderExtensions OpenTelemetry.Sdk OpenTelemetry.SimpleExportProcessor OpenTelemetry.SimpleExportProcessor.SimpleExportProcessor(OpenTelemetry.BaseExporter exporter) -> void @@ -176,10 +180,6 @@ abstract OpenTelemetry.Metrics.Export.MetricExporter.ExportAsync(System.Collecti abstract OpenTelemetry.Metrics.Export.MetricProcessor.FinishCollectionCycle(out System.Collections.Generic.IEnumerable metrics) -> void abstract OpenTelemetry.Metrics.Export.MetricProcessor.Process(OpenTelemetry.Metrics.Export.Metric metric) -> void abstract OpenTelemetry.Trace.Sampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult -const OpenTelemetry.Resources.Resource.ServiceInstanceIdKey = "service.instance.id" -> string -const OpenTelemetry.Resources.Resource.ServiceNameKey = "service.name" -> string -const OpenTelemetry.Resources.Resource.ServiceNamespaceKey = "service.namespace" -> string -const OpenTelemetry.Resources.Resource.ServiceVersionKey = "service.version" -> string override OpenTelemetry.BaseExportProcessor.Dispose(bool disposing) -> void override OpenTelemetry.BaseExportProcessor.OnShutdown(int timeoutMilliseconds) -> bool override OpenTelemetry.BatchExportProcessor.OnEnd(T data) -> void @@ -227,8 +227,13 @@ override abstract OpenTelemetry.BaseExportProcessor.OnEnd(T data) -> void override sealed OpenTelemetry.BaseExportProcessor.OnStart(T data) -> void readonly OpenTelemetry.BaseExportProcessor.exporter -> OpenTelemetry.BaseExporter static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource +static OpenTelemetry.ResourceBuilderExtensions.AddEnvironmentVariableDetector(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder 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.ResourceBuilderExtensions.AddAttributes(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, System.Collections.Generic.IEnumerable> attributes) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddService(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, string serviceName, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddTelemetrySdk(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateDefault() -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateEmpty() -> OpenTelemetry.Resources.ResourceBuilder static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void @@ -239,7 +244,7 @@ static OpenTelemetry.Trace.SamplingResult.operator !=(OpenTelemetry.Trace.Sampli static OpenTelemetry.Trace.SamplingResult.operator ==(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Build(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProvider -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Trace.TracerProviderBuilder +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResourceBuilder(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Trace.Sampler sampler) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProvider provider, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProvider static OpenTelemetry.Trace.TracerProviderExtensions.Shutdown(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt index d101bb50dd2..4f46407ac69 100644 --- a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt @@ -126,7 +126,11 @@ OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable> OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void -OpenTelemetry.Resources.Resources +OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.AddResource(OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.Build() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.ResourceBuilder.Clear() -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.ResourceBuilderExtensions OpenTelemetry.Sdk OpenTelemetry.SimpleExportProcessor OpenTelemetry.SimpleExportProcessor.SimpleExportProcessor(OpenTelemetry.BaseExporter exporter) -> void @@ -176,10 +180,6 @@ abstract OpenTelemetry.Metrics.Export.MetricExporter.ExportAsync(System.Collecti abstract OpenTelemetry.Metrics.Export.MetricProcessor.FinishCollectionCycle(out System.Collections.Generic.IEnumerable metrics) -> void abstract OpenTelemetry.Metrics.Export.MetricProcessor.Process(OpenTelemetry.Metrics.Export.Metric metric) -> void abstract OpenTelemetry.Trace.Sampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult -const OpenTelemetry.Resources.Resource.ServiceInstanceIdKey = "service.instance.id" -> string -const OpenTelemetry.Resources.Resource.ServiceNameKey = "service.name" -> string -const OpenTelemetry.Resources.Resource.ServiceNamespaceKey = "service.namespace" -> string -const OpenTelemetry.Resources.Resource.ServiceVersionKey = "service.version" -> string override OpenTelemetry.BaseExportProcessor.Dispose(bool disposing) -> void override OpenTelemetry.BaseExportProcessor.OnShutdown(int timeoutMilliseconds) -> bool override OpenTelemetry.BatchExportProcessor.OnEnd(T data) -> void @@ -227,8 +227,13 @@ override abstract OpenTelemetry.BaseExportProcessor.OnEnd(T data) -> void override sealed OpenTelemetry.BaseExportProcessor.OnStart(T data) -> void readonly OpenTelemetry.BaseExportProcessor.exporter -> OpenTelemetry.BaseExporter static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource +static OpenTelemetry.ResourceBuilderExtensions.AddEnvironmentVariableDetector(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder 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.ResourceBuilderExtensions.AddAttributes(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, System.Collections.Generic.IEnumerable> attributes) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddService(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, string serviceName, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddTelemetrySdk(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateDefault() -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateEmpty() -> OpenTelemetry.Resources.ResourceBuilder static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void @@ -237,11 +242,11 @@ static OpenTelemetry.SuppressInstrumentationScope.Begin(bool value = true) -> Sy static OpenTelemetry.SuppressInstrumentationScope.Enter() -> int static OpenTelemetry.Trace.SamplingResult.operator !=(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool static OpenTelemetry.Trace.SamplingResult.operator ==(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResourceBuilder(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProvider provider, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProvider static OpenTelemetry.Trace.TracerProviderExtensions.Shutdown(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Build(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProvider -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Trace.Sampler sampler) -> OpenTelemetry.Trace.TracerProviderBuilder virtual OpenTelemetry.BaseExporter.Dispose(bool disposing) -> void virtual OpenTelemetry.BaseExporter.OnShutdown(int timeoutMilliseconds) -> bool diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index 1f6b14d4770..d2fb0f45bb5 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -145,7 +145,11 @@ OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable> OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void -OpenTelemetry.Resources.Resources +OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.AddResource(OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.Build() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.ResourceBuilder.Clear() -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.ResourceBuilderExtensions OpenTelemetry.Sdk OpenTelemetry.SimpleExportProcessor OpenTelemetry.SimpleExportProcessor.SimpleExportProcessor(OpenTelemetry.BaseExporter exporter) -> void @@ -195,10 +199,6 @@ abstract OpenTelemetry.Metrics.Export.MetricExporter.ExportAsync(System.Collecti abstract OpenTelemetry.Metrics.Export.MetricProcessor.FinishCollectionCycle(out System.Collections.Generic.IEnumerable metrics) -> void abstract OpenTelemetry.Metrics.Export.MetricProcessor.Process(OpenTelemetry.Metrics.Export.Metric metric) -> void abstract OpenTelemetry.Trace.Sampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult -const OpenTelemetry.Resources.Resource.ServiceInstanceIdKey = "service.instance.id" -> string -const OpenTelemetry.Resources.Resource.ServiceNameKey = "service.name" -> string -const OpenTelemetry.Resources.Resource.ServiceNamespaceKey = "service.namespace" -> string -const OpenTelemetry.Resources.Resource.ServiceVersionKey = "service.version" -> string override OpenTelemetry.BaseExportProcessor.Dispose(bool disposing) -> void override OpenTelemetry.BaseExportProcessor.OnShutdown(int timeoutMilliseconds) -> bool override OpenTelemetry.BatchExportProcessor.OnEnd(T data) -> void @@ -247,8 +247,13 @@ override sealed OpenTelemetry.BaseExportProcessor.OnStart(T data) -> void readonly OpenTelemetry.BaseExportProcessor.exporter -> OpenTelemetry.BaseExporter static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddOpenTelemetry(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action configure = null) -> Microsoft.Extensions.Logging.ILoggingBuilder static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource +static OpenTelemetry.ResourceBuilderExtensions.AddEnvironmentVariableDetector(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder 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.ResourceBuilderExtensions.AddAttributes(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, System.Collections.Generic.IEnumerable> attributes) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddService(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, string serviceName, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddTelemetrySdk(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateDefault() -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateEmpty() -> OpenTelemetry.Resources.ResourceBuilder static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void @@ -257,11 +262,11 @@ static OpenTelemetry.SuppressInstrumentationScope.Begin(bool value = true) -> Sy static OpenTelemetry.SuppressInstrumentationScope.Enter() -> int static OpenTelemetry.Trace.SamplingResult.operator !=(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool static OpenTelemetry.Trace.SamplingResult.operator ==(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResourceBuilder(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProvider provider, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProvider static OpenTelemetry.Trace.TracerProviderExtensions.Shutdown(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Build(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProvider -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Trace.Sampler sampler) -> OpenTelemetry.Trace.TracerProviderBuilder virtual OpenTelemetry.BaseExporter.Dispose(bool disposing) -> void virtual OpenTelemetry.BaseExporter.OnShutdown(int timeoutMilliseconds) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 1f6b14d4770..d2fb0f45bb5 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -145,7 +145,11 @@ OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Attributes.get -> System.Collections.Generic.IEnumerable> OpenTelemetry.Resources.Resource.Merge(OpenTelemetry.Resources.Resource other) -> OpenTelemetry.Resources.Resource OpenTelemetry.Resources.Resource.Resource(System.Collections.Generic.IEnumerable> attributes) -> void -OpenTelemetry.Resources.Resources +OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.AddResource(OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.Resources.ResourceBuilder.Build() -> OpenTelemetry.Resources.Resource +OpenTelemetry.Resources.ResourceBuilder.Clear() -> OpenTelemetry.Resources.ResourceBuilder +OpenTelemetry.ResourceBuilderExtensions OpenTelemetry.Sdk OpenTelemetry.SimpleExportProcessor OpenTelemetry.SimpleExportProcessor.SimpleExportProcessor(OpenTelemetry.BaseExporter exporter) -> void @@ -195,10 +199,6 @@ abstract OpenTelemetry.Metrics.Export.MetricExporter.ExportAsync(System.Collecti abstract OpenTelemetry.Metrics.Export.MetricProcessor.FinishCollectionCycle(out System.Collections.Generic.IEnumerable metrics) -> void abstract OpenTelemetry.Metrics.Export.MetricProcessor.Process(OpenTelemetry.Metrics.Export.Metric metric) -> void abstract OpenTelemetry.Trace.Sampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult -const OpenTelemetry.Resources.Resource.ServiceInstanceIdKey = "service.instance.id" -> string -const OpenTelemetry.Resources.Resource.ServiceNameKey = "service.name" -> string -const OpenTelemetry.Resources.Resource.ServiceNamespaceKey = "service.namespace" -> string -const OpenTelemetry.Resources.Resource.ServiceVersionKey = "service.version" -> string override OpenTelemetry.BaseExportProcessor.Dispose(bool disposing) -> void override OpenTelemetry.BaseExportProcessor.OnShutdown(int timeoutMilliseconds) -> bool override OpenTelemetry.BatchExportProcessor.OnEnd(T data) -> void @@ -247,8 +247,13 @@ override sealed OpenTelemetry.BaseExportProcessor.OnStart(T data) -> void readonly OpenTelemetry.BaseExportProcessor.exporter -> OpenTelemetry.BaseExporter static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddOpenTelemetry(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action configure = null) -> Microsoft.Extensions.Logging.ILoggingBuilder static OpenTelemetry.ProviderExtensions.GetResource(this OpenTelemetry.BaseProvider baseProvider) -> OpenTelemetry.Resources.Resource +static OpenTelemetry.ResourceBuilderExtensions.AddEnvironmentVariableDetector(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder 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.ResourceBuilderExtensions.AddAttributes(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, System.Collections.Generic.IEnumerable> attributes) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddService(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder, string serviceName, string serviceNamespace = null, string serviceVersion = null, bool autoGenerateServiceInstanceId = true, string serviceInstanceId = null) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.ResourceBuilderExtensions.AddTelemetrySdk(this OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateDefault() -> OpenTelemetry.Resources.ResourceBuilder +static OpenTelemetry.Resources.ResourceBuilder.CreateEmpty() -> OpenTelemetry.Resources.ResourceBuilder static OpenTelemetry.Sdk.CreateMeterProviderBuilder() -> OpenTelemetry.Metrics.MeterProviderBuilder static OpenTelemetry.Sdk.CreateTracerProviderBuilder() -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Sdk.SetDefaultTextMapPropagator(OpenTelemetry.Context.Propagation.TextMapPropagator textMapPropagator) -> void @@ -257,11 +262,11 @@ static OpenTelemetry.SuppressInstrumentationScope.Begin(bool value = true) -> Sy static OpenTelemetry.SuppressInstrumentationScope.Enter() -> int static OpenTelemetry.Trace.SamplingResult.operator !=(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool static OpenTelemetry.Trace.SamplingResult.operator ==(OpenTelemetry.Trace.SamplingResult decision1, OpenTelemetry.Trace.SamplingResult decision2) -> bool +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResourceBuilder(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.ResourceBuilder resourceBuilder) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProvider provider, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProvider static OpenTelemetry.Trace.TracerProviderExtensions.Shutdown(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.BaseProcessor processor) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.Build(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder) -> OpenTelemetry.Trace.TracerProvider -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Resources.Resource resource) -> OpenTelemetry.Trace.TracerProviderBuilder static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, OpenTelemetry.Trace.Sampler sampler) -> OpenTelemetry.Trace.TracerProviderBuilder virtual OpenTelemetry.BaseExporter.Dispose(bool disposing) -> void virtual OpenTelemetry.BaseExporter.OnShutdown(int timeoutMilliseconds) -> bool diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index efa00f629cc..77dcc759edf 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -10,12 +10,25 @@ * `Resource` is no longer added to observed `Activity` objects as a `CustomProperty`. ([#1463](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1463)) -* Remove RentrantExportProcessor as it is not required by spec. +* Removed `RentrantExportProcessor` as it is not required by spec. * `ActivitySourceAdapter` supports setting `ActivitySource` for Activities created without `ActivitySource`. ([#1515](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1515/)) - * Implemented Shutdown for TracerProvider - ([#1489](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1489)) +* Implemented `Shutdown` for `TracerProvider`. + ([#1489](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1489)) +* `Resources.CreateServiceResource` has been removed in + favor of the `ResourceBuilder` API. + ([#1533](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1533)) +* `TracerProviderBuilder.SetResource` has been changed to + `TracerProviderBuilder.SetResourceBuilder`. + ([#1533](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1533)) +* By default `TracerProvider` will set a `Resource` containing [Telemetry + SDK](https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/resource/semantic_conventions#telemetry-sdk) + details + ([#1533](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1533)): + * `telemetry.sdk.name` = `opentelemetry` + * `telemetry.sdk.language` = `dotnet` + * `telemetry.sdk.version` = [SDK version] ## 0.8.0-beta.1 @@ -24,7 +37,6 @@ Released 2020-Nov-5 * TracerProviderBuilder API changes Renamed AddInstrumentation to AddDiagnosticSourceInstrumentation and made internal. Added AddInstrumentation ([#1454](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1454)) - * DiagnosticSource subscription helper classes (DiagnosticSourceSubscriber, ListenerHandler,PropertyFetcher) are made internal. diff --git a/src/OpenTelemetry/Exporter/PeerServiceResolver.cs b/src/OpenTelemetry/Internal/PeerServiceResolver.cs similarity index 100% rename from src/OpenTelemetry/Exporter/PeerServiceResolver.cs rename to src/OpenTelemetry/Internal/PeerServiceResolver.cs diff --git a/src/OpenTelemetry/Internal/ResourceSemanticConventions.cs b/src/OpenTelemetry/Internal/ResourceSemanticConventions.cs new file mode 100644 index 00000000000..d07d6683103 --- /dev/null +++ b/src/OpenTelemetry/Internal/ResourceSemanticConventions.cs @@ -0,0 +1,65 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +namespace OpenTelemetry.Resources +{ + internal static class ResourceSemanticConventions + { + public const string AttributeServiceName = "service.name"; + public const string AttributeServiceNamespace = "service.namespace"; + public const string AttributeServiceInstance = "service.instance.id"; + public const string AttributeServiceVersion = "service.version"; + + public const string AttributeTelemetrySdkName = "telemetry.sdk.name"; + public const string AttributeTelemetrySdkLanguage = "telemetry.sdk.language"; + public const string AttributeTelemetrySdkVersion = "telemetry.sdk.version"; + + public const string AttributeContainerName = "container.name"; + public const string AttributeContainerImage = "container.image.name"; + public const string AttributeContainerTag = "container.image.tag"; + + public const string AttributeFaasName = "faas.name"; + public const string AttributeFaasId = "faas.id"; + public const string AttributeFaasVersion = "faas.version"; + public const string AttributeFaasInstance = "faas.instance"; + + public const string AttributeK8sCluster = "k8s.cluster.name"; + public const string AttributeK8sNamespace = "k8s.namespace.name"; + public const string AttributeK8sPod = "k8s.pod.name"; + public const string AttributeK8sDeployment = "k8s.deployment.name"; + + public const string AttributeHostHostname = "host.hostname"; + public const string AttributeHostId = "host.id"; + public const string AttributeHostName = "host.name"; + public const string AttributeHostType = "host.type"; + public const string AttributeHostImageName = "host.image.name"; + public const string AttributeHostImageId = "host.image.id"; + public const string AttributeHostImageVersion = "host.image.version"; + + public const string AttributeProcessId = "process.id"; + public const string AttributeProcessExecutableName = "process.executable.name"; + public const string AttributeProcessExecutablePath = "process.executable.path"; + public const string AttributeProcessCommand = "process.command"; + public const string AttributeProcessCommandLine = "process.command_line"; + public const string AttributeProcessUsername = "process.username"; + + public const string AttributeCloudProvider = "cloud.provider"; + public const string AttributeCloudAccount = "cloud.account.id"; + public const string AttributeCloudRegion = "cloud.region"; + public const string AttributeCloudZone = "cloud.zone"; + public const string AttributeComponent = "component"; + } +} diff --git a/src/OpenTelemetry/Resources/Resource.cs b/src/OpenTelemetry/Resources/Resource.cs index c8dfe34ac3c..0cfed1936e0 100644 --- a/src/OpenTelemetry/Resources/Resource.cs +++ b/src/OpenTelemetry/Resources/Resource.cs @@ -14,7 +14,6 @@ // limitations under the License. // -using System; using System.Collections.Generic; using System.Linq; using OpenTelemetry.Internal; @@ -27,22 +26,12 @@ namespace OpenTelemetry.Resources /// public class Resource { - public const string ServiceNameKey = "service.name"; - public const string ServiceNamespaceKey = "service.namespace"; - public const string ServiceInstanceIdKey = "service.instance.id"; - public const string ServiceVersionKey = "service.version"; - private const string TelemetrySdkNameKey = "telemetry.sdk.name"; - private const string TelemetrySdkLanguageKey = "telemetry.sdk.language"; - private const string TelemetrySdkVersionKey = "telemetry.sdk.version"; - - private static readonly Version Version = typeof(Resource).Assembly.GetName().Version; - // this implementation follows https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/sdk.md /// /// Initializes a new instance of the class. /// - /// An of attributes that describe the resource. + /// An of attributes that describe the resource. public Resource(IEnumerable> attributes) { if (attributes == null) @@ -66,13 +55,6 @@ public Resource(IEnumerable> attributes) /// public IEnumerable> Attributes { get; } - private static Resource TelemetryResource { get; } = new Resource(new List> - { - new KeyValuePair(TelemetrySdkNameKey, "opentelemetry"), - new KeyValuePair(TelemetrySdkLanguageKey, "dotnet"), - new KeyValuePair(TelemetrySdkVersionKey, Version.ToString()), - }); - /// /// Returns a new, merged by merging the current with the. /// other . In case of a collision the current takes precedence. @@ -105,36 +87,9 @@ public Resource Merge(Resource other) return new Resource(newAttributes); } - /// - /// Returns a new with added attributes from telemetry sdk and the . - /// - /// . - internal Resource GetResourceWithDefaultAttributes() - { - return this.Merge(TelemetryResource).Merge(new OtelEnvResourceDetector().Detect()); - } - - /// - /// Returns a new with added attributes from resource detectors in the order of the list. - /// - /// A list of . - /// . - internal Resource GetResourceFromDetectors(List detectors) - { - var resource = this; - foreach (IResourceDetector detector in detectors) - { - resource = resource.Merge(detector.Detect()); - } - - return resource; - } - private static KeyValuePair SanitizeAttribute(KeyValuePair attribute) { - string sanitizedKey = null; - object sanitizedValue = null; - + string sanitizedKey; if (attribute.Key == null) { OpenTelemetrySdkEventSource.Log.InvalidArgument("Create resource", "attribute key", "Attribute key should be non-null string."); @@ -145,6 +100,7 @@ private static KeyValuePair SanitizeAttribute(KeyValuePair +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; + +namespace OpenTelemetry.Resources +{ + /// + /// Contains methods for building instances. + /// + public class ResourceBuilder + { + private readonly List resources = new List(); + + private ResourceBuilder() + { + } + + /// + /// Creates a instance with SDK defaults + /// added. See resource + /// semantic conventions for details. + /// + /// Created . + public static ResourceBuilder CreateDefault() + => new ResourceBuilder().AddTelemetrySdk(); // TODO: Seek spec clarify on whether or not OtelEnvResourceDetector should be added by default. + + /// + /// Creates an empty instance. + /// + /// Created . + public static ResourceBuilder CreateEmpty() + => new ResourceBuilder(); + + /// + /// Add a to the builder. + /// + /// . + /// for chaining. + public ResourceBuilder AddResource(Resource resource) + { + if (resource == null) + { + throw new ArgumentNullException(nameof(resource)); + } + + this.resources.Add(resource); + + return this; + } + + /// + /// Clears the s added to the builder. + /// + /// for chaining. + public ResourceBuilder Clear() + { + this.resources.Clear(); + + return this; + } + + /// + /// Build a merged from all the s added to the builder. + /// + /// . + public Resource Build() + { + Resource finalResource = Resource.Empty; + + foreach (Resource resource in this.resources) + { + finalResource = finalResource.Merge(resource); + } + + return finalResource; + } + + internal ResourceBuilder AddDetector(IResourceDetector resourceDetector) + { + if (resourceDetector == null) + { + throw new ArgumentNullException(nameof(resourceDetector)); + } + + Resource resource = resourceDetector.Detect(); + + if (resource != null) + { + this.resources.Add(resource); + } + + return this; + } + } +} diff --git a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs new file mode 100644 index 00000000000..9f63cc6a50f --- /dev/null +++ b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs @@ -0,0 +1,127 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; +using OpenTelemetry.Resources; + +namespace OpenTelemetry +{ + /// + /// Contains extension methods for building s. + /// + public static class ResourceBuilderExtensions + { + private static readonly Version Version = typeof(Resource).Assembly.GetName().Version; + + private static Resource TelemetryResource { get; } = new Resource(new Dictionary + { + [ResourceSemanticConventions.AttributeTelemetrySdkName] = "opentelemetry", + [ResourceSemanticConventions.AttributeTelemetrySdkLanguage] = "dotnet", + [ResourceSemanticConventions.AttributeTelemetrySdkVersion] = Version.ToString(), + }); + + /// + /// Adds service information to a + /// following semantic + /// conventions. + /// + /// . + /// Name of the service. + /// Optional namespace of the service. + /// Optional version of the service. + /// Specify to automatically generate a for if not supplied. + /// Optional unique identifier of the service instance. + /// Returns for chaining. + public static ResourceBuilder AddService( + this ResourceBuilder resourceBuilder, + string serviceName, + string serviceNamespace = null, + string serviceVersion = null, + bool autoGenerateServiceInstanceId = true, + string serviceInstanceId = null) + { + Dictionary resourceAttributes = new Dictionary(); + + if (string.IsNullOrEmpty(serviceName)) + { + throw new ArgumentNullException(nameof(serviceName)); + } + + resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceName, serviceName); + + if (!string.IsNullOrEmpty(serviceNamespace)) + { + resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceNamespace, serviceNamespace); + } + + if (!string.IsNullOrEmpty(serviceVersion)) + { + resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceVersion, serviceVersion); + } + + if (serviceInstanceId == null && autoGenerateServiceInstanceId) + { + serviceInstanceId = Guid.NewGuid().ToString(); + } + + if (serviceInstanceId != null) + { + resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceInstance, serviceInstanceId); + } + + return resourceBuilder.AddResource(new Resource(resourceAttributes)); + } + + /// + /// Adds service information to a + /// following semantic + /// conventions. + /// + /// . + /// Returns for chaining. + public static ResourceBuilder AddTelemetrySdk(this ResourceBuilder resourceBuilder) + { + return resourceBuilder.AddResource(TelemetryResource); + } + + /// + /// Adds attributes to a . + /// + /// . + /// An of attributes that describe the resource. + /// Returns for chaining. + public static ResourceBuilder AddAttributes(this ResourceBuilder resourceBuilder, IEnumerable> attributes) + { + return resourceBuilder.AddResource(new Resource(attributes)); + } + + /// + /// Adds resource attributes parsed from an environment variable to a + /// following the Resource + /// SDK. + /// + /// . + /// Returns for chaining. + public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilder resourceBuilder) + { + return resourceBuilder.AddDetector(new OtelEnvResourceDetector()); + } + } +} diff --git a/src/OpenTelemetry/Resources/Resources.cs b/src/OpenTelemetry/Resources/Resources.cs deleted file mode 100644 index 04c41909106..00000000000 --- a/src/OpenTelemetry/Resources/Resources.cs +++ /dev/null @@ -1,64 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Collections.Generic; -using OpenTelemetry.Internal; - -namespace OpenTelemetry.Resources -{ - public static class Resources - { - /// - /// Creates a new from service information following standard convention - /// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-resource-semantic-conventions.md#service. - /// - /// Name of the service. - /// Unique identifier of the service instance. - /// Optional namespace of the service. - /// Optional version of the service. - /// Returns a new . - public static Resource CreateServiceResource(string serviceName, string serviceInstanceId = null, string serviceNamespace = null, string serviceVersion = null) - { - if (serviceName == null) - { - OpenTelemetrySdkEventSource.Log.InvalidArgument("Create service resource", "serviceName", "is null"); - return Resource.Empty.GetResourceWithDefaultAttributes(); - } - - var attributes = new List> { new KeyValuePair(Resource.ServiceNameKey, serviceName), }; - - if (serviceInstanceId == null) - { - serviceInstanceId = Guid.NewGuid().ToString(); - } - - attributes.Add(new KeyValuePair(Resource.ServiceInstanceIdKey, serviceInstanceId)); - - if (serviceNamespace != null) - { - attributes.Add(new KeyValuePair(Resource.ServiceNamespaceKey, serviceNamespace)); - } - - if (serviceVersion != null) - { - attributes.Add(new KeyValuePair(Resource.ServiceVersionKey, serviceVersion)); - } - - return new Resource(attributes).GetResourceWithDefaultAttributes(); - } - } -} diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs index ada8aed10e1..32bbfc64e25 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs @@ -42,13 +42,13 @@ public static TracerProviderBuilder SetSampler(this TracerProviderBuilder tracer /// Sets the describing the app associated with all traces. Overwrites currently set resource. /// /// TracerProviderBuilder instance. - /// Resource to be associate with all traces. + /// to be associate with all traces. /// Returns for chaining. - public static TracerProviderBuilder SetResource(this TracerProviderBuilder tracerProviderBuilder, Resource resource) + public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilder tracerProviderBuilder, ResourceBuilder resourceBuilder) { if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk) { - tracerProviderBuilderSdk.SetResource(resource); + tracerProviderBuilderSdk.SetResourceBuilder(resourceBuilder); } return tracerProviderBuilder; diff --git a/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs index 174a7b958d9..89a7818e5f7 100644 --- a/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // + using System; using System.Collections.Generic; using System.Diagnostics; @@ -30,7 +31,7 @@ internal class TracerProviderBuilderSdk : TracerProviderBuilder private readonly List> processors = new List>(); private readonly List sources = new List(); - private Resource resource = Resource.Empty.GetResourceWithDefaultAttributes(); + private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault(); private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler()); internal TracerProviderBuilderSdk() @@ -95,28 +96,18 @@ public override TracerProviderBuilder AddSource(params string[] names) /// Returns for chaining. internal TracerProviderBuilder SetSampler(Sampler sampler) { - if (sampler == null) - { - throw new ArgumentNullException(nameof(sampler)); - } - - this.sampler = sampler; + this.sampler = sampler ?? throw new ArgumentNullException(nameof(sampler)); return this; } /// /// Sets the describing the app associated with all traces. Overwrites currently set resource. /// - /// Resource to be associate with all traces. + /// to be associate with all traces. /// Returns for chaining. - internal TracerProviderBuilder SetResource(Resource resource) + internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) { - if (resource == null) - { - throw new ArgumentNullException(nameof(resource)); - } - - this.resource = resource; + this.resourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder)); return this; } @@ -139,7 +130,13 @@ internal TracerProviderBuilder AddProcessor(BaseProcessor processor) internal TracerProvider Build() { - return new TracerProviderSdk(this.resource, this.sources, this.diagnosticSourceInstrumentationFactories, this.instrumentationFactories, this.sampler, this.processors); + return new TracerProviderSdk( + this.resourceBuilder.Build(), + this.sources, + this.diagnosticSourceInstrumentationFactories, + this.instrumentationFactories, + this.sampler, + this.processors); } /// diff --git a/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs b/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs index 66e382bd4b0..cfa04785d57 100644 --- a/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs +++ b/test/Benchmarks/Instrumentation/InstrumentedHttpClientBenchmark.cs @@ -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; @@ -53,7 +53,7 @@ public void GlobalSetup() this.tracerProvider = Sdk.CreateTracerProviderBuilder() .AddHttpClientInstrumentation() - .SetResource(Resources.CreateServiceResource(ResourceName)) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(ServiceName)) .AddSource(SourceName) .Build(); diff --git a/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterTests.cs b/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterTests.cs index 7c14f518fd5..334b9267150 100644 --- a/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterTests.cs @@ -57,11 +57,11 @@ public void JaegerTraceExporter_SetResource_UpdatesServiceName() Assert.Equal("TestService", process.ServiceName); - jaegerTraceExporter.SetResource(Resources.Resources.CreateServiceResource("MyService")); + jaegerTraceExporter.SetResource(ResourceBuilder.CreateEmpty().AddService("MyService").Build()); Assert.Equal("MyService", process.ServiceName); - jaegerTraceExporter.SetResource(Resources.Resources.CreateServiceResource("MyService", serviceNamespace: "MyNamespace")); + jaegerTraceExporter.SetResource(ResourceBuilder.CreateEmpty().AddService("MyService", "MyNamespace").Build()); Assert.Equal("MyNamespace.MyService", process.ServiceName); } @@ -109,8 +109,8 @@ public void JaegerTraceExporter_SetResource_IgnoreServiceResources() jaegerTraceExporter.SetResource(new Resource(new Dictionary { - [Resource.ServiceNameKey] = "servicename", - [Resource.ServiceNamespaceKey] = "servicenamespace", + [ResourceSemanticConventions.AttributeServiceName] = "servicename", + [ResourceSemanticConventions.AttributeServiceNamespace] = "servicenamespace", })); Assert.Null(process.Tags); diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterTest.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterTest.cs index d70becc737b..2f46f4aeeb9 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterTest.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterTest.cs @@ -83,8 +83,8 @@ public void ToOtlpResourceSpansTest(bool addResource, string optionsServiceName) new Resources.Resource( new List> { - new KeyValuePair(Resources.Resource.ServiceNameKey, "service-name"), - new KeyValuePair(Resources.Resource.ServiceNamespaceKey, "ns1"), + new KeyValuePair(Resources.ResourceSemanticConventions.AttributeServiceName, "service-name"), + new KeyValuePair(Resources.ResourceSemanticConventions.AttributeServiceNamespace, "ns1"), })); } else @@ -124,12 +124,12 @@ void RunTest(Batch batch) var oltpResource = request.ResourceSpans.First().Resource; if (addResource) { - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNameKey && kvp.Value.StringValue == "service-name"); - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNamespaceKey && kvp.Value.StringValue == "ns1"); + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.ResourceSemanticConventions.AttributeServiceName && kvp.Value.StringValue == "service-name"); + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.ResourceSemanticConventions.AttributeServiceNamespace && kvp.Value.StringValue == "ns1"); } else { - Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNameKey && kvp.Value.StringValue == optionsServiceName); + Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.ResourceSemanticConventions.AttributeServiceName && kvp.Value.StringValue == optionsServiceName); } foreach (var instrumentationLibrarySpans in request.ResourceSpans.First().InstrumentationLibrarySpans) diff --git a/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs b/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs index f19c2c91f2a..329a4161163 100644 --- a/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs @@ -157,7 +157,7 @@ public void IntegrationTest(bool useShortTraceIds, bool useTestResource, bool is exporter.SetLocalEndpointFromResource(new Resource(new Dictionary { - [Resource.ServiceNameKey] = serviceName, + [ResourceSemanticConventions.AttributeServiceName] = serviceName, ["service.tag"] = "hello world", })); diff --git a/test/OpenTelemetry.Tests/Resources/ResourceBuilderTests.cs b/test/OpenTelemetry.Tests/Resources/ResourceBuilderTests.cs new file mode 100644 index 00000000000..96d951bd54f --- /dev/null +++ b/test/OpenTelemetry.Tests/Resources/ResourceBuilderTests.cs @@ -0,0 +1,86 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace OpenTelemetry.Resources.Tests +{ + public class ResourceBuilderTests + { + [Fact] + public void ServiceResource_ServiceName() + { + var resource = ResourceBuilder.CreateEmpty().AddService("my-service").Build(); + Assert.Equal(2, resource.Attributes.Count()); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + Assert.Single(resource.Attributes.Where(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceName)); + Assert.True(Guid.TryParse((string)resource.Attributes.Single(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceInstance).Value, out _)); + } + + [Fact] + public void ServiceResource_ServiceNameAndInstance() + { + var resource = ResourceBuilder.CreateEmpty().AddService("my-service", serviceInstanceId: "123").Build(); + Assert.Equal(2, resource.Attributes.Count()); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceInstance, "123"), resource.Attributes); + } + + [Fact] + public void ServiceResource_ServiceNameAndInstanceAndNamespace() + { + var resource = ResourceBuilder.CreateEmpty().AddService("my-service", "my-namespace", serviceInstanceId: "123").Build(); + Assert.Equal(3, resource.Attributes.Count()); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceInstance, "123"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceNamespace, "my-namespace"), resource.Attributes); + } + + [Fact] + public void ServiceResource_ServiceNameAndInstanceAndNamespaceAndVersion() + { + var resource = ResourceBuilder.CreateEmpty().AddService("my-service", "my-namespace", "semVer:1.2.3", serviceInstanceId: "123").Build(); + Assert.Equal(4, resource.Attributes.Count()); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceInstance, "123"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceNamespace, "my-namespace"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceVersion, "semVer:1.2.3"), resource.Attributes); + } + + [Fact] + public void ServiceResource_AutoGenerateServiceInstanceIdOff() + { + var resource = ResourceBuilder.CreateEmpty().AddService("my-service", autoGenerateServiceInstanceId: false).Build(); + Assert.Single(resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + } + + [Fact] + public void ClearTest() + { + var resource = ResourceBuilder.CreateEmpty() + .AddTelemetrySdk() + .Clear() + .AddService("my-service", autoGenerateServiceInstanceId: false) + .Build(); + Assert.Single(resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "my-service"), resource.Attributes); + } + } +} diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 6c91ec06237..dea22152802 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -26,7 +26,6 @@ public class ResourceTest : IDisposable private const string KeyName = "key"; private const string ValueName = "value"; private const string OtelEnvVarKey = "OTEL_RESOURCE_ATTRIBUTES"; - private static readonly Random Random = new Random(); public ResourceTest() { @@ -339,11 +338,10 @@ public void MergeResource_SecondaryCanOverridePrimaryEmptyAttributeValue() public void GetResourceWithDefaultAttributes_EmptyResource() { // Arrange - var resource = Resource.Empty; - var new_resource = resource.GetResourceWithDefaultAttributes(); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().Build(); // Assert - var attributes = new_resource.Attributes; + var attributes = resource.Attributes; Assert.Equal(3, attributes.Count()); ValidateTelemetrySdkAttributes(attributes); } @@ -352,11 +350,10 @@ public void GetResourceWithDefaultAttributes_EmptyResource() public void GetResourceWithDefaultAttributes_ResourceWithAttrs() { // Arrange - var resource = new Resource(this.CreateAttributes(2)); - var new_resource = resource.GetResourceWithDefaultAttributes(); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert - var attributes = new_resource.Attributes; + var attributes = resource.Attributes; Assert.Equal(5, attributes.Count()); ValidateAttributes(attributes, 0, 1); ValidateTelemetrySdkAttributes(attributes); @@ -367,11 +364,10 @@ public void GetResourceWithDefaultAttributes_WithEnvVar() { // Arrange Environment.SetEnvironmentVariable(OtelEnvVarKey, "EVKey1=EVVal1,EVKey2=EVVal2"); - var resource = new Resource(this.CreateAttributes(2)); - var new_resource = resource.GetResourceWithDefaultAttributes(); + var resource = ResourceBuilder.CreateDefault().AddEnvironmentVariableDetector().AddAttributes(this.CreateAttributes(2)).Build(); // Assert - var attributes = new_resource.Attributes; + var attributes = resource.Attributes; Assert.Equal(7, attributes.Count()); ValidateAttributes(attributes, 0, 1); ValidateTelemetrySdkAttributes(attributes); @@ -379,26 +375,6 @@ public void GetResourceWithDefaultAttributes_WithEnvVar() Assert.Contains(new KeyValuePair("EVKey2", "EVVal2"), attributes); } - [Fact] - public void GetResourceFromDetectors_OtelEnvDetector() - { - // Arrange - Environment.SetEnvironmentVariable(OtelEnvVarKey, "EVKey11=EVVal11,EVKey22=EVVal22"); - var detectors = new List - { - new OtelEnvResourceDetector(), - }; - var resource = new Resource(this.CreateAttributes(2)); - var new_resource = resource.GetResourceFromDetectors(detectors); - - // Assert - var attributes = new_resource.Attributes; - Assert.Equal(4, attributes.Count()); - ValidateAttributes(attributes, 0, 1); - Assert.Contains(new KeyValuePair("EVKey11", "EVVal11"), attributes); - Assert.Contains(new KeyValuePair("EVKey22", "EVVal22"), attributes); - } - public void Dispose() { Environment.SetEnvironmentVariable(OtelEnvVarKey, null); @@ -440,13 +416,6 @@ private static void ValidateTelemetrySdkAttributes(IEnumerable s[Random.Next(s.Length)]).ToArray()); - } - private Dictionary CreateAttributes(int attributeCount, int startIndex = 0) { var attributes = new Dictionary(); diff --git a/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs b/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs deleted file mode 100644 index 273ef0d726c..00000000000 --- a/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs +++ /dev/null @@ -1,87 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Collections.Generic; -using System.Linq; -using OpenTelemetry.Trace; -using Xunit; - -namespace OpenTelemetry.Resources.Tests -{ - public class ResourcesTests - { - [Fact] - public void ServiceResource_ServiceName() - { - var resource = OpenTelemetry.Resources.Resources.CreateServiceResource("my-service"); - Assert.Equal(5, resource.Attributes.Count()); - Assert.Contains(new KeyValuePair(Resource.ServiceNameKey, "my-service"), resource.Attributes); - Assert.Single(resource.Attributes.Where(kvp => kvp.Key == Resource.ServiceNameKey)); - Assert.True(Guid.TryParse((string)resource.Attributes.Single(kvp => kvp.Key == Resource.ServiceInstanceIdKey).Value, out _)); - this.AssertDefaultAttributes(resource); - } - - [Fact] - public void ServiceResource_ServiceNameAndInstance() - { - var resource = OpenTelemetry.Resources.Resources.CreateServiceResource("my-service", "123"); - Assert.Equal(5, resource.Attributes.Count()); - Assert.Contains(new KeyValuePair(Resource.ServiceNameKey, "my-service"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceInstanceIdKey, "123"), resource.Attributes); - this.AssertDefaultAttributes(resource); - } - - [Fact] - public void ServiceResource_ServiceNameAndInstanceAndNamespace() - { - var resource = OpenTelemetry.Resources.Resources.CreateServiceResource("my-service", "123", "my-namespace"); - Assert.Equal(6, resource.Attributes.Count()); - Assert.Contains(new KeyValuePair(Resource.ServiceNameKey, "my-service"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceInstanceIdKey, "123"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceNamespaceKey, "my-namespace"), resource.Attributes); - this.AssertDefaultAttributes(resource); - } - - [Fact] - public void ServiceResource_ServiceNameAndInstanceAndNamespaceAndVersion() - { - var resource = OpenTelemetry.Resources.Resources.CreateServiceResource("my-service", "123", "my-namespace", "semVer:1.2.3"); - Assert.Equal(7, resource.Attributes.Count()); - Assert.Contains(new KeyValuePair(Resource.ServiceNameKey, "my-service"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceInstanceIdKey, "123"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceNamespaceKey, "my-namespace"), resource.Attributes); - Assert.Contains(new KeyValuePair(Resource.ServiceVersionKey, "semVer:1.2.3"), resource.Attributes); - this.AssertDefaultAttributes(resource); - } - - [Fact] - public void ServiceResource_NullParams() - { - var resource = OpenTelemetry.Resources.Resources.CreateServiceResource(null); - Assert.Equal(3, resource.Attributes.Count()); - this.AssertDefaultAttributes(resource); - } - - private void AssertDefaultAttributes(Resource resource) - { - Assert.Contains(new KeyValuePair(SemanticConventions.AttributeTelemetrySdkName, "opentelemetry"), resource.Attributes); - Assert.Contains(new KeyValuePair(SemanticConventions.AttributeTelemetrySdkLanguage, "dotnet"), resource.Attributes); - var versionAttribute = resource.Attributes.Where(pair => pair.Key.Equals("telemetry.sdk.version")); - Assert.Single(versionAttribute); - } - } -}