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

Separate auto-collectors and make them accept Tracer #266

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/TestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ internal static object Run()
});

var tracerFactory = new TracerFactory(new BatchingSpanProcessor(exporter));
var tracer = tracerFactory.GetTracer(string.Empty);
using (new DependenciesCollector(new DependenciesCollectorOptions(), tracerFactory))
var tracer = tracerFactory.GetTracer(nameof(HttpClientCollector));
using (new HttpClientCollector(new HttpClientCollectorOptions(), tracer))
{
using (tracer.WithSpan(tracer.SpanBuilder("incoming request").SetSampler(Samplers.AlwaysSample).StartSpan()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ internal static void AddLoggingTracer(this IServiceCollection services)
services.AddSingleton<ITracerFactory, LoggingTracerFactory>();

services.AddSingleton(Samplers.AlwaysSample);
services.AddSingleton<RequestsCollectorOptions>();
services.AddSingleton<RequestsCollector>();
services.AddSingleton<AspNetCoreCollectorOptions>();
services.AddSingleton<AspNetCoreCollector>();

services.AddSingleton<DependenciesCollectorOptions>();
services.AddSingleton<HttpClientCollectorOptions>();
services.AddSingleton<DependenciesCollector>();
}

internal static void UseLoggingTracer(this IApplicationBuilder app)
{
app.ApplicationServices.GetService<RequestsCollector>(); // get it instantiated
app.ApplicationServices.GetService<AspNetCoreCollector>(); // get it instantiated
app.ApplicationServices.GetService<DependenciesCollector>(); // get it instantiated
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LoggingTracerFactory : ITracerFactory
{
public override ITracer GetTracer(string name, string version = null)
{
Logger.Log($"TracerFactory.GetTracer('{name}', '{version}')");
Logger.Log($"ITracerFactory.GetTracer('{name}', '{version}')");

// Create a Resource from "name" and "version" information.
var labels = new Dictionary<string, string>();
Expand Down
3 changes: 0 additions & 3 deletions src/OpenTelemetry.Abstractions/Trace/ITracerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

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

/// <summary>
/// Creates Tracers for an instrumentation library.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="RequestsCollector.cs" company="OpenTelemetry Authors">
// <copyright file="AspNetCoreCollector.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,42 +17,24 @@
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;

/// <summary>
/// Requests collector.
/// </summary>
public class RequestsCollector : IDisposable
public class AspNetCoreCollector : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="RequestsCollector"/> class.
/// Initializes a new instance of the <see cref="AspNetCoreCollector"/> class.
/// </summary>
/// <param name="options">Configuration options for dependencies collector.</param>
/// <param name="tracerFactory">TracerFactory which creates the Tracer to record traced with.</param>
public RequestsCollector(RequestsCollectorOptions options, ITracerFactory tracerFactory)
/// <param name="tracer">Tracer to record traced with.</param>
public AspNetCoreCollector(AspNetCoreCollectorOptions options, ITracer tracer)
{
const string name = "Microsoft.AspNetCore";
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
new Dictionary<string, Func<ITracerFactory, ListenerHandler>>()
{
{
name, (t) =>
{
var version = typeof(RequestDelegate).Assembly.GetName().Version;
var tracer = tracerFactory.GetTracer(typeof(RequestsCollector).Namespace, "semver:" + version.ToString());
return new HttpInListener(name, tracer);
}
},
},
tracerFactory,
options.EventFilter);
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpInListener("Microsoft.AspNetCore", tracer), options.EventFilter);
this.diagnosticSourceSubscriber.Subscribe();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="RequestsCollectorOptions.cs" company="OpenTelemetry Authors">
// <copyright file="AspNetCoreCollectorOptions.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,21 +21,21 @@ namespace OpenTelemetry.Collector.AspNetCore
/// <summary>
/// Options for requests collector.
/// </summary>
public class RequestsCollectorOptions
public class AspNetCoreCollectorOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="RequestsCollectorOptions"/> class.
/// Initializes a new instance of the <see cref="AspNetCoreCollectorOptions"/> class.
/// </summary>
public RequestsCollectorOptions()
public AspNetCoreCollectorOptions()
{
this.EventFilter = DefaultFilter;
}

/// <summary>
/// Initializes a new instance of the <see cref="RequestsCollectorOptions"/> class.
/// Initializes a new instance of the <see cref="AspNetCoreCollectorOptions"/> class.
/// </summary>
/// <param name="eventFilter">Custom filtering predicate for DiagnosticSource events, if any.</param>
internal RequestsCollectorOptions(Func<string, object, object, bool> eventFilter = null)
internal AspNetCoreCollectorOptions(Func<string, object, object, bool> eventFilter = null)
{
// TODO This API is unusable and likely to change, let's not expose it for now.

Expand Down
44 changes: 44 additions & 0 deletions src/OpenTelemetry.Collector.Dependencies/AzureClientsCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <copyright file="AzureClientsCollector.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.Collector.Dependencies
{
using System;
using OpenTelemetry.Trace;

/// <summary>
/// Dependencies collector.
/// </summary>
public class AzureClientsCollector : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="AzureClientsCollector"/> class.
/// </summary>
/// <param name="tracer">Tracer to record traced with.</param>
public AzureClientsCollector(ITracer tracer)
{
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new AzureSdkDiagnosticListener("Azure.Clients", tracer), null);
this.diagnosticSourceSubscriber.Subscribe();
}

public void Dispose()
{
this.diagnosticSourceSubscriber?.Dispose();
}
}
}
44 changes: 44 additions & 0 deletions src/OpenTelemetry.Collector.Dependencies/AzurePipelineCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <copyright file="AzurePipelineCollector.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.Collector.Dependencies
{
using System;
using OpenTelemetry.Trace;

/// <summary>
/// Dependencies collector.
/// </summary>
public class AzurePipelineCollector : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="AzurePipelineCollector"/> class.
/// </summary>
/// <param name="tracer">Tracer to record traced with.</param>
public AzurePipelineCollector(ITracer tracer)
{
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new AzureSdkDiagnosticListener("Azure.Pipeline", tracer), null);
this.diagnosticSourceSubscriber.Subscribe();
}

public void Dispose()
{
this.diagnosticSourceSubscriber?.Dispose();
}
}
}
54 changes: 14 additions & 40 deletions src/OpenTelemetry.Collector.Dependencies/DependenciesCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,30 @@ namespace OpenTelemetry.Collector.Dependencies
{
using System;
using System.Collections.Generic;
using OpenTelemetry.Collector.Dependencies.Implementation;
using OpenTelemetry.Trace;

/// <summary>
/// Dependencies collector.
/// </summary>
public class DependenciesCollector : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
private readonly List<IDisposable> collectors = new List<IDisposable>();

/// <summary>
/// Initializes a new instance of the <see cref="DependenciesCollector"/> class.
/// </summary>
/// <param name="options">Configuration options for dependencies collector.</param>
/// <param name="tracerFactory">TracerFactory to create a Tracer to record traced with.</param>
public DependenciesCollector(DependenciesCollectorOptions options, ITracerFactory tracerFactory)
public DependenciesCollector(HttpClientCollectorOptions options, ITracerFactory tracerFactory)
{
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
new Dictionary<string, Func<ITracerFactory, ListenerHandler>>()
{
{
"HttpHandlerDiagnosticListener", (tf) =>
{
var tracer = tf.GetTracer("OpenTelemetry.Collector.Dependencies.HttpHandlerDiagnosticListener");
return new HttpHandlerDiagnosticListener(tracer);
}
},
{
"Azure.Clients", (tf) =>
{
var tracer = tf.GetTracer("OpenTelemetry.Collector.Dependencies.Azure.Clients");
return new AzureSdkDiagnosticListener("Azure.Clients", tracer);
}
},
{
"Azure.Pipeline", (tf) =>
{
var tracer = tf.GetTracer("OpenTelemetry.Collector.Dependencies.Azure.Pipeline");
return new AzureSdkDiagnosticListener("Azure.Pipeline", tracer);
}
},
},
tracerFactory,
options.EventFilter);
this.diagnosticSourceSubscriber.Subscribe();
var assemblyVersion = typeof(DependenciesCollector).Assembly.GetName().Version;
var httpClientListener = new HttpClientCollector(options, tracerFactory.GetTracer(nameof(HttpClientCollector), "semver:" + assemblyVersion));
var azureClientsListener = new AzureClientsCollector(tracerFactory.GetTracer(nameof(AzureClientsCollector), "semver:" + assemblyVersion));
var azurePipelineListener = new AzurePipelineCollector(tracerFactory.GetTracer(nameof(AzurePipelineCollector), "semver:" + assemblyVersion));

this.collectors.Add(httpClientListener);
this.collectors.Add(azureClientsListener);
this.collectors.Add(azurePipelineListener);
}

public void Dispose()
{
this.diagnosticSourceSubscriber?.Dispose();
foreach (var collector in this.collectors)
{
collector.Dispose();
}
}
}
}
46 changes: 46 additions & 0 deletions src/OpenTelemetry.Collector.Dependencies/HttpClientCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <copyright file="HttpClientCollector.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.Collector.Dependencies
{
using System;
using OpenTelemetry.Collector.Dependencies.Implementation;
using OpenTelemetry.Trace;

/// <summary>
/// Dependencies collector.
/// </summary>
public class HttpClientCollector : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="HttpClientCollector"/> class.
/// </summary>
/// <param name="options">Configuration options for dependencies collector.</param>
/// <param name="tracer">Tracer to record traced with.</param>
public HttpClientCollector(HttpClientCollectorOptions options, ITracer tracer)
{
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(new HttpHandlerDiagnosticListener(tracer), options.EventFilter);
this.diagnosticSourceSubscriber.Subscribe();
}

public void Dispose()
{
this.diagnosticSourceSubscriber?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DependenciesCollectorOptions.cs" company="OpenTelemetry Authors">
// <copyright file="HttpClientCollectorOptions.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,21 +22,21 @@ namespace OpenTelemetry.Collector.Dependencies
/// <summary>
/// Options for dependencies collector.
/// </summary>
public class DependenciesCollectorOptions
public class HttpClientCollectorOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="DependenciesCollectorOptions"/> class.
/// Initializes a new instance of the <see cref="HttpClientCollectorOptions"/> class.
/// </summary>
public DependenciesCollectorOptions()
public HttpClientCollectorOptions()
{
this.EventFilter = DefaultFilter;
}

/// <summary>
/// Initializes a new instance of the <see cref="DependenciesCollectorOptions"/> class.
/// Initializes a new instance of the <see cref="HttpClientCollectorOptions"/> class.
/// </summary>
/// <param name="eventFilter">Custom filtering predicate for DiagnosticSource events, if any.</param>
internal DependenciesCollectorOptions(Func<string, object, object, bool> eventFilter)
internal HttpClientCollectorOptions(Func<string, object, object, bool> eventFilter)
{
// TODO This API is unusable and likely to change, let's not expose it for now.

Expand Down
Loading