diff --git a/samples/Exporters/TestApplicationInsights.cs b/samples/Exporters/TestApplicationInsights.cs index 6824d3d504d..3080de8ee91 100644 --- a/samples/Exporters/TestApplicationInsights.cs +++ b/samples/Exporters/TestApplicationInsights.cs @@ -26,7 +26,6 @@ namespace Samples using OpenTelemetry.Stats.Measures; using OpenTelemetry.Tags; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Sampler; @@ -59,7 +58,8 @@ internal static object Run() var tagContextBuilder = Tagger.CurrentBuilder.Put(FrontendKey, TagValue.Create("mobile-ios9.3.5")); - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); var spanBuilder = tracer .SpanBuilder("incoming request") .SetRecordEvents(true) diff --git a/samples/Exporters/TestHttpClient.cs b/samples/Exporters/TestHttpClient.cs index ed954cb2097..bd1cb649240 100644 --- a/samples/Exporters/TestHttpClient.cs +++ b/samples/Exporters/TestHttpClient.cs @@ -22,7 +22,6 @@ namespace Samples using OpenTelemetry.Collector.Dependencies; using OpenTelemetry.Exporter.Zipkin; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Sampler; @@ -39,8 +38,9 @@ internal static object Run() ServiceName = typeof(Program).Assembly.GetName().Name, }); - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); - using (new DependenciesCollector(new DependenciesCollectorOptions(), tracer, Samplers.AlwaysSample)) + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); + using (new DependenciesCollector(new DependenciesCollectorOptions(), tracerFactory, Samplers.AlwaysSample)) { using (tracer.WithSpan(tracer.SpanBuilder("incoming request").SetSampler(Samplers.AlwaysSample).StartSpan())) { diff --git a/samples/Exporters/TestJaeger.cs b/samples/Exporters/TestJaeger.cs index 52e217ad612..f55dfe4fbfa 100644 --- a/samples/Exporters/TestJaeger.cs +++ b/samples/Exporters/TestJaeger.cs @@ -20,9 +20,7 @@ namespace Samples using System.Collections.Generic; using System.Threading; using OpenTelemetry.Exporter.Jaeger; - using OpenTelemetry.Exporter.Jaeger.Implementation; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; internal class TestJaeger @@ -41,7 +39,8 @@ internal static object Run(string host, int port) jaegerOptions); // Create a tracer. You may also need to register it as a global instance to make auto-collectors work.. - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); // Create a scoped span. It will end automatically when using statement ends using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan())) @@ -59,7 +58,7 @@ internal static object Run(string host, int port) return null; } - private static void DoWork(int i, Tracer tracer) + private static void DoWork(int i, ITracer tracer) { // Start another span. If another span was already started, it'll use that span as the parent span. // In this example, the main method already started a span, so that'll be the parent span, and this will be diff --git a/samples/Exporters/TestLightstep.cs b/samples/Exporters/TestLightstep.cs index a956f64739b..d59cd51e424 100644 --- a/samples/Exporters/TestLightstep.cs +++ b/samples/Exporters/TestLightstep.cs @@ -7,7 +7,6 @@ namespace Samples using System.Threading; using OpenTelemetry.Exporter.LightStep; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; internal class TestLightstep @@ -21,7 +20,8 @@ internal static object Run(string accessToken) ServiceName = "tracing-to-lightstep-service", }); - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan())) { @@ -38,7 +38,7 @@ internal static object Run(string accessToken) return null; } - private static void DoWork(int i, Tracer tracer) + private static void DoWork(int i, ITracer tracer) { using (tracer.WithSpan(tracer.SpanBuilder("DoWork").StartSpan())) { diff --git a/samples/Exporters/TestRedis.cs b/samples/Exporters/TestRedis.cs index c6b7e85f2c9..b0f20152051 100644 --- a/samples/Exporters/TestRedis.cs +++ b/samples/Exporters/TestRedis.cs @@ -22,7 +22,6 @@ namespace Samples using OpenTelemetry.Collector.StackExchangeRedis; using OpenTelemetry.Exporter.Zipkin; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using StackExchange.Redis; @@ -39,7 +38,8 @@ internal static object Run(string zipkinUri) }); // Create a tracer. You may also need to register it as a global instance to make auto-collectors work.. - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); var collector = new StackExchangeRedisCallsCollector(tracer); @@ -65,7 +65,7 @@ internal static object Run(string zipkinUri) return null; } - private static void DoWork(IDatabase db, Tracer tracer) + private static void DoWork(IDatabase db, ITracer tracer) { // Start another span. If another span was already started, it'll use that span as the parent span. // In this example, the main method already started a span, so that'll be the parent span, and this will be diff --git a/samples/Exporters/TestStackdriver.cs b/samples/Exporters/TestStackdriver.cs index 2cca00b1208..bd5c4ded711 100644 --- a/samples/Exporters/TestStackdriver.cs +++ b/samples/Exporters/TestStackdriver.cs @@ -25,7 +25,6 @@ namespace Samples using OpenTelemetry.Stats.Measures; using OpenTelemetry.Tags; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Sampler; @@ -57,7 +56,8 @@ internal static object Run(string projectId) Stats.ViewManager); metricExporter.Start(); - var tracer = new Tracer(new BatchingSpanProcessor(spanExporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(spanExporter)); + var tracer = tracerFactory.GetTracer(string.Empty); var tagContextBuilder = Tagger.CurrentBuilder.Put(FrontendKey, TagValue.Create("mobile-ios9.3.5")); diff --git a/samples/Exporters/TestZipkin.cs b/samples/Exporters/TestZipkin.cs index 325eabc0045..37824ac3a46 100644 --- a/samples/Exporters/TestZipkin.cs +++ b/samples/Exporters/TestZipkin.cs @@ -21,7 +21,6 @@ namespace Samples using System.Threading; using OpenTelemetry.Exporter.Zipkin; using OpenTelemetry.Trace; - using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; internal class TestZipkin @@ -37,7 +36,8 @@ internal static object Run(string zipkinUri) }); // Create a tracer. You may also need to register it as a global instance to make auto-collectors work.. - var tracer = new Tracer(new BatchingSpanProcessor(exporter), TraceConfig.Default); + var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter)); + var tracer = tracerFactory.GetTracer(string.Empty); // Create a scoped span. It will end automatically when using statement ends using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan())) @@ -54,7 +54,7 @@ internal static object Run(string zipkinUri) return null; } - private static void DoWork(int i, Tracer tracer) + private static void DoWork(int i, ITracer tracer) { // Start another span. If another span was already started, it'll use that span as the parent span. // In this example, the main method already started a span, so that'll be the parent span, and this will be diff --git a/samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.cs b/samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.cs index 96a290f79f5..eefe20fa262 100644 --- a/samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.cs +++ b/samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.cs @@ -15,7 +15,7 @@ internal static class LoggingTracerExtensions { internal static void AddLoggingTracer(this IServiceCollection services) { - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(Samplers.AlwaysSample); services.AddSingleton(); diff --git a/samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/Program.cs b/samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/Program.cs index 3d6d2b42a23..20ea8aef3f9 100644 --- a/samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/Program.cs +++ b/samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/Program.cs @@ -9,19 +9,20 @@ namespace LoggingTracer.Demo.ConsoleApp public class Program { - private static ITracer tracer = new LoggingTracer(); - public static async Task Main(string[] args) { + var tracerFactory = new LoggingTracerFactory(); + var tracer = tracerFactory.GetTracer("ConsoleApp", "semver:1.0.0"); + var builder = tracer.SpanBuilder("Main (span1)"); using (tracer.WithSpan(builder.StartSpan())) { await Task.Delay(100); - await Foo(); + await Foo(tracer); } } - private static async Task Foo() + private static async Task Foo(ITracer tracer) { var builder = tracer.SpanBuilder("Foo (span2)"); using (tracer.WithSpan(builder.StartSpan())) diff --git a/samples/LoggingTracer/LoggingTracer/LoggingTracer.cs b/samples/LoggingTracer/LoggingTracer/LoggingTracer.cs index 8d76e31662f..ac1d9c81381 100644 --- a/samples/LoggingTracer/LoggingTracer/LoggingTracer.cs +++ b/samples/LoggingTracer/LoggingTracer/LoggingTracer.cs @@ -2,6 +2,9 @@ // Copyright (c) PlaceholderCompany. All rights reserved. // +using System.Linq; +using OpenTelemetry.Resources; + namespace LoggingTracer { using System; @@ -10,6 +13,14 @@ namespace LoggingTracer public class LoggingTracer : ITracer { + private string prefix; + + internal LoggingTracer(Resource libraryResource) + { + this.prefix = "Tracer(" + string.Join(", ", libraryResource.Labels.Select(l => l.Value).ToArray()) + ")"; + Logger.Log($"{prefix}.ctor()"); + } + public ISpan CurrentSpan => CurrentSpanUtils.CurrentSpan; public IBinaryFormat BinaryFormat => new LoggingBinaryFormat(); @@ -18,13 +29,13 @@ public class LoggingTracer : ITracer public ISpanBuilder SpanBuilder(string spanName) { - Logger.Log($"Tracer.SpanBuilder({spanName})"); + Logger.Log($"{prefix}.SpanBuilder({spanName})"); return new LoggingSpanBuilder(spanName, SpanKind.Internal); } public IDisposable WithSpan(ISpan span) { - Logger.Log("Tracer.WithSpan"); + Logger.Log($"{prefix}.WithSpan"); return new CurrentSpanUtils.LoggingScope(span); } } diff --git a/samples/LoggingTracer/LoggingTracer/LoggingTracerFactory.cs b/samples/LoggingTracer/LoggingTracer/LoggingTracerFactory.cs new file mode 100644 index 00000000000..e363c10d417 --- /dev/null +++ b/samples/LoggingTracer/LoggingTracer/LoggingTracerFactory.cs @@ -0,0 +1,35 @@ +// +// Copyright (c) PlaceholderCompany. All rights reserved. +// + +using System.Collections.Generic; +using OpenTelemetry.Resources; + +namespace LoggingTracer +{ + using OpenTelemetry.Context; + using OpenTelemetry.Context.Propagation; + using OpenTelemetry.Trace; + + public class LoggingTracerFactory : ITracerFactory + { + public override ITracer GetTracer(string name, string version = null) + { + Logger.Log($"TracerFactory.GetTracer('{name}', '{version}')"); + + // Create a Resource from "name" and "version" information. + var labels = new Dictionary(); + if (!string.IsNullOrEmpty(name)) + { + labels.Add("name", name); + if (!string.IsNullOrEmpty(version)) + { + labels.Add("version", version); + } + } + var libraryResource = Resource.Create(labels); + + return new LoggingTracer(libraryResource); + } + } +} diff --git a/src/OpenTelemetry.Abstractions/Trace/ITracerFactory.cs b/src/OpenTelemetry.Abstractions/Trace/ITracerFactory.cs new file mode 100644 index 00000000000..980714c5acf --- /dev/null +++ b/src/OpenTelemetry.Abstractions/Trace/ITracerFactory.cs @@ -0,0 +1,35 @@ +// +// Copyright 2018, 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.Trace +{ + using OpenTelemetry.Context; + using OpenTelemetry.Context.Propagation; + + /// + /// Creates Tracers for an instrumentation library. + /// + public abstract class ITracerFactory + { + /// + /// Returns an ITracer for a given name and version. + /// + /// Name of the instrumentation library. + /// Version of the instrumentation library (optional). + /// Tracer for the given name and version information. + public abstract ITracer GetTracer(string name, string version = null); + } +} diff --git a/src/OpenTelemetry.Collector.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Collector.AspNetCore/Implementation/HttpInListener.cs index 039dd11f045..873d3017440 100644 --- a/src/OpenTelemetry.Collector.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Collector.AspNetCore/Implementation/HttpInListener.cs @@ -1,198 +1,198 @@ -// -// Copyright 2018, 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.Collector.AspNetCore.Implementation -{ - using System; - using System.Diagnostics; - using System.Linq; - using System.Text; - using Microsoft.AspNetCore.Http; - using Microsoft.AspNetCore.Http.Features; - using OpenTelemetry.Trace; - - internal class HttpInListener : ListenerHandler - { - private static readonly string UnknownHostName = "UNKNOWN-HOST"; - private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext"); - private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext"); - private readonly PropertyFetcher beforeActionActionDescriptorFetcher = new PropertyFetcher("actionDescriptor"); - private readonly PropertyFetcher beforeActionAttributeRouteInfoFetcher = new PropertyFetcher("AttributeRouteInfo"); - private readonly PropertyFetcher beforeActionTemplateFetcher = new PropertyFetcher("Template"); - private readonly bool hostingSupportsW3C = false; - - public HttpInListener(ITracer tracer, Func samplerFactory) - : base("Microsoft.AspNetCore", tracer, samplerFactory) - { - this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3; - } - - public override void OnStartActivity(Activity activity, object payload) - { - const string EventNameSuffix = ".OnStartActivity"; - var context = this.startContextFetcher.Fetch(payload) as HttpContext; - - if (context == null) - { - CollectorEventSource.Log.NullPayload(nameof(HttpInListener) + EventNameSuffix); - return; - } - - var request = context.Request; - - SpanContext ctx = null; - if (!this.hostingSupportsW3C) - { - ctx = this.Tracer.TextFormat.Extract( - request, - (r, name) => r.Headers[name]); - } - - // see the spec https://github.com/open-telemetry/OpenTelemetry-specs/blob/master/trace/HTTP.md - var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; - - var spanBuilder = this.Tracer.SpanBuilder(path) - .SetSpanKind(SpanKind.Server) - .SetSampler(this.SamplerFactory(request)); - - if (this.hostingSupportsW3C) - { - spanBuilder.SetCreateChild(false); - } - else - { - spanBuilder.SetParent(ctx); - } - - var span = spanBuilder.StartSpan(); - this.Tracer.WithSpan(span); - - if (span.IsRecordingEvents) - { - // Note, route is missing at this stage. It will be available later - span.PutHttpHostAttribute(request.Host.Host, request.Host.Port ?? 80); - span.PutHttpMethodAttribute(request.Method); - span.PutHttpPathAttribute(path); - - var userAgent = request.Headers["User-Agent"].FirstOrDefault(); - span.PutHttpUserAgentAttribute(userAgent); - span.PutHttpRawUrlAttribute(GetUri(request)); - } - } - - public override void OnStopActivity(Activity activity, object payload) - { - const string EventNameSuffix = ".OnStopActivity"; - var span = this.Tracer.CurrentSpan; - - if (span == null || span == BlankSpan.Instance) - { - CollectorEventSource.Log.NullOrBlankSpan(nameof(HttpInListener) + EventNameSuffix); - return; - } - - if (!span.IsRecordingEvents) - { - span.End(); - return; - } - - if (!(this.stopContextFetcher.Fetch(payload) is HttpContext context)) - { - CollectorEventSource.Log.NullPayload(nameof(HttpInListener) + EventNameSuffix); - return; - } - - var response = context.Response; - - span.PutHttpStatusCode(response.StatusCode, response.HttpContext.Features.Get().ReasonPhrase); - span.End(); - } - - public override void OnCustom(string name, Activity activity, object payload) - { - if (name == "Microsoft.AspNetCore.Mvc.BeforeAction") - { - var span = this.Tracer.CurrentSpan; - - if (span == null) - { - CollectorEventSource.Log.NullOrBlankSpan(name); - return; - } - - if (span.IsRecordingEvents) - { - // See https://github.com/aspnet/Mvc/blob/2414db256f32a047770326d14d8b0e2afd49ba49/src/Microsoft.AspNetCore.Mvc.Core/MvcCoreDiagnosticSourceExtensions.cs#L36-L44 - // Reflection accessing: ActionDescriptor.AttributeRouteInfo.Template - // The reason to use reflection is to avoid a reference on MVC package. - // This package can be used with non-MVC apps and this logic simply wouldn't run. - // Taking reference on MVC will increase size of deployment for non-MVC apps. - var actionDescriptor = this.beforeActionActionDescriptorFetcher.Fetch(payload); - var attributeRouteInfo = this.beforeActionAttributeRouteInfoFetcher.Fetch(actionDescriptor); - var template = this.beforeActionTemplateFetcher.Fetch(attributeRouteInfo) as string; - - if (!string.IsNullOrEmpty(template)) - { - // override the span name that was previously set to the path part of URL. - span.UpdateName(template); - - span.PutHttpRouteAttribute(template); - } - - // TODO: Should we get values from RouteData? - // private readonly PropertyFetcher beforActionRouteDataFetcher = new PropertyFetcher("routeData"); - // var routeData = this.beforActionRouteDataFetcher.Fetch(payload) as RouteData; - } - } - } - - private static string GetUri(HttpRequest request) - { - var builder = new StringBuilder(); - - builder.Append(request.Scheme).Append("://"); - - if (request.Host.HasValue) - { - builder.Append(request.Host.Value); - } - else - { - // HTTP 1.0 request with NO host header would result in empty Host. - // Use placeholder to avoid incorrect URL like "http:///" - builder.Append(UnknownHostName); - } - - if (request.PathBase.HasValue) - { - builder.Append(request.PathBase.Value); - } - - if (request.Path.HasValue) - { - builder.Append(request.Path.Value); - } - - if (request.QueryString.HasValue) - { - builder.Append(request.QueryString); - } - - return builder.ToString(); - } - } -} +// +// Copyright 2018, 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.Collector.AspNetCore.Implementation +{ + using System; + using System.Diagnostics; + using System.Linq; + using System.Text; + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Http.Features; + using OpenTelemetry.Trace; + + internal class HttpInListener : ListenerHandler + { + private static readonly string UnknownHostName = "UNKNOWN-HOST"; + private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext"); + private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext"); + private readonly PropertyFetcher beforeActionActionDescriptorFetcher = new PropertyFetcher("actionDescriptor"); + private readonly PropertyFetcher beforeActionAttributeRouteInfoFetcher = new PropertyFetcher("AttributeRouteInfo"); + private readonly PropertyFetcher beforeActionTemplateFetcher = new PropertyFetcher("Template"); + private readonly bool hostingSupportsW3C = false; + + public HttpInListener(string name, ITracer tracer, Func samplerFactory) + : base(name, tracer, samplerFactory) + { + this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3; + } + + public override void OnStartActivity(Activity activity, object payload) + { + const string EventNameSuffix = ".OnStartActivity"; + var context = this.startContextFetcher.Fetch(payload) as HttpContext; + + if (context == null) + { + CollectorEventSource.Log.NullPayload(nameof(HttpInListener) + EventNameSuffix); + return; + } + + var request = context.Request; + + SpanContext ctx = null; + if (!this.hostingSupportsW3C) + { + ctx = this.Tracer.TextFormat.Extract( + request, + (r, name) => r.Headers[name]); + } + + // see the spec https://github.com/open-telemetry/OpenTelemetry-specs/blob/master/trace/HTTP.md + var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; + + var spanBuilder = this.Tracer.SpanBuilder(path) + .SetSpanKind(SpanKind.Server) + .SetSampler(this.SamplerFactory(request)); + + if (this.hostingSupportsW3C) + { + spanBuilder.SetCreateChild(false); + } + else + { + spanBuilder.SetParent(ctx); + } + + var span = spanBuilder.StartSpan(); + this.Tracer.WithSpan(span); + + if (span.IsRecordingEvents) + { + // Note, route is missing at this stage. It will be available later + span.PutHttpHostAttribute(request.Host.Host, request.Host.Port ?? 80); + span.PutHttpMethodAttribute(request.Method); + span.PutHttpPathAttribute(path); + + var userAgent = request.Headers["User-Agent"].FirstOrDefault(); + span.PutHttpUserAgentAttribute(userAgent); + span.PutHttpRawUrlAttribute(GetUri(request)); + } + } + + public override void OnStopActivity(Activity activity, object payload) + { + const string EventNameSuffix = ".OnStopActivity"; + var span = this.Tracer.CurrentSpan; + + if (span == null || span == BlankSpan.Instance) + { + CollectorEventSource.Log.NullOrBlankSpan(nameof(HttpInListener) + EventNameSuffix); + return; + } + + if (!span.IsRecordingEvents) + { + span.End(); + return; + } + + if (!(this.stopContextFetcher.Fetch(payload) is HttpContext context)) + { + CollectorEventSource.Log.NullPayload(nameof(HttpInListener) + EventNameSuffix); + return; + } + + var response = context.Response; + + span.PutHttpStatusCode(response.StatusCode, response.HttpContext.Features.Get().ReasonPhrase); + span.End(); + } + + public override void OnCustom(string name, Activity activity, object payload) + { + if (name == "Microsoft.AspNetCore.Mvc.BeforeAction") + { + var span = this.Tracer.CurrentSpan; + + if (span == null) + { + CollectorEventSource.Log.NullOrBlankSpan(name); + return; + } + + if (span.IsRecordingEvents) + { + // See https://github.com/aspnet/Mvc/blob/2414db256f32a047770326d14d8b0e2afd49ba49/src/Microsoft.AspNetCore.Mvc.Core/MvcCoreDiagnosticSourceExtensions.cs#L36-L44 + // Reflection accessing: ActionDescriptor.AttributeRouteInfo.Template + // The reason to use reflection is to avoid a reference on MVC package. + // This package can be used with non-MVC apps and this logic simply wouldn't run. + // Taking reference on MVC will increase size of deployment for non-MVC apps. + var actionDescriptor = this.beforeActionActionDescriptorFetcher.Fetch(payload); + var attributeRouteInfo = this.beforeActionAttributeRouteInfoFetcher.Fetch(actionDescriptor); + var template = this.beforeActionTemplateFetcher.Fetch(attributeRouteInfo) as string; + + if (!string.IsNullOrEmpty(template)) + { + // override the span name that was previously set to the path part of URL. + span.UpdateName(template); + + span.PutHttpRouteAttribute(template); + } + + // TODO: Should we get values from RouteData? + // private readonly PropertyFetcher beforActionRouteDataFetcher = new PropertyFetcher("routeData"); + // var routeData = this.beforActionRouteDataFetcher.Fetch(payload) as RouteData; + } + } + } + + private static string GetUri(HttpRequest request) + { + var builder = new StringBuilder(); + + builder.Append(request.Scheme).Append("://"); + + if (request.Host.HasValue) + { + builder.Append(request.Host.Value); + } + else + { + // HTTP 1.0 request with NO host header would result in empty Host. + // Use placeholder to avoid incorrect URL like "http:///" + builder.Append(UnknownHostName); + } + + if (request.PathBase.HasValue) + { + builder.Append(request.PathBase.Value); + } + + if (request.Path.HasValue) + { + builder.Append(request.Path.Value); + } + + if (request.QueryString.HasValue) + { + builder.Append(request.QueryString); + } + + return builder.ToString(); + } + } +} diff --git a/src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs b/src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs index 0395ea5a14f..14bdd928a1e 100644 --- a/src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs +++ b/src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs @@ -18,9 +18,11 @@ namespace OpenTelemetry.Collector.AspNetCore { using System; using System.Collections.Generic; + using System.Reflection; using Microsoft.AspNetCore.Http; using OpenTelemetry.Collector.AspNetCore.Implementation; using OpenTelemetry.Trace; + using OpenTelemetry.Utils; /// /// Requests collector. @@ -33,16 +35,24 @@ public class RequestsCollector : IDisposable /// Initializes a new instance of the class. /// /// Configuration options for dependencies collector. - /// Tracer to record traced with. + /// TracerFactory which creates the Tracer to record traced with. /// Sampler to use to sample dependency calls. - public RequestsCollector(RequestsCollectorOptions options, ITracer tracer, ISampler sampler) + public RequestsCollector(RequestsCollectorOptions options, ITracerFactory tracerFactory, ISampler sampler) { + const string name = "Microsoft.AspNetCore"; this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber( - new Dictionary, ListenerHandler>>() + new Dictionary, ListenerHandler>>() { - { "Microsoft.AspNetCore", (t, s) => new HttpInListener(t, s) }, + { + name, (t, s) => + { + var version = typeof(RequestDelegate).Assembly.GetName().Version; + var tracer = tracerFactory.GetTracer(typeof(RequestsCollector).Namespace, "semver:" + version.ToString()); + return new HttpInListener(name, tracer, s); + } + }, }, - tracer, + tracerFactory, x => { ISampler s = null; diff --git a/src/OpenTelemetry.Collector.Dependencies/AzureSdkDiagnosticListener.cs b/src/OpenTelemetry.Collector.Dependencies/AzureSdkDiagnosticListener.cs index dd99f528535..50a02a863fd 100644 --- a/src/OpenTelemetry.Collector.Dependencies/AzureSdkDiagnosticListener.cs +++ b/src/OpenTelemetry.Collector.Dependencies/AzureSdkDiagnosticListener.cs @@ -26,14 +26,11 @@ namespace OpenTelemetry.Collector.Dependencies internal class AzureSdkDiagnosticListener : ListenerHandler { private static readonly PropertyFetcher LinksPropertyFetcher = new PropertyFetcher("Links"); - private readonly ITracer tracer; - private readonly ISampler sampler; public AzureSdkDiagnosticListener(string sourceName, ITracer tracer, ISampler sampler) : base(sourceName, tracer, null) { - this.tracer = tracer; this.sampler = sampler; } @@ -67,7 +64,7 @@ public override void OnStartActivity(Activity current, object valueValue) } } - var spanBuilder = this.tracer.SpanBuilder(operationName) + var spanBuilder = this.Tracer.SpanBuilder(operationName) .SetCreateChild(false) .SetSampler(this.sampler); @@ -84,23 +81,23 @@ public override void OnStartActivity(Activity current, object valueValue) span.Status = Status.Ok; - this.tracer.WithSpan(span); + this.Tracer.WithSpan(span); } public override void OnStopActivity(Activity current, object valueValue) { - var span = this.tracer.CurrentSpan; + var span = this.Tracer.CurrentSpan; foreach (var keyValuePair in current.Tags) { span.SetAttribute(keyValuePair.Key, keyValuePair.Value); } - this.tracer.CurrentSpan.End(); + this.Tracer.CurrentSpan.End(); } public override void OnException(Activity current, object valueValue) { - var span = this.tracer.CurrentSpan; + var span = this.Tracer.CurrentSpan; span.Status = Status.Unknown.WithDescription(valueValue?.ToString()); } diff --git a/src/OpenTelemetry.Collector.Dependencies/DependenciesCollector.cs b/src/OpenTelemetry.Collector.Dependencies/DependenciesCollector.cs index d7e6e3d51dc..2961f3667e1 100644 --- a/src/OpenTelemetry.Collector.Dependencies/DependenciesCollector.cs +++ b/src/OpenTelemetry.Collector.Dependencies/DependenciesCollector.cs @@ -19,8 +19,10 @@ namespace OpenTelemetry.Collector.Dependencies using System; using System.Collections.Generic; using System.Net.Http; + using System.Reflection; using OpenTelemetry.Collector.Dependencies.Implementation; using OpenTelemetry.Trace; + using OpenTelemetry.Utils; /// /// Dependencies collector. @@ -33,18 +35,36 @@ public class DependenciesCollector : IDisposable /// Initializes a new instance of the class. /// /// Configuration options for dependencies collector. - /// Tracer to record traced with. - /// Sampler to use to sample dependnecy calls. - public DependenciesCollector(DependenciesCollectorOptions options, ITracer tracer, ISampler sampler) + /// TracerFactory to create a Tracer to record traced with. + /// Sampler to use to sample dependency calls. + public DependenciesCollector(DependenciesCollectorOptions options, ITracerFactory tracerFactory, ISampler sampler) { this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber( - new Dictionary, ListenerHandler>>() + new Dictionary, ListenerHandler>>() { - { "HttpHandlerDiagnosticListener", (t, s) => new HttpHandlerDiagnosticListener(t, s) }, - { "Azure.Clients", (t, s) => new AzureSdkDiagnosticListener("Azure.Clients", t, sampler) }, - { "Azure.Pipeline", (t, s) => new AzureSdkDiagnosticListener("Azure.Pipeline", t, sampler) }, + { + "HttpHandlerDiagnosticListener", (tf, s) => + { + var tracer = tracerFactory.GetTracer("OpenTelemetry.Collector.Dependencies.HttpHandlerDiagnosticListener"); + return new HttpHandlerDiagnosticListener(tracer, s); + } + }, + { + "Azure.Clients", (tf, s) => + { + var tracer = tracerFactory.GetTracer("OpenTelemetry.Collector.Dependencies.Azure.Clients"); + return new AzureSdkDiagnosticListener("Azure.Clients", tracer, sampler); + } + }, + { + "Azure.Pipeline", (tf, s) => + { + var tracer = tracerFactory.GetTracer("OpenTelemetry.Collector.Dependencies.Azure.Pipeline"); + return new AzureSdkDiagnosticListener("Azure.Pipeline", tracer, sampler); + } + }, }, - tracer, + tracerFactory, x => { ISampler s = null; diff --git a/src/OpenTelemetry.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs b/src/OpenTelemetry.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs index 238073d3e19..cf741f55110 100644 --- a/src/OpenTelemetry.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs +++ b/src/OpenTelemetry.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs @@ -21,7 +21,7 @@ namespace OpenTelemetry.Collector.Dependencies.Implementation using System.Linq; using System.Net; using System.Net.Http; - using System.Reflection; + using System.Reflection; using System.Runtime.Versioning; using System.Threading.Tasks; using OpenTelemetry.Trace; @@ -55,7 +55,7 @@ public override void OnStartActivity(Activity activity, object payload) { const string EventNameSuffix = ".OnStartActivity"; if (!(this.startRequestFetcher.Fetch(payload) is HttpRequestMessage request)) - { + { CollectorEventSource.Log.NullPayload(nameof(HttpHandlerDiagnosticListener) + EventNameSuffix); return; } @@ -91,7 +91,7 @@ public override void OnStartActivity(Activity activity, object payload) } public override void OnStopActivity(Activity activity, object payload) - { + { const string EventNameSuffix = ".OnStopActivity"; var span = this.Tracer.CurrentSpan; diff --git a/src/OpenTelemetry/Collector/DiagnosticSourceSubscriber.cs b/src/OpenTelemetry/Collector/DiagnosticSourceSubscriber.cs index 03c99a00337..ad72c1b0609 100644 --- a/src/OpenTelemetry/Collector/DiagnosticSourceSubscriber.cs +++ b/src/OpenTelemetry/Collector/DiagnosticSourceSubscriber.cs @@ -25,18 +25,18 @@ namespace OpenTelemetry.Collector public class DiagnosticSourceSubscriber : IDisposable, IObserver { - private readonly Dictionary, ListenerHandler>> handlers; - private readonly ITracer tracer; + private readonly Dictionary, ListenerHandler>> handlers; + private readonly ITracerFactory tracerFactory; private readonly Func sampler; private ConcurrentDictionary> subscriptions; private long disposed; private IDisposable subscription; - public DiagnosticSourceSubscriber(Dictionary, ListenerHandler>> handlers, ITracer tracer, Func sampler) + public DiagnosticSourceSubscriber(Dictionary, ListenerHandler>> handlers, ITracerFactory tracerFactory, Func sampler) { - this.subscriptions = new ConcurrentDictionary>(); - this.handlers = handlers ?? throw new ArgumentNullException(nameof(handlers)); - this.tracer = tracer ?? throw new ArgumentNullException(nameof(tracer)); + this.subscriptions = new ConcurrentDictionary>(); + this.handlers = handlers ?? throw new ArgumentNullException(nameof(handlers)); + this.tracerFactory = tracerFactory ?? throw new ArgumentNullException(nameof(tracerFactory)); this.sampler = sampler ?? throw new ArgumentNullException(nameof(sampler)); } @@ -56,7 +56,7 @@ public void OnNext(DiagnosticListener value) { this.subscriptions.GetOrAdd(value.Name, name => { - var dl = new DiagnosticSourceListener(this.handlers[value.Name](this.tracer, this.sampler)); + var dl = new DiagnosticSourceListener(this.handlers[value.Name](this.tracerFactory, this.sampler)); dl.Subscription = value.Subscribe(dl); return dl; }); @@ -75,8 +75,8 @@ public void OnError(Exception error) public void Dispose() { if (Interlocked.CompareExchange(ref this.disposed, 1, 0) == 1) - { - return; + { + return; } var subsCopy = this.subscriptions; diff --git a/src/OpenTelemetry/Trace/Span.cs b/src/OpenTelemetry/Trace/Span.cs index b627b691b87..2cd48269b9d 100644 --- a/src/OpenTelemetry/Trace/Span.cs +++ b/src/OpenTelemetry/Trace/Span.cs @@ -20,6 +20,7 @@ namespace OpenTelemetry.Trace using System.Collections.Generic; using System.Diagnostics; using System.Linq; + using OpenTelemetry.Resources; using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Internal; @@ -48,7 +49,8 @@ internal Span( TraceConfig traceConfig, SpanProcessor spanProcessor, DateTimeOffset startTimestamp, - bool ownsActivity) + bool ownsActivity, + Resource libraryResource) { this.Activity = activity; this.spanContext = new Lazy(() => new SpanContext( @@ -63,6 +65,7 @@ internal Span( this.OwnsActivity = ownsActivity; this.IsRecordingEvents = this.Activity.Recorded; this.startTimestamp = startTimestamp; + this.LibraryResource = libraryResource; if (this.IsRecordingEvents) { @@ -140,12 +143,17 @@ public Status Status public DateTimeOffset EndTimestamp => this.endTimestamp; /// - /// Gets or sets span kind. + /// Gets the span kind. /// - public SpanKind? Kind { get; set; } + public SpanKind? Kind { get; } + /// + /// Gets the "Library Resource" (name + version) associated with the Tracer that produced this span. + /// + public Resource LibraryResource { get; } + internal bool OwnsActivity { get; } - + private Status StatusWithDefault => this.status.IsValid ? this.status : Status.Ok; /// diff --git a/src/OpenTelemetry/Trace/SpanBuilder.cs b/src/OpenTelemetry/Trace/SpanBuilder.cs index d79f67cb1d0..7aac82c26ce 100644 --- a/src/OpenTelemetry/Trace/SpanBuilder.cs +++ b/src/OpenTelemetry/Trace/SpanBuilder.cs @@ -20,6 +20,7 @@ namespace OpenTelemetry.Trace using System.Collections.Generic; using System.Diagnostics; using OpenTelemetry.Context.Propagation; + using OpenTelemetry.Resources; using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Internal; @@ -41,12 +42,14 @@ public class SpanBuilder : ISpanBuilder private List links; private bool recordEvents; private DateTimeOffset startTimestamp; + private Resource libraryResource; - internal SpanBuilder(string name, SpanProcessor spanProcessor, TraceConfig traceConfig) + internal SpanBuilder(string name, SpanProcessor spanProcessor, TraceConfig traceConfig, Resource libraryResource) { this.name = name ?? throw new ArgumentNullException(nameof(name)); this.spanProcessor = spanProcessor ?? throw new ArgumentNullException(nameof(spanProcessor)); this.traceConfig = traceConfig ?? throw new ArgumentNullException(nameof(traceConfig)); + this.libraryResource = libraryResource ?? throw new ArgumentNullException(nameof(libraryResource)); } private enum ContextSource @@ -250,7 +253,8 @@ public ISpan StartSpan() this.traceConfig, this.spanProcessor, this.startTimestamp, - ownsActivity: this.contextSource != ContextSource.Activity); + ownsActivity: this.contextSource != ContextSource.Activity, + this.libraryResource); if (activityForSpan.OperationName != this.name) { diff --git a/src/OpenTelemetry/Trace/Tracer.cs b/src/OpenTelemetry/Trace/Tracer.cs index d1af6d75e57..1c4c6c38fcf 100644 --- a/src/OpenTelemetry/Trace/Tracer.cs +++ b/src/OpenTelemetry/Trace/Tracer.cs @@ -20,6 +20,7 @@ namespace OpenTelemetry.Trace using System.Diagnostics; using OpenTelemetry.Context; using OpenTelemetry.Context.Propagation; + using OpenTelemetry.Resources; using OpenTelemetry.Trace.Config; using OpenTelemetry.Trace.Export; using OpenTelemetry.Trace.Internal; @@ -40,12 +41,10 @@ static Tracer() /// /// Span processor. /// Trace configuration. - public Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig) + /// Resource describing the instrumentation library. + public Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig, Resource libraryResource) + : this(spanProcessor, traceConfig, new BinaryFormat(), new TraceContextFormat(), libraryResource) { - this.spanProcessor = spanProcessor ?? throw new ArgumentNullException(nameof(spanProcessor)); - this.ActiveTraceConfig = traceConfig ?? throw new ArgumentNullException(nameof(traceConfig)); - this.BinaryFormat = new BinaryFormat(); - this.TextFormat = new TraceContextFormat(); } /// @@ -55,14 +54,18 @@ public Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig) /// Trace configuration. /// Binary format context propagator. /// Text format context propagator. - public Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig, IBinaryFormat binaryFormat, ITextFormat textFormat) + /// Resource describing the instrumentation library. + internal Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig, IBinaryFormat binaryFormat, ITextFormat textFormat, Resource libraryResource) { this.spanProcessor = spanProcessor ?? throw new ArgumentNullException(nameof(spanProcessor)); this.ActiveTraceConfig = traceConfig ?? throw new ArgumentNullException(nameof(traceConfig)); this.BinaryFormat = binaryFormat ?? throw new ArgumentNullException(nameof(binaryFormat)); this.TextFormat = textFormat ?? throw new ArgumentNullException(nameof(textFormat)); + this.LibraryResource = libraryResource ?? throw new ArgumentNullException(nameof(libraryResource)); } + public Resource LibraryResource { get; } + /// public ISpan CurrentSpan => CurrentSpanUtils.CurrentSpan; @@ -77,7 +80,7 @@ public Tracer(SpanProcessor spanProcessor, TraceConfig traceConfig, IBinaryForma /// public ISpanBuilder SpanBuilder(string spanName) { - return new SpanBuilder(spanName, this.spanProcessor, this.ActiveTraceConfig); + return new SpanBuilder(spanName, this.spanProcessor, this.ActiveTraceConfig, this.LibraryResource); } public IDisposable WithSpan(ISpan span) diff --git a/src/OpenTelemetry/Trace/TracerFactory.cs b/src/OpenTelemetry/Trace/TracerFactory.cs new file mode 100644 index 00000000000..4438af7caf5 --- /dev/null +++ b/src/OpenTelemetry/Trace/TracerFactory.cs @@ -0,0 +1,90 @@ +// +// Copyright 2018, 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.Trace +{ + using System.Collections.Generic; + using OpenTelemetry.Context.Propagation; + using OpenTelemetry.Resources; + using OpenTelemetry.Trace.Config; + using OpenTelemetry.Trace.Export; + + /// + public sealed class TracerFactory : ITracerFactory + { + private readonly object lck = new object(); + private readonly SpanProcessor spanProcessor; + private readonly TraceConfig traceConfig; + private readonly ITextFormat textFormat; + private readonly IBinaryFormat binaryFormat; + private readonly Tracer defaultTracer; + private readonly Dictionary tracerRegistry = new Dictionary(); + + public TracerFactory(SpanProcessor spanProcessor = null, TraceConfig traceConfig = null, ITextFormat textFormat = null, IBinaryFormat binaryFormat = null) + { + this.spanProcessor = spanProcessor ?? Tracing.SpanProcessor; + this.traceConfig = traceConfig ?? Tracing.TraceConfig; + this.textFormat = textFormat ?? new TraceContextFormat(); + this.binaryFormat = binaryFormat ?? new BinaryFormat(); + this.defaultTracer = new Tracer(this.spanProcessor, this.traceConfig, this.binaryFormat, this.textFormat, Resource.Empty); + } + + /// + public override ITracer GetTracer(string name, string version = null) + { + if (string.IsNullOrEmpty(name)) + { + return this.defaultTracer; + } + + lock (this.lck) + { + var key = new TracerRegistryKey(name, version); + if (!this.tracerRegistry.TryGetValue(key, out var tracer)) + { + var labels = CreateLibraryResourceLabels(name, version); + tracer = new Tracer(this.spanProcessor, this.traceConfig, this.binaryFormat, this.textFormat, Resource.Create(labels)); + this.tracerRegistry.Add(key, tracer); + } + + return tracer; + } + } + + private static Dictionary CreateLibraryResourceLabels(string name, string version) + { + var labels = new Dictionary { { "name", name } }; + if (!string.IsNullOrEmpty(version)) + { + labels.Add("version", version); + } + + return labels; + } + + private struct TracerRegistryKey + { + private string name; + private string version; + + internal TracerRegistryKey(string name, string version) + { + this.name = name; + this.version = version; + } + } + } +} diff --git a/src/OpenTelemetry/Trace/Tracing.cs b/src/OpenTelemetry/Trace/Tracing.cs index a57d44d9da1..9e35dff90a3 100644 --- a/src/OpenTelemetry/Trace/Tracing.cs +++ b/src/OpenTelemetry/Trace/Tracing.cs @@ -22,33 +22,30 @@ namespace OpenTelemetry.Trace /// /// Class that manages a global instance of the . /// - public sealed class Tracing + public static class Tracing { - private static Tracing tracingValue = new Tracing(); - private static Tracer tracer; + private static TracerFactory tracerFactory; - internal Tracing() + static Tracing() { TraceConfig = TraceConfig.Default; - SpanProcessor = new BatchingSpanProcessor(new NoopSpanExporter()); - - tracer = new Tracer(SpanProcessor, TraceConfig); + tracerFactory = new TracerFactory(SpanProcessor, TraceConfig); } /// /// Gets the tracer to record spans. /// - public static ITracer Tracer => (ITracer)tracer; - + public static TracerFactory TracerFactory => tracerFactory; + /// /// Gets the exporter to use to upload spans. /// - public static SpanProcessor SpanProcessor { get; private set; } + public static SpanProcessor SpanProcessor { get; } /// /// Gets the trace config. /// - public static TraceConfig TraceConfig { get; private set; } + public static TraceConfig TraceConfig { get; } } } diff --git a/test/OpenTelemetry.Collector.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Collector.AspNetCore.Tests/BasicTests.cs index 6e5b1ee8c73..64a69d8bb90 100644 --- a/test/OpenTelemetry.Collector.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Collector.AspNetCore.Tests/BasicTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Collector.AspNetCore.Tests { using Xunit; @@ -47,10 +49,10 @@ public BasicTests(WebApplicationFactory factory) public async Task SuccessfulTemplateControllerCallGeneratesASpan() { var panProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(panProcessor.Object, TraceConfig.Default); + var tracerFactory = new TracerFactory(panProcessor.Object); void ConfigureTestServices(IServiceCollection services) => - services.AddSingleton(tracer); + services.AddSingleton(tracerFactory); // Arrange using (var client = this.factory @@ -103,16 +105,14 @@ public async Task SuccessfulTemplateControllerCallUsesParentContext() Tracestate.Empty )); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default, new BinaryFormat(), tf.Object); - + var tracerFactory = new TracerFactory(spanProcessor.Object, null, tf.Object); + // Arrange using (var client = this.factory .WithWebHostBuilder(builder => builder.ConfigureTestServices((services) => { - services.AddSingleton(tracer); - services.AddSingleton(tf.Object); - services.AddSingleton(new BinaryFormat()); + services.AddSingleton(tracerFactory); })) .CreateClient()) { diff --git a/test/OpenTelemetry.Collector.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs b/test/OpenTelemetry.Collector.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs index 0e48b0ea541..f4fb233d227 100644 --- a/test/OpenTelemetry.Collector.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs +++ b/test/OpenTelemetry.Collector.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Collector.AspNetCore.Tests { using Xunit; @@ -55,7 +57,7 @@ public override async Task ProcessAsync(HttpContext context) public async Task SuccessfulTemplateControllerCallGeneratesASpan() { var spanProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default); + var tracerFactory = new TracerFactory(spanProcessor.Object); // Arrange using (var client = this.factory @@ -63,7 +65,7 @@ public async Task SuccessfulTemplateControllerCallGeneratesASpan() builder.ConfigureTestServices((IServiceCollection services) => { services.AddSingleton(new TestCallbackMiddlewareImpl()); - services.AddSingleton(tracer); + services.AddSingleton(tracerFactory); })) .CreateClient()) { diff --git a/test/OpenTelemetry.Collector.Dependencies.Tests/BasicTests.cs b/test/OpenTelemetry.Collector.Dependencies.Tests/BasicTests.cs index b8e67e13710..04fc3dc2dae 100644 --- a/test/OpenTelemetry.Collector.Dependencies.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Collector.Dependencies.Tests/BasicTests.cs @@ -15,6 +15,7 @@ // using System.Linq; +using OpenTelemetry.Resources; namespace OpenTelemetry.Collector.Dependencies.Tests { @@ -51,7 +52,7 @@ public HttpClientTests() public async Task HttpDependenciesCollectorInjectsHeadersAsync() { var spanProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default); + var tracerFactory = new TracerFactory(spanProcessor.Object); var request = new HttpRequestMessage { @@ -64,7 +65,7 @@ public async Task HttpDependenciesCollectorInjectsHeadersAsync() .Start(); parent.TraceStateString = "k1=v1,k2=v2"; - using (new DependenciesCollector(new DependenciesCollectorOptions(), tracer, Samplers.AlwaysSample)) + using (new DependenciesCollector(new DependenciesCollectorOptions(), tracerFactory, Samplers.AlwaysSample)) using (var c = new HttpClient()) { await c.SendAsync(request); @@ -91,7 +92,7 @@ public async Task HttpDependenciesCollectorInjectsHeadersAsync() public async Task HttpDependenciesCollectorBacksOffIfAlreadyInstrumented() { var spanProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default); + var tracerFactory = new TracerFactory(spanProcessor.Object); var request = new HttpRequestMessage { @@ -101,7 +102,7 @@ public async Task HttpDependenciesCollectorBacksOffIfAlreadyInstrumented() request.Headers.Add("traceparent", "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01"); - using (new DependenciesCollector(new DependenciesCollectorOptions(), tracer, Samplers.AlwaysSample)) + using (new DependenciesCollector(new DependenciesCollectorOptions(), tracerFactory, Samplers.AlwaysSample)) using (var c = new HttpClient()) { await c.SendAsync(request); diff --git a/test/OpenTelemetry.Collector.Dependencies.Tests/HttpClientTests.cs b/test/OpenTelemetry.Collector.Dependencies.Tests/HttpClientTests.cs index 424e57243e7..4a6538a0961 100644 --- a/test/OpenTelemetry.Collector.Dependencies.Tests/HttpClientTests.cs +++ b/test/OpenTelemetry.Collector.Dependencies.Tests/HttpClientTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Collector.Dependencies.Tests { using Moq; @@ -93,12 +95,12 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpOutTestCase tc) out var port); var spanProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default); + var tracerFactory = new TracerFactory(spanProcessor.Object); tc.url = NormalizeValues(tc.url, host, port); using (serverLifeTime) { - using (var dc = new DependenciesCollector(new DependenciesCollectorOptions(), tracer, Samplers.AlwaysSample)) + using (var dc = new DependenciesCollector(new DependenciesCollectorOptions(), tracerFactory, Samplers.AlwaysSample)) { try diff --git a/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToSpanConverterTests.cs b/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToSpanConverterTests.cs index 66b905a92a8..824f341b163 100644 --- a/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToSpanConverterTests.cs +++ b/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToSpanConverterTests.cs @@ -15,6 +15,8 @@ // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Collector.StackExchangeRedis.Implementation { using System; @@ -32,7 +34,7 @@ public class RedisProfilerEntryToSpanConverterTests public RedisProfilerEntryToSpanConverterTests() { - tracer = new Tracer(new SimpleSpanProcessor(new NoopSpanExporter()), TraceConfig.Default); + tracer = new Tracer(new SimpleSpanProcessor(new NoopSpanExporter()), TraceConfig.Default, Resource.Empty); } [Fact] diff --git a/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/StackExchangeRedisCallsCollectorTests.cs b/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/StackExchangeRedisCallsCollectorTests.cs index 330d3a6f6cf..efca37e49d3 100644 --- a/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/StackExchangeRedisCallsCollectorTests.cs +++ b/test/OpenTelemetry.Collector.StackExchangeRedis.Tests/StackExchangeRedisCallsCollectorTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Collector.StackExchangeRedis { using Moq; @@ -30,7 +32,7 @@ public class StackExchangeRedisCallsCollectorTests public async void ProfilerSessionUsesTheSameDefault() { var spanProcessor = new Mock(new NoopSpanExporter()); - var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default); + var tracer = new Tracer(spanProcessor.Object, TraceConfig.Default, Resource.Empty); using (var collector = new StackExchangeRedisCallsCollector(tracer)) { diff --git a/test/OpenTelemetry.Exporter.ApplicationInsights.Tests/Implementation/ApplicationInsightsTraceExporterTests.cs b/test/OpenTelemetry.Exporter.ApplicationInsights.Tests/Implementation/ApplicationInsightsTraceExporterTests.cs index f4deabea2ff..e36fdb13b5d 100644 --- a/test/OpenTelemetry.Exporter.ApplicationInsights.Tests/Implementation/ApplicationInsightsTraceExporterTests.cs +++ b/test/OpenTelemetry.Exporter.ApplicationInsights.Tests/Implementation/ApplicationInsightsTraceExporterTests.cs @@ -1648,7 +1648,7 @@ internal static Span CreateTestSpan(string name, SpanKind kind, Status status) { - var spanBuilder = Tracing.Tracer + var spanBuilder = Tracing.TracerFactory.GetTracer("") .SpanBuilder(name); if (parentSpanId != default) diff --git a/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerSpanConverterTest.cs b/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerSpanConverterTest.cs index 875c5dad6c0..38df4fb16b8 100644 --- a/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerSpanConverterTest.cs +++ b/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerSpanConverterTest.cs @@ -356,7 +356,7 @@ internal static Span CreateTestSpan(bool setAttributes = true, ActivityTraceFlags.Recorded, Tracestate.Empty)); - var span = (Span)Tracing.Tracer + var span = (Span)Tracing.TracerFactory.GetTracer("") .SpanBuilder("Name") .SetParent(new SpanContext(traceId, parentSpanId, ActivityTraceFlags.Recorded, Tracestate.Empty)) .SetSpanKind(SpanKind.Client) diff --git a/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerThriftIntegrationTest.cs b/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerThriftIntegrationTest.cs index de538b61d10..eb0235a2b56 100644 --- a/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerThriftIntegrationTest.cs +++ b/test/OpenTelemetry.Exporter.Jaeger.Tests/Implementation/JaegerThriftIntegrationTest.cs @@ -100,7 +100,7 @@ private Span CreateTestSpan() ActivityTraceFlags.Recorded, Tracestate.Empty)); - var span = (Span)Tracing.Tracer + var span = (Span)Tracing.TracerFactory.GetTracer("") .SpanBuilder("Name") .SetParent(new SpanContext(traceId, parentSpanId, ActivityTraceFlags.Recorded, Tracestate.Empty)) .SetSpanKind(SpanKind.Client) diff --git a/test/OpenTelemetry.Exporter.LightStep.Tests/LightStepSpanConverterTest.cs b/test/OpenTelemetry.Exporter.LightStep.Tests/LightStepSpanConverterTest.cs index 2a9f34ba7c6..f84dce3f45f 100644 --- a/test/OpenTelemetry.Exporter.LightStep.Tests/LightStepSpanConverterTest.cs +++ b/test/OpenTelemetry.Exporter.LightStep.Tests/LightStepSpanConverterTest.cs @@ -66,7 +66,7 @@ public void AllPropertiesShouldTranslate() var link = new Link(new SpanContext( traceId, linkedSpanId, ActivityTraceFlags.Recorded, Tracestate.Empty)); - var span = (Span)Tracing.Tracer + var span = (Span)Tracing.TracerFactory.GetTracer("") .SpanBuilder("Test") .SetParent(new SpanContext(traceId, parentId, ActivityTraceFlags.Recorded, Tracestate.Empty)) .SetSpanKind(SpanKind.Client) diff --git a/test/OpenTelemetry.Tests/Impl/Trace/CurrentSpanUtilsTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/CurrentSpanUtilsTest.cs index 17d377f6f44..eb1344e4155 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/CurrentSpanUtilsTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/CurrentSpanUtilsTest.cs @@ -15,6 +15,7 @@ // using OpenTelemetry.Trace.Internal; +using OpenTelemetry.Resources; namespace OpenTelemetry.Trace.Test { @@ -68,7 +69,8 @@ public void WithSpan_CloseDetaches(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); Assert.Same(BlankSpan.Instance, CurrentSpanUtils.CurrentSpan); using (CurrentSpanUtils.WithSpan(span, stopSpan)) @@ -102,7 +104,8 @@ public void WithSpan_NotOwningActivity(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - false); + false, + Resource.Empty); Assert.Same(BlankSpan.Instance, CurrentSpanUtils.CurrentSpan); using (CurrentSpanUtils.WithSpan(span, stopSpan)) @@ -135,7 +138,9 @@ public void WithSpan_NoopOnBrokenScope(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); + var parentScope = CurrentSpanUtils.WithSpan(parentSpan, stopSpan); var childActivity = new Activity("child").Start(); @@ -146,7 +151,8 @@ public void WithSpan_NoopOnBrokenScope(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); Assert.Same(BlankSpan.Instance, CurrentSpanUtils.CurrentSpan); @@ -180,7 +186,8 @@ public void WithSpan_RestoresParentScope(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); var parentScope = CurrentSpanUtils.WithSpan(parentSpan, stopSpan); @@ -192,7 +199,8 @@ public void WithSpan_RestoresParentScope(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); Assert.Same(BlankSpan.Instance, CurrentSpanUtils.CurrentSpan); @@ -218,7 +226,8 @@ public void WithSpan_SameActivityCreateScopeTwice() TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); using(CurrentSpanUtils.WithSpan(span, true)) using(CurrentSpanUtils.WithSpan(span, true)) @@ -242,7 +251,8 @@ public void WithSpan_NullActivity() TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); activity.Stop(); @@ -276,7 +286,8 @@ public void WithSpan_WrongActivity(bool stopSpan, bool recordEvents) TraceConfig.Default, spanProcessor, default, - true); + true, + Resource.Empty); Assert.Same(BlankSpan.Instance, CurrentSpanUtils.CurrentSpan); using (CurrentSpanUtils.WithSpan(span, stopSpan)) diff --git a/test/OpenTelemetry.Tests/Impl/Trace/Export/BatchingSpanProcessorTests.cs b/test/OpenTelemetry.Tests/Impl/Trace/Export/BatchingSpanProcessorTests.cs index ae4c4f042a2..053b2ce41ae 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/Export/BatchingSpanProcessorTests.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/Export/BatchingSpanProcessorTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Trace.Export.Test { using System; @@ -57,7 +59,8 @@ private Span CreateSampledEndedSpan(string spanName) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - default); + default, + Resource.Empty); span.End(); return span; } @@ -75,7 +78,8 @@ private Span CreateNotSampledEndedSpan(string spanName) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); span.End(); return span; } @@ -245,7 +249,8 @@ public void ProcessorDoesNotBlockOnExporter() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - default); + default, + Resource.Empty); // does not block var sw = Stopwatch.StartNew(); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs b/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs index 13bd927daf9..b509337e782 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/Export/SimpleSpanProcessorTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Trace.Export.Test { using System; @@ -53,7 +55,8 @@ private Span CreateSampledEndedSpan(string spanName) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - default); + default, + Resource.Empty); span.End(); return span; } @@ -71,7 +74,8 @@ private Span CreateNotSampledEndedSpan(string spanName) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); span.End(); return span; } @@ -100,7 +104,8 @@ public void ThrowsInExporter() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - default); + default, + Resource.Empty); // does not throw span.End(); @@ -125,7 +130,8 @@ public void ProcessorDoesNotBlockOnExporter() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - default); + default, + Resource.Empty); // does not block var sw = Stopwatch.StartNew(); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs index 365652f7d15..f798a5c5514 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/SpanBuilderTest.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Trace.Test { using System; @@ -38,13 +40,13 @@ public class SpanBuilderTest : IDisposable private readonly ITracer tracer; public SpanBuilderTest() { - tracer = new Tracer(spanProcessor, alwaysSampleTraceConfig); + tracer = new Tracer(spanProcessor, alwaysSampleTraceConfig, Resource.Empty); } [Fact] public void StartSpanNullParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .StartSpan(); @@ -71,7 +73,7 @@ public void StartSpanLastParentWins1() ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var span = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetNoParent() .SetParent(spanContext) .StartSpan(); @@ -90,7 +92,7 @@ public void StartSpanLastParentWins2() ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var span = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent(spanContext) .SetNoParent() .StartSpan(); @@ -108,10 +110,10 @@ public void StartSpanLastParentWins3() ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var rootSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .StartSpan(); - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent(spanContext) .SetParent(rootSpan) .StartSpan(); @@ -129,10 +131,10 @@ public void StartSpanLastParentWins4() ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var rootSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .StartSpan(); - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent(rootSpan) .SetParent(spanContext) .StartSpan(); @@ -154,7 +156,7 @@ public void StartSpanLastParentWins5() .SetIdFormat(ActivityIdFormat.W3C) .Start(); - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent(spanContext) .SetParent(activity) .StartSpan(); @@ -176,7 +178,7 @@ public void StartSpanLastParentWins6() .SetIdFormat(ActivityIdFormat.W3C) .Start(); - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent(spanContext) .SetCreateChild(false) .StartSpan(); @@ -198,7 +200,7 @@ public void StartSpanLastParentWins7() .SetIdFormat(ActivityIdFormat.W3C) .Start(); - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetCreateChild(false) .SetParent(spanContext) .StartSpan(); @@ -211,7 +213,7 @@ public void StartSpanLastParentWins7() [Fact] public void StartSpanNullParentWithRecordEvents() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetRecordEvents(true) @@ -227,7 +229,7 @@ public void StartSpanNullParentWithRecordEvents() public void StartSpanWithStartTimestamp() { var timestamp = DateTime.UtcNow.AddSeconds(-100); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.AlwaysSample) .SetStartTimestamp(timestamp) @@ -240,7 +242,7 @@ public void StartSpanWithStartTimestamp() public void StartSpanWithImplicitTimestamp() { var timestamp = PreciseTimestamp.GetUtcNow(); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.AlwaysSample) .StartSpan(); @@ -251,7 +253,7 @@ public void StartSpanWithImplicitTimestamp() [Fact] public void StartSpanNullParentNoRecordOptions() { - var span = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetNoParent() @@ -264,7 +266,7 @@ public void StartSpanNullParentNoRecordOptions() [Fact] public void StartChildSpan() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .StartSpan(); @@ -273,7 +275,7 @@ public void StartChildSpan() Assert.True(rootSpan.IsRecordingEvents); Assert.True((rootSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0); - var childSpan = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(rootSpan) .StartSpan(); @@ -291,7 +293,7 @@ public void StartSpanInScopeOfCurrentActivity() .Start(); parentActivity.TraceStateString = "k1=v1,k2=v2"; - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .StartSpan(); @@ -314,7 +316,7 @@ public void StartSpanInScopeOfCurrentActivityRecorded() .Start(); parentActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded; - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .StartSpan(); @@ -331,7 +333,7 @@ public void StartSpanInScopeOfCurrentActivityNoParent() var parentActivity = new Activity(SpanName).Start(); parentActivity.TraceStateString = "k1=v1,k2=v2"; - var childSpan = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .StartSpan(); @@ -357,7 +359,7 @@ public void StartSpanFromExplicitActivity() parentActivity.TraceStateString = "k1=v1,k2=v2"; parentActivity.Stop(); - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(parentActivity) .StartSpan(); @@ -384,7 +386,7 @@ public void StartSpanFromExplicitRecordedActivity() parentActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded; parentActivity.Stop(); - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(parentActivity) .StartSpan(); @@ -403,7 +405,7 @@ public void StartSpanFromCurrentActivity() .Start(); activity.TraceStateString = "k1=v1,k2=v2"; - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetCreateChild(false) .StartSpan(); @@ -428,7 +430,7 @@ public void StartSpanFromCurrentRecordedActivity() .Start(); activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded; - var span = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetCreateChild(false) .StartSpan(); @@ -444,7 +446,7 @@ public void StartSpanFromCurrentRecordedActivity() [Fact] public void StartSpan_ExplicitNoParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .StartSpan(); @@ -465,7 +467,7 @@ public void StartSpan_ExplicitNoParent() [Fact] public void StartSpan_NoParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .StartSpan(); @@ -478,7 +480,7 @@ public void StartSpan_NoParent() [Fact] public void StartSpan_BlankSpanParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(BlankSpan.Instance) .StartSpan(); @@ -492,7 +494,7 @@ public void StartSpan_BlankSpanParent() [Fact] public void StartSpan_BlankSpanContextParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(SpanContext.Blank) .StartSpan(); @@ -508,7 +510,7 @@ public void StartSpan_BlankSpanContextParent() [Fact] public void StartSpan_CurrentSpanParent() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetParent( new SpanContext( ActivityTraceId.CreateRandom(), @@ -518,7 +520,7 @@ public void StartSpan_CurrentSpanParent() .StartSpan(); using (tracer.WithSpan(rootSpan)) { - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .StartSpan(); Assert.True(childSpan.Context.IsValid); @@ -531,11 +533,11 @@ public void StartSpan_CurrentSpanParent() [Fact] public void StartSpan_NoParentInScopeOfCurrentSpan() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .StartSpan(); using (tracer.WithSpan(rootSpan)) { - var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = (Span)new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetNoParent() .StartSpan(); @@ -548,7 +550,7 @@ public void StartSpan_NoParentInScopeOfCurrentSpan() [Fact] public void StartSpanInvalidParent() { - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(SpanContext.Blank) .StartSpan(); @@ -569,7 +571,7 @@ public void StartRemoteSpan() ActivityTraceFlags.None, Tracestate.Builder.Set("k1", "v1").Build()); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(spanContext) .SetRecordEvents(true) @@ -592,7 +594,7 @@ public void StartSpan_WithLink() ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty)); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .AddLink(link) .StartSpan(); @@ -614,7 +616,7 @@ public void StartSpan_WithLinkFromActivity() var contextLink = new SpanContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .AddLink(contextLink) .StartSpan(); @@ -641,7 +643,7 @@ public void StartSpan_WithLinkFromSpanContextAndAttributes() ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .AddLink(linkContext, new Dictionary { ["k"] = "v", }) .StartSpan(); @@ -668,7 +670,7 @@ public void StartSpan_WithLinkFromSpanContext() ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, Tracestate.Empty); - var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var span = (Span) new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .AddLink(linkContext) .StartSpan(); @@ -687,7 +689,7 @@ public void StartSpan_WithLinkFromSpanContext() public void StartRootSpan_WithSpecifiedSampler() { // Apply given sampler before default sampler for root spans. - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .SetSampler(Samplers.NeverSample) @@ -701,7 +703,7 @@ public void StartRootSpan_WithSpecifiedSampler() public void StartRootSpan_WithoutSpecifiedSampler() { // Apply default sampler (always true in the tests) for root spans. - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetNoParent() .StartSpan(); @@ -713,7 +715,7 @@ public void StartRootSpan_WithoutSpecifiedSampler() [Fact] public void StartRemoteChildSpan_WithSpecifiedSampler() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.AlwaysSample) .SetNoParent() @@ -722,7 +724,7 @@ public void StartRemoteChildSpan_WithSpecifiedSampler() Assert.True(rootSpan.Context.IsValid); Assert.True((rootSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0); // Apply given sampler before default sampler for spans with remote parent. - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetParent(rootSpan.Context) @@ -736,7 +738,7 @@ public void StartRemoteChildSpan_WithSpecifiedSampler() [Fact] public void StartRemoteChildSpan_WithoutSpecifiedSampler() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetNoParent() @@ -745,7 +747,7 @@ public void StartRemoteChildSpan_WithoutSpecifiedSampler() Assert.True(rootSpan.Context.IsValid); Assert.True((rootSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) == 0); // Apply default sampler (always true in the tests) for spans with remote parent. - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(rootSpan.Context) .StartSpan(); @@ -758,7 +760,7 @@ public void StartRemoteChildSpan_WithoutSpecifiedSampler() [Fact] public void StartChildSpan_WithSpecifiedSampler() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.AlwaysSample) .SetNoParent() @@ -768,7 +770,7 @@ public void StartChildSpan_WithSpecifiedSampler() Assert.True((rootSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0); // Apply the given sampler for child spans. - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetParent(rootSpan) @@ -782,7 +784,7 @@ public void StartChildSpan_WithSpecifiedSampler() [Fact] public void StartChildSpan_WithoutSpecifiedSampler() { - var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetNoParent() @@ -792,7 +794,7 @@ public void StartChildSpan_WithoutSpecifiedSampler() Assert.True((rootSpan.Context.TraceOptions & ActivityTraceFlags.Recorded) == 0); // Don't apply the default sampler (always true) for child spans. - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(rootSpan) .StartSpan(); @@ -805,7 +807,7 @@ public void StartChildSpan_WithoutSpecifiedSampler() [Fact] public void StartChildSpan_SampledLinkedParent() { - var rootSpanUnsampled = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var rootSpanUnsampled = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.NeverSample) .SetNoParent() @@ -813,7 +815,7 @@ public void StartChildSpan_SampledLinkedParent() Assert.True((rootSpanUnsampled.Context.TraceOptions & ActivityTraceFlags.Recorded) == 0); var rootSpanSampled = - new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetSampler(Samplers.AlwaysSample) .SetNoParent() @@ -821,7 +823,7 @@ public void StartChildSpan_SampledLinkedParent() Assert.True((rootSpanSampled.Context.TraceOptions & ActivityTraceFlags.Recorded) != 0); // Sampled because the linked parent is sampled. - var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + var childSpan = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .AddLink(new Link(rootSpanSampled.Context)) .SetParent(rootSpanUnsampled) @@ -843,7 +845,7 @@ public void StartRemoteChildSpan_WithProbabilitySamplerDefaultSampler() // If parent is sampled then the remote child must be sampled. var childSpan = - new SpanBuilder(SpanName, spanProcessor, new TraceConfig(ProbabilitySampler.Create(0.1))) + new SpanBuilder(SpanName, spanProcessor, new TraceConfig(ProbabilitySampler.Create(0.1)), Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(new SpanContext( traceId, @@ -859,7 +861,7 @@ public void StartRemoteChildSpan_WithProbabilitySamplerDefaultSampler() // If parent is not sampled then the remote child must be not sampled. childSpan = - new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig) + new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty) .SetSpanKind(SpanKind.Internal) .SetParent(new SpanContext( traceId, @@ -877,11 +879,11 @@ public void StartRemoteChildSpan_WithProbabilitySamplerDefaultSampler() [Fact] public void SpanBuilder_BadArguments() { - Assert.Throws(() => new SpanBuilder(null, spanProcessor, alwaysSampleTraceConfig)); - Assert.Throws(() => new SpanBuilder(SpanName, null, alwaysSampleTraceConfig)); - Assert.Throws(() => new SpanBuilder(SpanName, spanProcessor, null)); + Assert.Throws(() => new SpanBuilder(null, spanProcessor, alwaysSampleTraceConfig, Resource.Empty)); + Assert.Throws(() => new SpanBuilder(SpanName, null, alwaysSampleTraceConfig, Resource.Empty)); + Assert.Throws(() => new SpanBuilder(SpanName, spanProcessor, null, Resource.Empty)); - var spanBuilder = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig); + var spanBuilder = new SpanBuilder(SpanName, spanProcessor, alwaysSampleTraceConfig, Resource.Empty); Assert.Throws(() => spanBuilder.SetParent((ISpan)null)); Assert.Throws(() => spanBuilder.SetParent((SpanContext)null)); Assert.Throws(() => spanBuilder.SetParent((Activity)null)); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs index 6083e1b7d01..e3cea6a33e3 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/SpanTest.cs @@ -14,6 +14,8 @@ // limitations under the License. // +using OpenTelemetry.Resources; + namespace OpenTelemetry.Trace.Test { using System; @@ -55,6 +57,15 @@ public SpanTest() }; } + [Fact] + public void SpanHoldsSameLibraryResourceAsCreatingTracer() + { + var tracerFactory = new TracerFactory(); + var tracer = (Tracer)tracerFactory.GetTracer("foo", "semver:1.0.0"); + var span = (Span)tracer.SpanBuilder("some span").StartSpan(); + Assert.Equal(tracer.LibraryResource, span.LibraryResource); + } + [Fact] public void GetSpanContextFromActivity() { @@ -70,7 +81,8 @@ public void GetSpanContextFromActivity() TraceConfig.Default, spanProcessor, default, - false); + false, + Resource.Empty); Assert.True(span.Context.IsValid); Assert.Equal(activity.TraceId, span.Context.TraceId); Assert.Equal(activity.SpanId, span.Context.SpanId); @@ -94,7 +106,8 @@ public void GetSpanContextFromActivityRecordedWithParent() TraceConfig.Default, spanProcessor, default, - false); + false, + Resource.Empty); Assert.True(span.Context.IsValid); Assert.Equal(activity.TraceId, span.Context.TraceId); Assert.Equal(activity.SpanId, span.Context.SpanId); @@ -120,7 +133,8 @@ public void NoEventsRecordedAfterEnd() TraceConfig.Default, spanProcessor, spanStartTime, - false); + false, + Resource.Empty); var spanEndTime = PreciseTimestamp.GetUtcNow(); span.End(spanEndTime); // Check that adding trace events after Span#End() does not throw any exception and are not @@ -160,7 +174,8 @@ public void ImplicitTimestamps() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); var spanEndTime = PreciseTimestamp.GetUtcNow(); span.End(); @@ -191,7 +206,8 @@ public async Task ActiveSpan_Properties() TraceConfig.Default, spanProcessor, spanStartTime, - false); + false, + Resource.Empty); span.SetAttribute( "MySingleStringAttributeKey", @@ -264,7 +280,8 @@ public async Task EndedSpan_Properties() TraceConfig.Default, spanProcessor, spanStartTime, - false); + false, + Resource.Empty); span.SetAttribute( "MySingleStringAttributeKey", @@ -328,7 +345,8 @@ public void Status_ViaSetStatus() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); Assert.Equal(Status.Ok, span.Status); ((Span)span).Status = Status.Cancelled; @@ -354,7 +372,8 @@ public void status_ViaEndSpanOptions() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); Assert.Equal(Status.Ok, span.Status); ((Span)span).Status = Status.Cancelled; @@ -382,7 +401,8 @@ public void DroppingAttributes() traceConfig, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); for (var i = 0; i < 2 * maxNumberOfAttributes; i++) { IDictionary attributes = new Dictionary(); @@ -432,7 +452,8 @@ public void DroppingAndAddingAttributes() traceConfig, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); for (var i = 0; i < 2 * maxNumberOfAttributes; i++) { IDictionary attributes = new Dictionary(); @@ -499,7 +520,8 @@ public async Task DroppingEvents() traceConfig, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); var eventTimestamps = new DateTimeOffset[2 * maxNumberOfEvents]; @@ -542,7 +564,8 @@ public void DroppingLinks() traceConfig, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); var link = new Link(contextLink); for (var i = 0; i < 2 * maxNumberOfLinks; i++) { @@ -578,7 +601,8 @@ public void BadArguments() TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - false); + false, + Resource.Empty); Assert.Throws(() => span.Status = new Status()); Assert.Throws(() => span.UpdateName(null)); @@ -614,7 +638,8 @@ public void EndSpanStopsActivity(bool recordEvents) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - ownsActivity: true); + ownsActivity: true, + Resource.Empty); span.End(); Assert.Same(parentActivity, Activity.Current); @@ -639,7 +664,8 @@ public void EndSpanDoesNotStopActivityWhenDoesNotOwnIt(bool recordEvents) TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - ownsActivity: false); + ownsActivity: false, + Resource.Empty); span.End(); Assert.Equal(recordEvents, span.HasEnded); @@ -667,7 +693,8 @@ public void EndSpanStopActivity_NotCurrentActivity(bool recordEvents, bool ownsA TraceConfig.Default, spanProcessor, PreciseTimestamp.GetUtcNow(), - ownsActivity: ownsActivity); + ownsActivity: ownsActivity, + Resource.Empty); var anotherActivity = new Activity(SpanName).Start(); span.End(); diff --git a/test/OpenTelemetry.Tests/Impl/Trace/TracerFactoryTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/TracerFactoryTest.cs new file mode 100644 index 00000000000..577bf7c7fd2 --- /dev/null +++ b/test/OpenTelemetry.Tests/Impl/Trace/TracerFactoryTest.cs @@ -0,0 +1,85 @@ +// +// Copyright 2018, 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 OpenTelemetry.Resources; + +namespace OpenTelemetry.Trace.Test +{ + using System; + using OpenTelemetry.Trace.Config; + using OpenTelemetry.Trace.Export; + using OpenTelemetry.Trace.Sampler; + + using Xunit; + + public class TracerFactoryTest + { + private TracerFactory tracerFactory = new TracerFactory(); + + [Fact] + public void GetTracer_NoName_NoVersion() + { + var tracer = (Tracer)tracerFactory.GetTracer(""); + Assert.False(tracer.LibraryResource.Labels.ContainsKey("name")); + Assert.False(tracer.LibraryResource.Labels.ContainsKey("version")); + } + + [Fact] + public void GetTracer_NoName_Version() + { + var tracer = (Tracer)tracerFactory.GetTracer(null, "semver:1.0.0"); + Assert.False(tracer.LibraryResource.Labels.ContainsKey("name")); + Assert.False(tracer.LibraryResource.Labels.ContainsKey("version")); + } + + [Fact] + public void GetTracer_Name_NoVersion() + { + var tracer = (Tracer)tracerFactory.GetTracer("foo"); + Assert.Equal("foo", tracer.LibraryResource.Labels["name"]); + Assert.False(tracer.LibraryResource.Labels.ContainsKey("version")); + } + + [Fact] + public void GetTracer_Name_Version() + { + var tracer = (Tracer)tracerFactory.GetTracer("foo", "semver:1.2.3"); + Assert.Equal("foo", tracer.LibraryResource.Labels["name"]); + Assert.Equal("semver:1.2.3", tracer.LibraryResource.Labels["version"]); + } + + [Fact] + public void FactoryReturnsSameTracerForGivenNameAndVersion() + { + var tracer1 = tracerFactory.GetTracer("foo", "semver:1.2.3"); + var tracer2 = tracerFactory.GetTracer("foo"); + var tracer3 = tracerFactory.GetTracer("foo", "semver:2.3.4"); + var tracer4 = tracerFactory.GetTracer("bar", "semver:1.2.3"); + var tracer5 = tracerFactory.GetTracer("foo", "semver:1.2.3"); + var tracer6 = tracerFactory.GetTracer(""); + var tracer7 = tracerFactory.GetTracer(null); + var tracer8 = tracerFactory.GetTracer(null, "semver:1.2.3"); + + Assert.NotEqual(tracer1, tracer2); + Assert.NotEqual(tracer1, tracer3); + Assert.NotEqual(tracer1, tracer4); + Assert.Equal(tracer1, tracer5); + Assert.NotEqual(tracer5, tracer6); + Assert.Equal(tracer6, tracer7); + Assert.Equal(tracer7, tracer8); + } + } +} diff --git a/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs index 9289a99a608..d0447e937e6 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/TracerTest.cs @@ -15,6 +15,7 @@ // using OpenTelemetry.Context.Propagation; +using OpenTelemetry.Resources; namespace OpenTelemetry.Trace.Test { @@ -37,7 +38,7 @@ public TracerTest() { spanProcessor = new SimpleSpanProcessor(new NoopSpanExporter()); traceConfig = TraceConfig.Default; - tracer = new Tracer(spanProcessor, traceConfig); + tracer = new Tracer(spanProcessor, traceConfig, Resource.Empty); } [Fact] @@ -51,15 +52,17 @@ public void CreateSpanBuilder() public void BadConstructorArgumentsThrow() { var noopProc = new SimpleSpanProcessor(new NoopSpanExporter()); - Assert.Throws(() => new Tracer(null, TraceConfig.Default)); - Assert.Throws(() => new Tracer(null, TraceConfig.Default, new BinaryFormat(), new TraceContextFormat())); + Assert.Throws(() => new Tracer(null, TraceConfig.Default, Resource.Empty)); + Assert.Throws(() => new Tracer(null, TraceConfig.Default, new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, null)); - Assert.Throws(() => new Tracer(noopProc, null, new BinaryFormat(), new TraceContextFormat())); + Assert.Throws(() => new Tracer(noopProc, null, Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, null, new BinaryFormat(), new TraceContextFormat(), Resource.Empty)); - Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, null, new TraceContextFormat())); - Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, new BinaryFormat(), null)); + Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, null, new TraceContextFormat(), Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, new BinaryFormat(), null, Resource.Empty)); + Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, null)); + Assert.Throws(() => new Tracer(noopProc, TraceConfig.Default, new BinaryFormat(), new TraceContextFormat(), null)); } [Fact] @@ -107,7 +110,7 @@ public void GetBinaryFormat() public void GetActiveConfig() { var config = new TraceConfig(Samplers.NeverSample); - var tracer = new Tracer(spanProcessor, config); + var tracer = new Tracer(spanProcessor, config, Resource.Empty); Assert.Equal(config, tracer.ActiveTraceConfig); } diff --git a/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs b/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs index 34cddc5e143..df59269f6ad 100644 --- a/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs +++ b/test/OpenTelemetry.Tests/Impl/Trace/TracingTest.cs @@ -23,9 +23,9 @@ namespace OpenTelemetry.Trace.Test public class TracingTest { [Fact] - public void DefaultTracer() + public void DefaultTracerFactory() { - Assert.Equal(typeof(Tracer), Tracing.Tracer.GetType()); + Assert.Equal(typeof(TracerFactory), Tracing.TracerFactory.GetType()); } [Fact]