Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named Tracers #238

Closed
wants to merge 11 commits into from
4 changes: 3 additions & 1 deletion samples/Exporters/TestApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Samples
using System.Threading;
using Microsoft.ApplicationInsights.Extensibility;
using OpenTelemetry.Exporter.ApplicationInsights;
using OpenTelemetry.Resources;
using OpenTelemetry.Stats;
using OpenTelemetry.Stats.Aggregations;
using OpenTelemetry.Stats.Measures;
Expand Down Expand Up @@ -59,7 +60,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)
Expand Down
6 changes: 4 additions & 2 deletions samples/Exporters/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Samples
using System.Threading;
using OpenTelemetry.Collector.Dependencies;
using OpenTelemetry.Exporter.Zipkin;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Config;
using OpenTelemetry.Trace.Export;
Expand All @@ -39,8 +40,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()))
{
Expand Down
6 changes: 4 additions & 2 deletions samples/Exporters/TestJaeger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Samples
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Exporter.Jaeger;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Config;
using OpenTelemetry.Trace.Export;
Expand All @@ -38,7 +39,8 @@ internal static object Run(string host, int port)
});

// 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()))
Expand All @@ -56,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
Expand Down
7 changes: 5 additions & 2 deletions samples/Exporters/TestLightstep.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// <auto-generated/>

using OpenTelemetry.Resources;

namespace Samples
{
using System;
Expand All @@ -21,7 +23,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()))
{
Expand All @@ -38,7 +41,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()))
{
Expand Down
6 changes: 4 additions & 2 deletions samples/Exporters/TestRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Samples
using System.Threading;
using OpenTelemetry.Collector.StackExchangeRedis;
using OpenTelemetry.Exporter.Zipkin;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Config;
using OpenTelemetry.Trace.Export;
Expand All @@ -39,7 +40,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);

Expand All @@ -65,7 +67,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
Expand Down
4 changes: 3 additions & 1 deletion samples/Exporters/TestStackdriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Samples
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Exporter.Stackdriver;
using OpenTelemetry.Resources;
using OpenTelemetry.Stats;
using OpenTelemetry.Stats.Aggregations;
using OpenTelemetry.Stats.Measures;
Expand Down Expand Up @@ -57,7 +58,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"));

Expand Down
6 changes: 4 additions & 2 deletions samples/Exporters/TestZipkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Samples
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Exporter.Zipkin;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Config;
using OpenTelemetry.Trace.Export;
Expand All @@ -37,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);

// Create a scoped span. It will end automatically when using statement ends
using (tracer.WithSpan(tracer.SpanBuilder("Main").StartSpan()))
Expand All @@ -54,7 +56,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class LoggingTracerExtensions
{
internal static void AddLoggingTracer(this IServiceCollection services)
{
services.AddSingleton<ITracer, LoggingTracer>();
services.AddSingleton<ITracerFactory, LoggingTracerFactory>();

services.AddSingleton(Samplers.AlwaysSample);
services.AddSingleton<RequestsCollectorOptions>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
15 changes: 13 additions & 2 deletions samples/LoggingTracer/LoggingTracer/LoggingTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

using System.Linq;
using OpenTelemetry.Resources;

namespace LoggingTracer
{
using OpenTelemetry.Context;
Expand All @@ -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();
Expand All @@ -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 IScope WithSpan(ISpan span)
{
Logger.Log("Tracer.WithSpan");
Logger.Log($"{prefix}.WithSpan");
return new CurrentSpanUtils.LoggingScope(span);
}
}
Expand Down
35 changes: 35 additions & 0 deletions samples/LoggingTracer/LoggingTracer/LoggingTracerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// <copyright file="LoggingTracerFactory.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

using System.Collections.Generic;
using OpenTelemetry.Resources;

namespace LoggingTracer
{
using OpenTelemetry.Context;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;

public class LoggingTracerFactory : ITracerFactory
{
public 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<string, string>();
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);
}
}
}
35 changes: 35 additions & 0 deletions src/OpenTelemetry.Abstractions/Trace/ITracerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// <copyright file="ITracerFactory.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>

namespace OpenTelemetry.Trace
{
using OpenTelemetry.Context;
using OpenTelemetry.Context.Propagation;

/// <summary>
/// Creates Tracers for an instrumentation library.
/// </summary>
public interface ITracerFactory
{
/// <summary>
/// Returns an ITracer for a given name and version.
/// </summary>
/// <param name="name">Name of the instrumentation library.</param>
/// <param name="version">Version of the instrumentation library (optional).</param>
/// <returns>Tracer for the given name and version information.</returns>
ITracer GetTracer(string name, string version = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal class HttpInListener : ListenerHandler<HttpRequest>
private readonly PropertyFetcher beforeActionTemplateFetcher = new PropertyFetcher("Template");
private readonly bool hostingSupportsW3C = false;

public HttpInListener(ITracer tracer, Func<HttpRequest, ISampler> samplerFactory)
: base("Microsoft.AspNetCore", tracer, samplerFactory)
public HttpInListener(string name, Version version, ITracerFactory tracerFactory, Func<HttpRequest, ISampler> samplerFactory)
: base(name, version, tracerFactory, samplerFactory)
{
this.hostingSupportsW3C = typeof(HttpRequest).Assembly.GetName().Version.Major >= 3;
}
Expand All @@ -46,7 +46,7 @@ public override void OnStartActivity(Activity activity, object payload)
var context = this.startContextFetcher.Fetch(payload) as HttpContext;

if (context == null)
{
{
CollectorEventSource.Log.NullPayload(nameof(HttpInListener) + EventNameSuffix);
return;
}
Expand Down
12 changes: 7 additions & 5 deletions src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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;
Expand All @@ -33,16 +34,17 @@ public class RequestsCollector : IDisposable
/// Initializes a new instance of the <see cref="RequestsCollector"/> class.
/// </summary>
/// <param name="options">Configuration options for dependencies collector.</param>
/// <param name="tracer">Tracer to record traced with.</param>
/// <param name="tracerFactory">TracerFactory which creates the Tracer to record traced with.</param>
/// <param name="sampler">Sampler to use to sample dependency calls.</param>
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<HttpRequest>(
new Dictionary<string, Func<ITracer, Func<HttpRequest, ISampler>, ListenerHandler<HttpRequest>>>()
new Dictionary<string, Func<ITracerFactory, Func<HttpRequest, ISampler>, ListenerHandler<HttpRequest>>>()
{
{ "Microsoft.AspNetCore", (t, s) => new HttpInListener(t, s) },
{ name, (t, s) => new HttpInListener(name, Assembly.GetExecutingAssembly().GetName().Version, t, s) },
},
tracer,
tracerFactory,
x =>
{
ISampler s = null;
Expand Down
Loading