-
-
Save yowko/58a0b75afced949044399cff39227cde to your computer and use it in GitHub Desktop.
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
using System.Diagnostics; | |
using OpenTelemetry.Resources; | |
using OpenTelemetry.Trace; | |
using OtlpGrpcServer; | |
var builder = WebApplication.CreateBuilder(args); | |
ActivitySource sSource = new ActivitySource(builder.Environment.ApplicationName); | |
// Add services to the container. | |
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | |
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddSwaggerGen(); | |
builder.Services.AddGrpcClient<Greeter.GreeterClient>(o => o.Address = new Uri("https://localhost:7150")); | |
var tracingOtlpEndpoint = builder.Configuration.GetValue("Otlp:Endpoint", defaultValue: "http://localhost:4317")!; | |
var otel = builder.Services.AddOpenTelemetry(); | |
// Configure OpenTelemetry Resources with the application name | |
otel.ConfigureResource(resource => resource | |
.AddService(serviceName: builder.Environment.ApplicationName)); | |
otel.WithTracing(tracing => | |
{ | |
tracing.AddAspNetCoreInstrumentation(); | |
tracing.AddHttpClientInstrumentation(); | |
tracing.AddGrpcClientInstrumentation(); | |
tracing.AddSource(sSource.Name ); | |
if (tracingOtlpEndpoint != null) | |
{ | |
tracing.AddOtlpExporter(otlpOptions => | |
{ | |
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); | |
}); | |
} | |
// else | |
// { | |
tracing.AddConsoleExporter(); | |
//} | |
}); | |
var app = builder.Build(); | |
// // Configure the HTTP request pipeline. | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseSwagger(); | |
app.UseSwaggerUI(); | |
} | |
app.MapGet("/getweatherforecastgrpc", async () => | |
{ | |
using Activity activity = sSource.StartActivity("call GetWeatherAsync via grpc"); | |
return await GetWeatherGrpcAsync(); | |
}) | |
.WithName("GetWeatherForecast"); | |
app.Run(); | |
async Task<HelloReply?> GetWeatherGrpcAsync() | |
{ | |
using Activity activity = sSource.StartActivity("exec GetWeatherAsync via grpc"); | |
activity?.SetTag("yowko", "tag grpc OK"); | |
var grpcClient= app.Services.GetService<Greeter.GreeterClient>(); | |
return await grpcClient.SayHelloAsync(new HelloRequest | |
{ | |
Name = ".NET 8" | |
}); | |
} |
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
using System.Diagnostics; | |
using OpenTelemetry.Resources; | |
using OpenTelemetry.Trace; | |
using OtlpGrpcServer.Services; | |
var builder = WebApplication.CreateBuilder(args); | |
ActivitySource sSource = new ActivitySource(builder.Environment.ApplicationName); | |
// Add services to the container. | |
builder.Services.AddGrpc(); | |
var tracingOtlpEndpoint = builder.Configuration.GetValue("Otlp:Endpoint", defaultValue: "http://localhost:4317")!; | |
var otel = builder.Services.AddOpenTelemetry(); | |
// Configure OpenTelemetry Resources with the application name | |
otel.ConfigureResource(resource => resource | |
.AddService(serviceName: builder.Environment.ApplicationName)); | |
// Add Tracing for ASP.NET Core and our custom ActivitySource and export to Tempo | |
otel.WithTracing(tracing => | |
{ | |
tracing.AddAspNetCoreInstrumentation(); | |
tracing.AddHttpClientInstrumentation(); | |
tracing.AddSource(sSource.Name); | |
if (tracingOtlpEndpoint != null) | |
{ | |
tracing.AddOtlpExporter(otlpOptions => | |
{ | |
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint); | |
}); | |
} | |
else | |
{ | |
tracing.AddConsoleExporter(); | |
} | |
}); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
app.MapGrpcService<GreeterService>(); | |
app.MapGet("/", | |
() => | |
"Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); | |
//Task.Run(() => app.Run()); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment