Skip to content

Commit

Permalink
OpenTelemetry configuration improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
GinoCanessa committed Aug 29, 2024
1 parent 02eb763 commit a549108
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 36 deletions.
71 changes: 68 additions & 3 deletions src/FhirStore.Common/Configuration/CandleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,19 +646,72 @@ public class CandleConfig
},
};


[ConfigOption(
ArgName = "--otel-otlp-endpoint",
EnvName = "OTEL_EXPORTER_OTLP_ENDPOINT",
Description = "Enables OpenTelemetry and sends via OLTP to the specified endpoint")]
Description = "Enables OpenTelemetry and sends traces, metrics, and logs via OLTP to the specified endpoint")]
public string? OpenTelemetryEndpoint { get; set; } = null;

private static ConfigurationOption OpenTelemetryEndpointParameter { get; } = new()
{
Name = "OpenTelemetryProtocolEndpoint",
EnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT",
DefaultValue = string.Empty,
CliOption = new System.CommandLine.Option<string>("--otel-otlp-endpoint", "Enables OpenTelemetry and sends via OLTP to the specified endpoint")
CliOption = new System.CommandLine.Option<string>("--otel-otlp-endpoint", "Enables OpenTelemetry and sends traces, metrics, and logs via OLTP to the specified endpoint")
{
Arity = System.CommandLine.ArgumentArity.ZeroOrOne,
IsRequired = false,
},
};

[ConfigOption(
ArgName = "--otel-otlp-traces-endpoint",
EnvName = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
Description = "Enables OpenTelemetry and sends traces via OLTP to the specified endpoint")]
public string? OpenTelemetryTracesEndpoint { get; set; } = null;

private static ConfigurationOption OpenTelemetryTracesEndpointParameter { get; } = new()
{
Name = "OpenTelemetryProtocolTracesEndpoint",
EnvVarName = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
DefaultValue = string.Empty,
CliOption = new System.CommandLine.Option<string>("--otel-otlp-traces-endpoint", "Enables OpenTelemetry and sends traces via OLTP to the specified endpoint")
{
Arity = System.CommandLine.ArgumentArity.ZeroOrOne,
IsRequired = false,
},
};

[ConfigOption(
ArgName = "--otel-otlp-metrics-endpoint",
EnvName = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
Description = "Enables OpenTelemetry and sends metrics via OLTP to the specified endpoint")]
public string? OpenTelemetryMetricsEndpoint { get; set; } = null;

private static ConfigurationOption OpenTelemetryMetricsEndpointParameter { get; } = new()
{
Name = "OpenTelemetryProtocolMetricsEndpoint",
EnvVarName = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
DefaultValue = string.Empty,
CliOption = new System.CommandLine.Option<string>("--otel-otlp-metrics-endpoint", "Enables OpenTelemetry and sends metrics via OLTP to the specified endpoint")
{
Arity = System.CommandLine.ArgumentArity.ZeroOrOne,
IsRequired = false,
},
};

[ConfigOption(
ArgName = "--otel-otlp-logs-endpoint",
EnvName = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
Description = "Enables OpenTelemetry and sends logs via OLTP to the specified endpoint")]
public string? OpenTelemetryLogsEndpoint { get; set; } = null;

