serilog-sinks-opentelemetry
serilog-sinks-opentelemetry copied to clipboard
Add a ConfigurationMethod to Populate ResourceAttributes-dictionary from appsettings.json
When using new LoggerConfiguration().ReadFrom.Configuration(config)
I would like to be able to assign values to ResourceAttributes: I'm thinking something like
"WriteTo": [
{
"Name": "OpenTelemetry",
"Args" : {
"endpoint" : "http://localhost:4317",
"protocol": "Grpc",
"resourceAttributes": {
"service.name": "my choice of ServiceName in stead of 'unknown service: executable.exe'."
}
}
}
]
Of course that doesn't work without a little help. I pulled the source and built it into the existing ConfigurationExtension like so
public static LoggerConfiguration OpenTelemetry(
this LoggerSinkConfiguration loggerSinkConfiguration,
string endpoint = OpenTelemetrySinkOptions.DefaultEndpoint,
OtlpProtocol protocol = OpenTelemetrySinkOptions.DefaultProtocol,
IDictionary<string,object>? resourceAttributes = null)
{
if (loggerSinkConfiguration == null) throw new ArgumentNullException(nameof(loggerSinkConfiguration));
return loggerSinkConfiguration.OpenTelemetry(options =>
{
options.Endpoint = endpoint;
options.Protocol = protocol;
if (resourceAttributes is not null)
options.ResourceAttributes = resourceAttributes!;
});
}
It works but is sort of hard to unittest without resorting to reflection. Could the above be integrated into the project? Or is there a better way to do what I propose?