Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Serilog with OpenTelemetry?

Is it possible to use Serilog with OpenTelemetry?

https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/getting-started/README.md

like image 357
Xavier John Avatar asked May 14 '21 20:05

Xavier John


1 Answers

One approach you might consider if you're running apps in Docker or Kubernetes is to just make Serilog log to console (as per 12 factor logging guidelines). From there the console logs can get received by a collector/forwarder agent such as the OpenTelemetry collector agent (or others like Logstash of Fluentd) and exported to a destination solution (e.g. Splunk or Elasticsearch). Some ways for a collector/forwarder agent to receive console logs include:

  • tail and parse container logs that are persisted on the hosts
  • docker logging provider
  • etc.

A lot of the collector/forwarder agents like the ones mentioned have the ability to transform and enrich log messages on their way through e.g. you could reshape log messages to the Open Telemetry log data model described here.

Actually I've played around with the OpenTelemetry collector agent and it does some out of the box transformations e.g. a log message of "test" received by the OpenTelemetry collector agent via the filelog receiver ends up looking like the following after it gets exported (that is, without any processing/transformation on the way through):

{
  "time_unix_nano": 1637538007545231018,
  "body": {
    "Value": {
      "string_value": "test"
    }
  },
  "attributes": [
    {
      "key": "file.name",
      "value": {
        "Value": {
          "string_value": "filename.log"
        }
      }
    }
  ],
  "trace_id": "",
  "span_id": ""
}

I suppose if you were to log as JSON in Serilog and enrich the logs with trace & span IDs using a Serilog enricher similar to this one (disclaimer this is a plugin I wrote a few years back when things were still known as OpenTracing), you could potentially do some transformation in the collector/forwarder agent to get the trace & span IDs populated correctly as per the defined OTEL log data model.


If you don't want to do that approach outlined above (which might be overly complex for simple scenarios) you could do some custom output formatting - see here for formatting output in Serilog, and send the logs directly to your sink...

like image 162
Ryan.Bartsch Avatar answered Nov 16 '22 23:11

Ryan.Bartsch