Skip to content

Instantly share code, notes, and snippets.

@yowko
Created December 1, 2023 09:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yowko/58a0b75afced949044399cff39227cde to your computer and use it in GitHub Desktop.
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"
});
}
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