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 #239

Merged
merged 25 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
996ec40
merge (#1)
discostu105 Jun 4, 2019
7e6e91f
Merge remote-tracking branch 'upstream/master'
discostu105 Jun 14, 2019
0900aa9
Merge remote-tracking branch 'upstream/master'
z1c0 Aug 12, 2019
c6ef11b
Merge pull request #2 from z1c0/master
z1c0 Aug 12, 2019
891f593
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
z1c0 Sep 13, 2019
a4dcb12
Merge remote-tracking branch 'upstream/master'
z1c0 Sep 26, 2019
ba535a3
Merge remote-tracking branch 'upstream/master'
z1c0 Sep 30, 2019
6ce27de
Implementing the "Named Tracers" proposal for .NET
z1c0 Sep 26, 2019
c53d101
Improve Named Tracers implementation.
z1c0 Sep 27, 2019
b5b1047
Improved and refactored Tracer creation
z1c0 Sep 27, 2019
e71bf65
Fixed issues and tests after merge
z1c0 Sep 30, 2019
1fb25f3
Implemented PR feedback
z1c0 Oct 1, 2019
519129f
Merge remote-tracking branch 'upstream/master'
z1c0 Oct 1, 2019
f7d9996
Implementing the "Named Tracers" proposal for .NET
z1c0 Sep 26, 2019
0713421
Improve Named Tracers implementation.
z1c0 Sep 27, 2019
c64010d
Improved and refactored Tracer creation
z1c0 Sep 27, 2019
f28fdd4
Fixed issues and tests after merge
z1c0 Sep 30, 2019
9f43f14
Implemented PR feedback
z1c0 Oct 1, 2019
1e3a954
Resolve conflict
z1c0 Oct 1, 2019
27a6ecb
Implement PR review feedback
z1c0 Oct 2, 2019
c4081a8
Merge branch 'master' into named-tracers
z1c0 Oct 2, 2019
46023fc
Organize using directives
z1c0 Oct 2, 2019
5b06187
Update src/OpenTelemetry.Collector.AspNetCore/RequestsCollector.cs
z1c0 Oct 2, 2019
0014905
Update RequestsCollector.cs
z1c0 Oct 2, 2019
3a667c8
Merge branch 'master' into named-tracers
Oct 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/Exporters/TestApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions samples/Exporters/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()))
{
Expand Down
7 changes: 3 additions & 4 deletions samples/Exporters/TestJaeger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions samples/Exporters/TestLightstep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
{
Expand All @@ -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()))
{
Expand Down
6 changes: 3 additions & 3 deletions samples/Exporters/TestRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions samples/Exporters/TestStackdriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));

Expand Down
6 changes: 3 additions & 3 deletions samples/Exporters/TestZipkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
Expand All @@ -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
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 System;
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 IDisposable 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 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<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 abstract class 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>
public abstract ITracer GetTracer(string name, string version = null);
}
}
Loading