Skip to content

Commit

Permalink
feat:add otel exporter (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahendraintelops authored Feb 21, 2024
1 parent abb66cd commit 7f53981
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,51 @@ using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics;
using OpenTelemetry.Exporter;

namespace Infrastructure.Extensions
{
public static class OpenTelemetryRegistration
{
public static IServiceCollection AddCustomOpenTelemetry(this IServiceCollection services, string serviceName)
public static IServiceCollection AddCustomOpenTelemetry(this IServiceCollection services, IConfiguration configuration)
{
services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter())
var serviceName = configuration["Otel:ServiceName"];
var otlpEndpoint = configuration["Otel:OtlpEndpoint"];
services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: serviceName))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter());
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(otlpEndpoint);
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
}))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(otlpEndpoint);
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
})
.AddConsoleExporter());
// Register Tracer so it can be injected into other components (eg Controllers)
services.AddSingleton(TracerProvider.Default.GetTracer(serviceName));

return services;
}

public static ILoggingBuilder AddCustomOpenTelemetryLogging(this ILoggingBuilder builder, string serviceName)
public static ILoggingBuilder AddCustomOpenTelemetryLogging(this ILoggingBuilder builder, IConfiguration configuration)
{
var serviceName = configuration["Otel:ServiceName"];
return builder.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter();
});
}

public static IServiceCollection AddHoneycomb(this IServiceCollection services, IConfiguration configuration)
{
var honeycombOptions = configuration.GetHoneycombOptions();

// Setup OpenTelemetry Tracing
services.AddOpenTelemetry().WithTracing(otelBuilder =>
{
otelBuilder
.AddHoneycomb(honeycombOptions)
.AddCommonInstrumentations();
.AddService(serviceName));
});

// Register Tracer so it can be injected into other components (eg Controllers)
services.AddSingleton(TracerProvider.Default.GetTracer(honeycombOptions.ServiceName));

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Honeycomb.OpenTelemetry" Version="1.3.1" />
<PackageReference Include="Honeycomb.OpenTelemetry.CommonInstrumentations" Version="0.27.1-beta" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0-beta.3" />
<PackageReference Include="OpenTelemetry" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.4.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.1" />

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
Expand All @@ -27,7 +27,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ builder.Services.AddInfrastructureServices(builder.Configuration);
/********************************************************************************************************/

//OpenTelemetry
const string serviceName = "{{.MicroServiceName}}";
builder.Services.AddCustomOpenTelemetry(serviceName);
builder.Logging.AddCustomOpenTelemetryLogging(serviceName);
//Honeycomb
builder.Services.AddHoneycomb(builder.Configuration);
builder.Services.AddCustomOpenTelemetry(builder.Configuration);
builder.Logging.AddCustomOpenTelemetryLogging(builder.Configuration);

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"ConnectionStrings": {
"ConnectionString": "Data Source=localhost,1433;Initial Catalog={{.MicroServiceName }};User Id=sa;Password=Test@123;TrustServerCertificate=True;"
},
"Honeycomb": {
"Otel": {
"ServiceName": "{{.MicroServiceName}}",
"ApiKey": ""
"OtlpEndpoint": "http://quality-trace-otelcollector.demoagent.optimizor.app"
}
}

0 comments on commit 7f53981

Please sign in to comment.