Skip to content

Commit

Permalink
fix tests and add helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Jan 10, 2025
1 parent 9592c43 commit 8a81c22
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
}
Expand All @@ -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<OpenTelemetrySinkOptions> ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) =>
options =>
{
options.Endpoint = endpoint;
options.Protocol = protocol;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
}
Expand All @@ -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<OpenTelemetrySinkOptions> ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) =>
options =>
{
options.Endpoint = endpoint;
options.Protocol = protocol;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
}
Expand All @@ -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<OpenTelemetrySinkOptions> ConfigureOtlpSink(string endpoint, OtlpProtocol protocol) =>
options =>
{
options.Endpoint = endpoint;
options.Protocol = protocol;
};
}

0 comments on commit 8a81c22

Please sign in to comment.