-
Notifications
You must be signed in to change notification settings - Fork 112
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
add trace ctx to logger metadata when attached to pdict context #394
Conversation
Codecov Report
@@ Coverage Diff @@
## main #394 +/- ##
==========================================
+ Coverage 73.18% 73.34% +0.16%
==========================================
Files 53 53
Lines 1656 1666 +10
==========================================
+ Hits 1212 1222 +10
Misses 444 444
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Still needs to show an example to at least make clear that you use a list of atoms to access the subkeys in logger formatter: But maybe this is good enough aside from stuff like that I'll do before we merge. |
@tsloughter we also need to clear it from metadata on detach |
@bryannaegele woop, good catch, I made sure setting to |
Updated. Also updated the changelog. |
Coming back from the original discussion, I have deployed this to a staging Elixir project and can confirm trace metadata lines up with logs. |
@btkostner thanks! Would you be willing to give a simple example of your logger config for adding the metadata to your logs? |
@tsloughter So this is an Elixir project sending data to Datadog. For the logger backend, we've been using https://github.com/Nebo15/logger_json with the # config.exs
config :logger,
backends: [LoggerJSON],
level: :info
config :logger_json, :backend,
formatter: LoggerJSON.Formatters.DatadogLogger,
metadata: :all And then we use a custom plug to log Phoenix requests in a single output like so: # endpoint.ex
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug LoggerJSON.Plug, metadata_formatter: LoggerJSON.Plug.MetadataFormatters.DatadogLogger The plug needs to come after {
"domain": ["elixir"],
"duration": 5597270,
"erl_level": "info",
"http": {
"url": "http://localhost/_health",
"status_code": 200,
"method": "GET",
"referer": null,
"request_id": "FvLDEr8jXx2K9E8AAAAB",
"useragent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
"url_details": {
"host": "localhost",
"port": 4000,
"path": "/_health",
"queryString": "",
"scheme": "http"
}
},
"logger": {
"thread_name": "#PID<0.616.0>",
"method_name": "Elixir.LoggerJSON.Plug.call/2",
"file_name": "lib/logger_json/plug.ex",
"line": 44
},
"message": "",
"network": {
"client": {
"ip": "127.0.0.1"
}
},
"otel_span_ctx": {
"span_id": "cd0375b7305d668e",
"trace_flags": "01",
"trace_id": "228ad09ba553e60b365a7485f107d9bb"
},
"phoenix": {
"controller": "Elixir.HealthcheckAggregatorWeb.HealthController",
"action": "index"
},
"request_id": "FvLDEr8jXx2K9E8AAAAB",
"syslog": {
"hostname": "Caprica",
"severity": "info",
"timestamp": "2022-05-26T14:54:58.495Z"
}
} Datadog has a weird formatting issue between otel and internal datadog ids (https://docs.datadoghq.com/tracing/connect_logs_and_traces/opentelemetry/) but I don't consider that an issue here. It's a Datadog feature / support issue. Fighting a bit with a more native setup (standard Phoenix project, console logger config, and plugs), and I wasn't actually able to get it to appear. This is from a log function in the controller and
Tracking it down, seems that it gets ignored because it doesn't have a string implementation: https://github.com/elixir-lang/elixir/blob/main/lib/logger/lib/logger/formatter.ex#L227 |
would be great to see it merged. Will it work with opentelemetry_cowboy out of the box provided I will set up logger format accordingly? |
Ah, it is because the metadata is nested. Elixir's Logger must support nested metadata and it just need to be patched to work even with |
d4160c4
to
1ec99c9
Compare
since Elixir's Logger formatter does not support nested keys for metadata, like Erlang logger formatter does, this patch changes the metadata from #{otel_span_ctx => #{...}} to remove the wrapping `otel_span_ctx`. This also meant changing the example logger format template to have to check for existance of each key individually.
Sweet. Thank you @tsloughter! 🎊 |
No description provided.