-
Notifications
You must be signed in to change notification settings - Fork 765
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
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/OpenTelemetry.Hosting/Implementation/CollectorHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/OpenTelemetry.Hosting/Implementation/OpenTelemetryBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
src/OpenTelemetry.Hosting/OpenTelemetryBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)