-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added OpenTelemetryEnvironment #141
Conversation
This method configure with default environmental variables definitions Fixes serilog#12
Is there a test? |
I will add later |
Thanks for kicking this off, @AlbertoMonteiro! A few initial thoughts -
Super keen to see this work smoothly, let me know if I can clarify any of the above, or unblock anything that holds up the works :-) |
Hey @nblumhardt tyvm for your review.
I also would like to know the best way to test the I also didn't understand entirely the stuff around the path |
Thanks for the updates @AlbertoMonteiro! I need to give a little more thought to how this should fit in with the existing For example, we could avoid the need for an additional configuration method if the existing configuration methods supported an public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerSinkConfiguration,
string endpoint = OpenTelemetrySinkOptions.DefaultEndpoint,
OtlpProtocol protocol = OpenTelemetrySinkOptions.DefaultProtocol,
IDictionary<string, string>? headers = null,
IDictionary<string, object>? resourceAttributes = null,
IncludedData? includedData = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
- LoggingLevelSwitch? levelSwitch = null)
+ LoggingLevelSwitch? levelSwitch = null,
+ bool includeEnvironment = false)
public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerSinkConfiguration,
- Action<OpenTelemetrySinkOptions> configure)
+ Action<OpenTelemetrySinkOptions> configure,
+ bool includeEnvironment = false) (The default might end up being Then, where the sink is configured, public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerSinkConfiguration,
- Action<OpenTelemetrySinkOptions> configure)
+ Action<OpenTelemetrySinkOptions> configure,
+ bool includeEnvironment = false)
{
if (configure == null) throw new ArgumentNullException(nameof(configure));
var options = new OpenTelemetrySinkOptions();
configure(options);
+ if (includeEnvironment)
+ {
+ OpenTelemetryEnvironment.Configure(options);
+ }
var exporter = Exporter.Create( We could also then aim to line up the defaults specified for the existing configuration method with the (I assume) OpenTelemetry-specified defaults. Any thoughts? |
…metry with the option to override config with env var values
@nblumhardt thanks for the detailed answer. I like the idea of using the parameter to use env var values. So I just added a new commit with that change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks for making the change. I've run through and reviewed line-by-line, if any of the comments are unclear please just let me know.
src/Serilog.Sinks.OpenTelemetry/Helpers/OpenTelemetryEnvironment.cs
Outdated
Show resolved
Hide resolved
{ | ||
options.Endpoint = Endpoint; | ||
options.Headers = Headers; | ||
options.ResourceAttributes = ResourceAttributes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should only be applied when the environment variables are present. When no environment variables are set, the configure method shouldn't do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support for OTEL_EXPORTER_OTLP_PROTOCOL
will be needed in order for this to work with both supported protocols.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When OTEL_EXPORTER_OTLP_ENDPOINT
is provided, and the protocol is determined to be http/protobuf, if the value doesn't end with /v1/logs
it will be necessary to tack this on. (In a near-future version of the sink this will be fixed, but some more pieces need to come together around it.)
if (part.Split('=') is { Length: 2 } parts) | ||
configs.Add(parts[0], parts[1]); | ||
else | ||
throw new InvalidOperationException($"Invalid header format: {part}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: exception messages should end with .
, although it's a bit awkward here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the header name to make debugging easier.
About the exception here, since the value can come from manual creation, I think that being able to know when the value is not in the expected format can help to fix it asap.
But if you still think that it isn't necessary here, I can remove it.
src/Serilog.Sinks.OpenTelemetry/Helpers/OpenTelemetryEnvironment.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Sinks.OpenTelemetry/Helpers/OpenTelemetryEnvironment.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Sinks.OpenTelemetry/Helpers/OpenTelemetryEnvironment.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Sinks.OpenTelemetry/Helpers/OpenTelemetryEnvironment.cs
Outdated
Show resolved
Hide resolved
src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs
Outdated
Show resolved
Hide resolved
var resourceAttributes = "name1=1,name2=2"; | ||
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint); | ||
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", headers); | ||
Environment.SetEnvironmentVariable("OTEL_RESOURCE_ATTRIBUTES", resourceAttributes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the environment variables directly will cause issues if the tests rely on OTLP logging at some point in the future, and will cause tests to interfere with each other when running concurrently.
Perhaps OpenTelemetryEnvironment
could have an internal Configure(options, endpointVariable, headersVariable, resourceAttributesVariable)
method added, so that test can call it without setting the actual environment variables?
The "real" configure method could then just be a simple, untested:
public static void Configure(BatchedOpenTelemetrySinkOptions options)
{
Configure(
options,
Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"),
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS"), ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the advice!
I've fixed it in another way, the Configure method receives the function that can get the env var value, so in the test, I can provide a mock function.
src/Serilog.Sinks.OpenTelemetry/OpenTelemetryLoggerConfigurationExtensions.cs
Outdated
Show resolved
Hide resolved
Hey @nblumhardt I've pushed the fixes! Let me know if still need some changes |
Looks great, thank you @AlbertoMonteiro 🎉 |
I'll update the version of the package to 4.0.0-dev-*, since the added parameter is a binary breaking change (and reading variables by default is a breaking behavioral one). |
Thanks for your support @nblumhardt ! |
I think this PR is welcome but has missed the mark a little. It only caters for the default OTEL env vars and misses the signal specific ones for logs. These are OTEL_EXPORTER_OTLP_LOGS_ENDPOINT="http://localhost:5341/ingest/otlp/v1/logs" If i want my metrics and traces to go to the defaults but i want the logs to go to Seq I can not do this at the LOG level. |
This method configure with default environmental variables definitions
Fixes #12