Skip to content

Commit db7e27d

Browse files
authored
Add OpenTelemetry to aggregator sample (#570)
1 parent 38335b0 commit db7e27d

File tree

6 files changed

+109
-3
lines changed

6 files changed

+109
-3
lines changed

build/dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<NewtonsoftJsonPackageVersion>12.0.2</NewtonsoftJsonPackageVersion>
1919
<NunitPackageVersion>3.12.0</NunitPackageVersion>
2020
<Nunit3TestAdapterPackageVersion>3.13.0</Nunit3TestAdapterPackageVersion>
21+
<OpenTelemetryPackageVersion>0.2.0-alpha.51</OpenTelemetryPackageVersion>
2122
<SystemCommandLinePackageVersion>0.3.0-alpha.19405.1</SystemCommandLinePackageVersion>
2223
<SystemDiagnosticsDiagnosticSourcePackageVersion>4.5.1</SystemDiagnosticsDiagnosticSourcePackageVersion>
2324
<SystemSecurityPrincipalWindowsPackageVersion>4.6.0</SystemSecurityPrincipalWindowsPackageVersion>

examples/Aggregator/Server/Server.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
<Protobuf Include="..\Proto\count.proto" GrpcServices="Both" Link="Protos\count.proto" />
1111

1212
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
13+
14+
<PackageReference Include="OpenTelemetry" Version="$(OpenTelemetryPackageVersion)" />
15+
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="$(OpenTelemetryPackageVersion)" />
16+
<PackageReference Include="OpenTelemetry.Collector.Dependencies" Version="$(OpenTelemetryPackageVersion)" />
17+
<PackageReference Include="OpenTelemetry.Collector.AspNetCore" Version="$(OpenTelemetryPackageVersion)" />
1318
</ItemGroup>
19+
20+
<!-- Required because the project is using a preview version of OpenTelemtry from MyGet -->
21+
<PropertyGroup Label="RestoreSources">
22+
<RestoreSources>
23+
$(RestoreSources);
24+
https://www.myget.org/F/opentelemetry/api/v3/index.json;
25+
</RestoreSources>
26+
</PropertyGroup>
1427

1528
</Project>

examples/Aggregator/Server/Services/GreeterService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public override async Task SayHellos(HelloRequest request, IServerStreamWriter<H
4848

4949
await responseStream.WriteAsync(new HelloReply { Message = message });
5050

51-
// Gotta look busy
5251
await Task.Delay(1000);
5352
}
5453
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

examples/Aggregator/Server/Startup.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,39 @@
1717
#endregion
1818

1919
using System;
20+
using System.Diagnostics;
2021
using Count;
2122
using Greet;
2223
using Microsoft.AspNetCore.Builder;
2324
using Microsoft.AspNetCore.Hosting;
2425
using Microsoft.AspNetCore.Http;
26+
using Microsoft.Extensions.Configuration;
2527
using Microsoft.Extensions.DependencyInjection;
2628
using Microsoft.Extensions.Hosting;
29+
using Microsoft.Extensions.Logging;
2730

2831
namespace Server
2932
{
30-
public class Startup
33+
public partial class Startup
3134
{
35+
private readonly IConfiguration _configuration;
36+
private const string EnableOpenTelemetryKey = "EnableOpenTelemetry";
37+
38+
public Startup(IConfiguration configuration)
39+
{
40+
_configuration = configuration;
41+
}
42+
3243
public void ConfigureServices(IServiceCollection services)
3344
{
3445
services.AddGrpc();
3546
services.AddSingleton<IncrementingCounter>();
3647

48+
if (_configuration.GetValue<bool>(EnableOpenTelemetryKey))
49+
{
50+
ConfigureOpenTelemetryServices(services);
51+
}
52+
3753
// These clients will call back to the server
3854
services
3955
.AddGrpcClient<Greeter.GreeterClient>((s, o) => { o.Address = GetCurrentAddress(s); })
@@ -62,6 +78,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6278
app.UseDeveloperExceptionPage();
6379
}
6480

81+
if (_configuration.GetValue<bool>(EnableOpenTelemetryKey))
82+
{
83+
ConfigureOpenTelemetry(app.ApplicationServices);
84+
}
85+
6586
app.UseRouting();
6687

6788
app.UseEndpoints(endpoints =>

examples/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,17 @@ The worker shows how a [.NET worker service](https://devblogs.microsoft.com/aspn
9393

9494
The aggregator shows how a to make nested gRPC calls (a gRPC service calling another gRPC service). The gRPC client factory is used in ASP.NET Core to inject a client into services. The gRPC client factory is configured to propagate the context from the original call to the nested call. In this example the cancellation from the client will automatically propagate through to nested gRPC calls.
9595

96+
The aggregator can optionally be run with [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-dotnet) enabled. OpenTelemetry is configured to capture tracing information and send it to [Zipkin](https://zipkin.io), a distributed tracing system. A Zipkin server needs to be running to receive traces. The simplest way to do that is [run the Zipkin Docker image](https://zipkin.io/pages/quickstart.html).
97+
98+
To run the aggregator server with OpenTelemetry enabled:
99+
100+
```console
101+
dotnet run --EnableOpenTelemetry=true
102+
```
103+
96104
##### Scenarios:
97105

98106
* Client factory
99107
* Client canceling a call
100-
* Cancellation propagation
108+
* Cancellation propagation
109+
* Capture tracing with [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-dotnet) (optional)

0 commit comments

Comments
 (0)