From 8a81c2201f7e2bb434f0327c5b094bf26561e7bb Mon Sep 17 00:00:00 2001 From: Are Almaas Date: Fri, 10 Jan 2025 11:57:03 +0100 Subject: [PATCH] fix tests and add helper functions --- .../Extensions/OpenTelemetryExtensions.cs | 66 ++++++++++--------- .../Extensions/OpenTelemetryExtensions.cs | 66 ++++++++++--------- .../Extensions/OpenTelemetryExtensions.cs | 66 ++++++++++--------- 3 files changed, 105 insertions(+), 93 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.GraphQL/Common/Extensions/OpenTelemetryExtensions.cs b/src/Digdir.Domain.Dialogporten.GraphQL/Common/Extensions/OpenTelemetryExtensions.cs index d69cec673..1a8901b31 100644 --- a/src/Digdir.Domain.Dialogporten.GraphQL/Common/Extensions/OpenTelemetryExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.GraphQL/Common/Extensions/OpenTelemetryExtensions.cs @@ -107,23 +107,13 @@ public static LoggerConfiguration OpenTelemetryOrConsole(this LoggerSinkConfigur { var otelEndpoint = context.Configuration[OtelExporterOtlpEndpoint]; var otelProtocol = context.Configuration[OtelExporterOtlpProtocol]; + return otelEndpoint switch { - null when context.HostingEnvironment.IsDevelopment() => writeTo.Console(formatProvider: CultureInfo.InvariantCulture), - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => throw new InvalidOperationException($"Invalid otel protocol: {otelProtocol}") - }, + null => + writeTo.Console(formatProvider: CultureInfo.InvariantCulture), + not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => + writeTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, ParseOtlpProtocol(otelProtocol))), _ => throw new InvalidOperationException($"Invalid otel endpoint: {otelEndpoint}") }; } @@ -132,23 +122,37 @@ public static LoggerConfiguration TryWriteToOpenTelemetry(this LoggerConfigurati { var otelEndpoint = Environment.GetEnvironmentVariable(OtelExporterOtlpEndpoint); var otelProtocol = Environment.GetEnvironmentVariable(OtelExporterOtlpProtocol); - return otelEndpoint switch + + if (otelEndpoint is null || !Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute)) { - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => config - }, - _ => config + return config; + } + + try + { + var protocol = ParseOtlpProtocol(otelProtocol); + return config.WriteTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, protocol)); + } + catch (ArgumentException) + { + return config; + } + } + + private static OtlpProtocol ParseOtlpProtocol(string? protocol) + { + return protocol?.ToLowerInvariant() switch + { + "grpc" => OtlpProtocol.Grpc, + "http/protobuf" => OtlpProtocol.HttpProtobuf, + _ => throw new ArgumentException($"Unsupported OTLP protocol: {protocol}") }; } + + private static Action ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) => + options => + { + options.Endpoint = endpoint; + options.Protocol = protocol; + }; } diff --git a/src/Digdir.Domain.Dialogporten.Service/Common/Extensions/OpenTelemetryExtensions.cs b/src/Digdir.Domain.Dialogporten.Service/Common/Extensions/OpenTelemetryExtensions.cs index a43bf98d5..d91128848 100644 --- a/src/Digdir.Domain.Dialogporten.Service/Common/Extensions/OpenTelemetryExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.Service/Common/Extensions/OpenTelemetryExtensions.cs @@ -104,23 +104,13 @@ public static LoggerConfiguration OpenTelemetryOrConsole(this LoggerSinkConfigur { var otelEndpoint = context.Configuration[OtelExporterOtlpEndpoint]; var otelProtocol = context.Configuration[OtelExporterOtlpProtocol]; + return otelEndpoint switch { - null when context.HostingEnvironment.IsDevelopment() => writeTo.Console(formatProvider: CultureInfo.InvariantCulture), - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => throw new InvalidOperationException($"Invalid otel protocol: {otelProtocol}") - }, + null => + writeTo.Console(formatProvider: CultureInfo.InvariantCulture), + not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => + writeTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, ParseOtlpProtocol(otelProtocol))), _ => throw new InvalidOperationException($"Invalid otel endpoint: {otelEndpoint}") }; } @@ -129,23 +119,37 @@ public static LoggerConfiguration TryWriteToOpenTelemetry(this LoggerConfigurati { var otelEndpoint = Environment.GetEnvironmentVariable(OtelExporterOtlpEndpoint); var otelProtocol = Environment.GetEnvironmentVariable(OtelExporterOtlpProtocol); - return otelEndpoint switch + + if (otelEndpoint is null || !Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute)) { - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => config - }, - _ => config + return config; + } + + try + { + var protocol = ParseOtlpProtocol(otelProtocol); + return config.WriteTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, protocol)); + } + catch (ArgumentException) + { + return config; + } + } + + private static OtlpProtocol ParseOtlpProtocol(string? protocol) + { + return protocol?.ToLowerInvariant() switch + { + "grpc" => OtlpProtocol.Grpc, + "http/protobuf" => OtlpProtocol.HttpProtobuf, + _ => throw new ArgumentException($"Unsupported OTLP protocol: {protocol}") }; } + + private static Action ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) => + options => + { + options.Endpoint = endpoint; + options.Protocol = protocol; + }; } diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/OpenTelemetryExtensions.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/OpenTelemetryExtensions.cs index 78c6a6cb4..a81c8bb54 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/OpenTelemetryExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/OpenTelemetryExtensions.cs @@ -105,23 +105,13 @@ public static LoggerConfiguration OpenTelemetryOrConsole(this LoggerSinkConfigur { var otelEndpoint = context.Configuration[OtelExporterOtlpEndpoint]; var otelProtocol = context.Configuration[OtelExporterOtlpProtocol]; + return otelEndpoint switch { - null when context.HostingEnvironment.IsDevelopment() => writeTo.Console(formatProvider: CultureInfo.InvariantCulture), - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => writeTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => throw new InvalidOperationException($"Invalid otel protocol: {otelProtocol}") - }, + null => + writeTo.Console(formatProvider: CultureInfo.InvariantCulture), + not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => + writeTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, ParseOtlpProtocol(otelProtocol))), _ => throw new InvalidOperationException($"Invalid otel endpoint: {otelEndpoint}") }; } @@ -130,23 +120,37 @@ public static LoggerConfiguration TryWriteToOpenTelemetry(this LoggerConfigurati { var otelEndpoint = Environment.GetEnvironmentVariable(OtelExporterOtlpEndpoint); var otelProtocol = Environment.GetEnvironmentVariable(OtelExporterOtlpProtocol); - return otelEndpoint switch + + if (otelEndpoint is null || !Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute)) { - not null when Uri.IsWellFormedUriString(otelEndpoint, UriKind.Absolute) => otelProtocol?.ToLowerInvariant() switch - { - "grpc" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.Grpc; - }), - "http/protobuf" => config.WriteTo.OpenTelemetry(options => - { - options.Endpoint = otelEndpoint; - options.Protocol = OtlpProtocol.HttpProtobuf; - }), - _ => config - }, - _ => config + return config; + } + + try + { + var protocol = ParseOtlpProtocol(otelProtocol); + return config.WriteTo.OpenTelemetry(ConfigureOtlpSink(otelEndpoint, protocol)); + } + catch (ArgumentException) + { + return config; + } + } + + private static OtlpProtocol ParseOtlpProtocol(string? protocol) + { + return protocol?.ToLowerInvariant() switch + { + "grpc" => OtlpProtocol.Grpc, + "http/protobuf" => OtlpProtocol.HttpProtobuf, + _ => throw new ArgumentException($"Unsupported OTLP protocol: {protocol}") }; } + + private static Action ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) => + options => + { + options.Endpoint = endpoint; + options.Protocol = protocol; + }; }