Skip to content

Commit

Permalink
Use OTLP env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Sep 29, 2024
1 parent 0b86e08 commit 833cdb8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 222 deletions.
49 changes: 0 additions & 49 deletions src/DiscordTranslationBot/Telemetry/TelemetryEndpointOptions.cs

This file was deleted.

62 changes: 18 additions & 44 deletions src/DiscordTranslationBot/Telemetry/TelemetryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand All @@ -10,12 +9,21 @@ internal static class TelemetryExtensions
{
public static void AddTelemetry(this WebApplicationBuilder builder)
{
var section = builder.Configuration.GetSection(TelemetryOptions.SectionName);
builder.Services.AddOptions<TelemetryOptions>().Bind(section).ValidateDataAnnotations().ValidateOnStart();
var options = builder.Configuration.GetSection(TelemetryOptions.SectionName).Get<TelemetryOptions>();
if (options?.Enabled != true)
{
return;
}

var options = section.Get<TelemetryOptions>();
builder.Logging.AddOpenTelemetry(
o =>
{
o.IncludeFormattedMessage = true;
o.IncludeScopes = true;
o.AddOtlpExporter();
});

var openTelemetryBuilder = builder
builder
.Services
.AddOpenTelemetry()
.ConfigureResource(
Expand All @@ -25,48 +33,14 @@ public static void AddTelemetry(this WebApplicationBuilder builder)
new Dictionary<string, object>
{
["deployment.environment"] = builder.Environment.EnvironmentName
}));

if (options?.MetricsEndpoint.Enabled == true)
{
openTelemetryBuilder.WithMetrics(
}))
.WithMetrics(
b => b
.AddProcessInstrumentation()
.AddRuntimeInstrumentation()
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(e => SetOltpExporterOptions(e, options.MetricsEndpoint)));
}

if (options?.TracingEndpoint.Enabled == true)
{
openTelemetryBuilder.WithTracing(
b => b
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(e => SetOltpExporterOptions(e, options.TracingEndpoint)));
}

if (options?.LoggingEndpoint.Enabled == true)
{
builder.Logging.AddOpenTelemetry(
o =>
{
o.IncludeFormattedMessage = true;
o.IncludeScopes = true;
o.AddOtlpExporter(e => SetOltpExporterOptions(e, options.LoggingEndpoint));
});
}
}

private static void SetOltpExporterOptions(
OtlpExporterOptions otlpExporterOptions,
TelemetryEndpointOptions telemetryEndpointOptions)
{
otlpExporterOptions.Protocol = telemetryEndpointOptions.Protocol;
otlpExporterOptions.Endpoint = telemetryEndpointOptions.Url!;
otlpExporterOptions.Headers = string.Join(
';',
telemetryEndpointOptions.Headers.Select(x => $"{x.Key}={x.Value}"));
.AddOtlpExporter())
.WithTracing(b => b.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation().AddOtlpExporter());
}
}
35 changes: 4 additions & 31 deletions src/DiscordTranslationBot/Telemetry/TelemetryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
using System.ComponentModel.DataAnnotations;
using DiscordTranslationBot.Extensions;
namespace DiscordTranslationBot.Telemetry;

namespace DiscordTranslationBot.Telemetry;

public sealed class TelemetryOptions : IValidatableObject
internal sealed class TelemetryOptions
{
/// <summary>
/// Configuration section name for <see cref="TelemetryOptions" />.
/// </summary>
public const string SectionName = "Telemetry";

/// <summary>
/// The endpoint options for metrics.
/// Flag indicating whether telemetry is enabled.
/// </summary>
public TelemetryEndpointOptions MetricsEndpoint { get; init; } = new();

/// <summary>
/// The endpoint options for logging.
/// </summary>
public TelemetryEndpointOptions LoggingEndpoint { get; init; } = new();

/// <summary>
/// The endpoint options for tracing.
/// </summary>
public TelemetryEndpointOptions TracingEndpoint { get; init; } = new();

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
MetricsEndpoint.TryValidate(out var metricsEndpointValidationResults);
LoggingEndpoint.TryValidate(out var loggingEndpointValidationResults);
TracingEndpoint.TryValidate(out var tracingEndpointValidationResults);

return
[
..metricsEndpointValidationResults,
..loggingEndpointValidationResults,
..tracingEndpointValidationResults
];
}
public bool Enabled { get; init; }
}
2 changes: 1 addition & 1 deletion src/DiscordTranslationBot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}

This file was deleted.

0 comments on commit 833cdb8

Please sign in to comment.