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

Add OpenTelemetry.Hosting #253

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Ligh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.LightStep.Tests", "test\OpenTelemetry.Exporter.LightStep.Tests\OpenTelemetry.Exporter.LightStep.Tests.csproj", "{BFDC583B-B2F2-4354-A0CE-83816883847C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Hosting", "src\OpenTelemetry.Hosting\OpenTelemetry.Hosting.csproj", "{A03102CD-A996-4418-BA19-7141C34B5A7D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Hosting.Tests", "test\OpenTelemetry.Hosting.Tests\OpenTelemetry.Hosting.Tests.csproj", "{4CB1187F-27F2-48F9-83F4-3A7567F73B5F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -208,6 +212,14 @@ Global
{BFDC583B-B2F2-4354-A0CE-83816883847C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFDC583B-B2F2-4354-A0CE-83816883847C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFDC583B-B2F2-4354-A0CE-83816883847C}.Release|Any CPU.Build.0 = Release|Any CPU
{A03102CD-A996-4418-BA19-7141C34B5A7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A03102CD-A996-4418-BA19-7141C34B5A7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A03102CD-A996-4418-BA19-7141C34B5A7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A03102CD-A996-4418-BA19-7141C34B5A7D}.Release|Any CPU.Build.0 = Release|Any CPU
{4CB1187F-27F2-48F9-83F4-3A7567F73B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CB1187F-27F2-48F9-83F4-3A7567F73B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CB1187F-27F2-48F9-83F4-3A7567F73B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CB1187F-27F2-48F9-83F4-3A7567F73B5F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />

<ProjectReference Include="..\..\..\src\OpenTelemetry.Abstractions\OpenTelemetry.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\OpenTelemetry.Hosting\OpenTelemetry.Hosting.csproj" />

<ProjectReference Include="..\..\..\src\OpenTelemetry.Collector.AspNetCore\OpenTelemetry.Collector.AspNetCore.csproj" />

Expand Down

This file was deleted.

12 changes: 9 additions & 3 deletions samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ namespace LoggingTracer.Demo.AspNetCore
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Collector.AspNetCore;
using OpenTelemetry.Collector.Dependencies;
using OpenTelemetry.Hosting;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLoggingTracer();
services.AddOpenTelemetry(telemetry =>
{
telemetry.SetTracer<LoggingTracer>();
telemetry.AddCollector<RequestsCollector>(new RequestsCollectorOptions());
telemetry.AddCollector<DependenciesCollector>(new DependenciesCollectorOptions());
});
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand All @@ -23,8 +31,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDeveloperExceptionPage();
}

app.UseLoggingTracer();

app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
Expand Down
25 changes: 25 additions & 0 deletions src/OpenTelemetry.Hosting/IOpenTelemetryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="IOpenTelemetryBuilder.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.Hosting
{
using Microsoft.Extensions.DependencyInjection;

public interface IOpenTelemetryBuilder
{
IServiceCollection Services { get; }
}
}
49 changes: 49 additions & 0 deletions src/OpenTelemetry.Hosting/Implementation/CollectorHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="CollectorHostedService.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.Hosting.Implementation
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

internal class CollectorHostedService<TCollector> : IHostedService
where TCollector : class
{
private readonly IServiceProvider serviceProvider;

private TCollector collector;

public CollectorHostedService(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

public Task StartAsync(CancellationToken cancellationToken)
{
this.collector = this.serviceProvider.GetRequiredService<TCollector>();
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
(this.collector as IDisposable)?.Dispose();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this in dispose (implement IDisposable)

return Task.CompletedTask;
}
}
}
30 changes: 30 additions & 0 deletions src/OpenTelemetry.Hosting/Implementation/OpenTelemetryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <copyright file="OpenTelemetryBuilder.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.Hosting.Implementation
{
using Microsoft.Extensions.DependencyInjection;

internal class OpenTelemetryBuilder : IOpenTelemetryBuilder
{
public OpenTelemetryBuilder(IServiceCollection services)
{
this.Services = services;
}

public IServiceCollection Services { get; }
}
}
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Hosting/OpenTelemetry.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Description>OpenTelemetry hosting integration</Description>
<RootNamespace>OpenTelemetry</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.0" />
<ProjectReference Include="..\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>
</Project>
113 changes: 113 additions & 0 deletions src/OpenTelemetry.Hosting/OpenTelemetryBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// <copyright file="OpenTelemetryBuilderExtensions.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.Hosting
{
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Hosting.Implementation;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Export;

public static class OpenTelemetryBuilderExtensions
{
public static IOpenTelemetryBuilder AddCollector<TCollector>(this IOpenTelemetryBuilder builder)
where TCollector : class
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Services.TryAddSingleton<TCollector>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, CollectorHostedService<TCollector>>());
return builder;
}

public static IOpenTelemetryBuilder AddCollector<TCollector, TCollectorOptions>(this IOpenTelemetryBuilder builder)
where TCollector : class
where TCollectorOptions : class
{
builder.AddCollector<TCollector>();
builder.Services.TryAddSingleton<TCollectorOptions>();
return builder;
}

public static IOpenTelemetryBuilder AddCollector<TCollector>(this IOpenTelemetryBuilder builder, object options)
where TCollector : class
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

builder.AddCollector<TCollector>();
builder.Services.TryAdd(ServiceDescriptor.Singleton(options.GetType(), options));
return builder;
}

public static IOpenTelemetryBuilder SetSpanExporter<TExporter>(this IOpenTelemetryBuilder builder)
where TExporter : SpanExporter
{
builder.Services.TryAddSingleton<SpanExporter, TExporter>();
return builder;
}

public static IOpenTelemetryBuilder SetSpanExporter<TExporter>(this IOpenTelemetryBuilder builder, object options)
where TExporter : SpanExporter
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

builder.SetSpanExporter<TExporter>();
builder.Services.TryAdd(ServiceDescriptor.Singleton(options.GetType(), options));
return builder;
}

public static IOpenTelemetryBuilder SetTracer<TTracer>(this IOpenTelemetryBuilder builder)
where TTracer : class, ITracer
{
builder.Services.TryAddSingleton<ITracer, TTracer>();
return builder;
}

public static IOpenTelemetryBuilder SetTracer<TTracer>(this IOpenTelemetryBuilder builder, object options)
where TTracer : class, ITracer
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}

builder.SetTracer<TTracer>();
builder.Services.TryAdd(ServiceDescriptor.Singleton(options.GetType(), options));
return builder;
}

public static IOpenTelemetryBuilder SetTracer<TTracer, TTracerOptions>(this IOpenTelemetryBuilder builder)
where TTracer : class, ITracer
where TTracerOptions : class
{
builder.SetTracer<TTracer>();
builder.Services.TryAddSingleton<TTracerOptions>();
return builder;
}
}
}
Loading