private static ConfigurationOption OpenTelemetryLogsEndpointParameter { get; } = new()
{
Name = "OpenTelemetryProtocolLogsEndpoint",
EnvVarName = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
DefaultValue = string.Empty,
CliOption = new System.CommandLine.Option<string>("--otel-otlp-logs-endpoint", "Enables OpenTelemetry and sends logs via OLTP to the specified endpoint")
{
Arity = System.CommandLine.ArgumentArity.ZeroOrOne,
IsRequired = false,
Expand Down Expand Up @@ -700,6 +753,9 @@ public class CandleConfig
SmtpPasswordParameter,
FhirPathLabUrlParameter,
OpenTelemetryEndpointParameter,
OpenTelemetryTracesEndpointParameter,
OpenTelemetryMetricsEndpointParameter,
OpenTelemetryLogsEndpointParameter,
];
/// <summary>Parses the given parse result.</summary>
Expand Down Expand Up @@ -812,6 +868,15 @@ public virtual void Parse(System.CommandLine.Parsing.ParseResult parseResult)
case "OpenTelemetryProtocolEndpoint":
OpenTelemetryEndpoint = GetOpt(parseResult, opt.CliOption, OpenTelemetryEndpoint);
break;
case "OpenTelemetryProtocolTracesEndpoint":
OpenTelemetryTracesEndpoint = GetOpt(parseResult, opt.CliOption, OpenTelemetryTracesEndpoint);
break;
case "OpenTelemetryProtocolMetricsEndpoint":
OpenTelemetryMetricsEndpoint = GetOpt(parseResult, opt.CliOption, OpenTelemetryMetricsEndpoint);
break;
case "OpenTelemetryProtocolLogsEndpoint":
OpenTelemetryLogsEndpoint = GetOpt(parseResult, opt.CliOption, OpenTelemetryLogsEndpoint);
break;
}
}
}
Expand Down
120 changes: 87 additions & 33 deletions src/fhir-candle/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,39 +177,8 @@ public static async Task<int> RunServer(SCL.Parsing.ParseResult pr, Cancellation

builder.Services.AddCors();

// setup OpenTelemetry if it is enable
if (!string.IsNullOrEmpty(config.OpenTelemetryEndpoint))
{
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService("fhir-candle"))
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(config.OpenTelemetryEndpoint);
});
});

builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("fhir-candle"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(config.OpenTelemetryEndpoint);
}))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(config.OpenTelemetryEndpoint);
}));
}
// setup open telemetry if necessary
ConfigureOpenTelemetry(config, builder);

// add our configuration
builder.Services.AddSingleton(config);
Expand Down Expand Up @@ -323,6 +292,91 @@ public static async Task<int> RunServer(SCL.Parsing.ParseResult pr, Cancellation
}
}

/// <summary>
/// Configures OpenTelemetry for the application.
/// </summary>
/// <param name="config">The CandleConfig object containing the configuration settings.</param>
/// <param name="builder">The WebApplicationBuilder used to configure the application.</param>
private static void ConfigureOpenTelemetry(CandleConfig config, WebApplicationBuilder builder)
{
string? traceEndpoint = !string.IsNullOrEmpty(config.OpenTelemetryTracesEndpoint)
? config.OpenTelemetryTracesEndpoint
: !string.IsNullOrEmpty(config.OpenTelemetryEndpoint)
? config.OpenTelemetryEndpoint
: null;

string? metricsEndpoint = !string.IsNullOrEmpty(config.OpenTelemetryMetricsEndpoint)
? config.OpenTelemetryMetricsEndpoint
: !string.IsNullOrEmpty(config.OpenTelemetryEndpoint)
? config.OpenTelemetryEndpoint
: null;

string? logsEndpoint = !string.IsNullOrEmpty(config.OpenTelemetryLogsEndpoint)
? config.OpenTelemetryLogsEndpoint
: !string.IsNullOrEmpty(config.OpenTelemetryEndpoint)
? config.OpenTelemetryEndpoint
: null;

if (logsEndpoint != null)
{
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService("fhir-candle"))
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(logsEndpoint);
});
});
}

if ((traceEndpoint != null) && (metricsEndpoint != null))
{
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("fhir-candle"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(traceEndpoint);
}))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(metricsEndpoint);
}));
}
else if (traceEndpoint != null)
{
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("fhir-candle"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(traceEndpoint);
}));
}
else if (metricsEndpoint != null)
{
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("fhir-candle"))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(metricsEndpoint);
}));
}
}

/// <summary>After server start.</summary>
/// <param name="app"> The application.</param>
Expand Down

0 comments on commit a549108

Please sign in to comment.