|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System; |
| 20 | +using Microsoft.Extensions.DependencyInjection; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | +using OpenTelemetry.Collector.AspNetCore; |
| 23 | +using OpenTelemetry.Collector.Dependencies; |
| 24 | +using OpenTelemetry.Exporter.Zipkin; |
| 25 | +using OpenTelemetry.Trace; |
| 26 | +using OpenTelemetry.Trace.Config; |
| 27 | +using OpenTelemetry.Trace.Export; |
| 28 | +using OpenTelemetry.Trace.Sampler; |
| 29 | + |
| 30 | +namespace Server |
| 31 | +{ |
| 32 | + public partial class Startup |
| 33 | + { |
| 34 | + private void ConfigureOpenTelemetryServices(IServiceCollection services) |
| 35 | + { |
| 36 | + services.AddSingleton<ISampler>(Samplers.AlwaysSample); |
| 37 | + services.AddSingleton<ZipkinTraceExporterOptions>(_ => new ZipkinTraceExporterOptions |
| 38 | + { |
| 39 | + ServiceName = "aggregator", |
| 40 | + Endpoint = new Uri("http://localhost:9411/api/v2/spans") |
| 41 | + }); |
| 42 | + services.AddSingleton<SpanExporter, ZipkinTraceExporter>(); |
| 43 | + services.AddSingleton<SpanProcessor, BatchingSpanProcessor>(); |
| 44 | + services.AddSingleton<TraceConfig>(); |
| 45 | + services.AddSingleton<ITracer, Tracer>(); |
| 46 | + |
| 47 | + // you may also configure request and dependencies collectors |
| 48 | + services.AddSingleton<RequestsCollectorOptions>(new RequestsCollectorOptions()); |
| 49 | + services.AddSingleton<RequestsCollector>(); |
| 50 | + |
| 51 | + services.AddSingleton<DependenciesCollectorOptions>(new DependenciesCollectorOptions()); |
| 52 | + services.AddSingleton<DependenciesCollector>(); |
| 53 | + } |
| 54 | + |
| 55 | + public void ConfigureOpenTelemetry(IServiceProvider serviceProvider) |
| 56 | + { |
| 57 | + serviceProvider.GetRequiredService<ILogger<Startup>>().LogInformation("Enabling OpenTelemetry"); |
| 58 | + |
| 59 | + serviceProvider.GetRequiredService<RequestsCollector>(); |
| 60 | + serviceProvider.GetRequiredService<DependenciesCollector>(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments