reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
321 stars 115 forks source link

Enabling ActionController to hook into metrics #77

Closed maniax89 closed 3 years ago

maniax89 commented 5 years ago

Environment

Provide at least:

Expected Behavior

logger.info(metric: 'foo')

but I don't see it happening for any of the ActionController log messages even after I configured a custom formatter (which I believe happens after the events are triggered, which makes sense)

reidmorrison commented 5 years ago

logger.info(metric: 'foo') is referred to a "metrics only" log event. By design these metrics are not output to human readable text output destinations. For example they are written to the JSON output appender, which is how we forward these metrics only events to Splunk.

All of the built-in destinations are documented: https://rocketjob.github.io/semantic_logger/appenders.html Of course, you can create your own appender for any other destination that you want to send events/metrics to.

The most complete metrics only appender currently implemented is for Signalfx since it supports metrics as well as dimensions. It ignores all log events that do not have metrics.

maniax89 commented 5 years ago

I guess my question is whether or not I can take the existing log subscribers for the action controller that get monkey patched in this library and somehow hook into them and intercept the info/error to add additional information in order to trigger a metric subscriber to fire as well

reidmorrison commented 5 years ago

Using named tags will add any additional data to the action controller log messages. For example:

 config.log_tags = {
    request_id: :request_id,
    ip:         :remote_ip,
    user:       -> request { request.cookie_jar['login'] }
  }

Rails also has a built-in mechanism to add custom data to its payload from any controller by implementing append_info_to_payload. For an example, look for Adding custom data to the Rails Completed log message: https://rocketjob.github.io/semantic_logger/rails.html