contextualized_logs 0.0.2.pre.alpha → 0.0.3.pre.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d2a30eaa931eb9013ce4e8007229c24f71150d89d7399372820eb33e1e72deb
4
- data.tar.gz: 1b7a809fb3ea83334a7248a97a63c9728b899400c8e5869d7be28a55379fc832
3
+ metadata.gz: c8025983ccba69c3acd120ac1e5f37142f658d3139353d42757435d2b99ecd9c
4
+ data.tar.gz: 2be16ecff77a93ee9460dea3e95a76c195f1a1db4f5994c950c5d0b2ad11e90c
5
5
  SHA512:
6
- metadata.gz: d1543991e0904103f8bd6dbf0fc791fa6fe6f8302aea6e49049ae49adbf35d352d5d226fc7075f48bbc956b9d7a2b7f7f136e0b9e980cd00a03be47658070552
7
- data.tar.gz: 99b5866d81903a8bb4e7e3231acdd15931de535bb8297a2a6bd60c9c5ec9d7b397194b0f44f1f7e48ce9554962ab3a2174815033d736f6d4392d6ba89f4f517b
6
+ metadata.gz: 01f9f4e901e80a95c1ce9f04aef20f6438261af9e9ac17fb8e29ee63e3341de5da50189dd3353625281a28fb53271f96d9dd360ec675b75e1c3072687f24440a
7
+ data.tar.gz: 4a95d1831a58274b5e094e64f1bedc88584ba774d073831cdd08d6b457d9216f51f1cc0e199e4a2f47d46a3b0a0e1a5e7d3e38ed064a9937a3a4f249a7955901
@@ -59,4 +59,5 @@ Rails.application.configure do
59
59
  # routes, locales, etc. This feature depends on the listen gem.
60
60
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
61
61
  config.logger = ContextualizedLogs::ContextualizedLogger.new("log/#{Rails.env}.log")
62
+ config.logger.level = :info
62
63
  end
@@ -8,13 +8,16 @@ module ContextualizedLogs
8
8
  log = JSON.parse(log)
9
9
  # set log <> APM trace correlation
10
10
  datadog_correlation = Datadog.tracer.active_correlation
11
- log.merge(
11
+ log.merge!(
12
12
  dd: {
13
13
  trace_id: datadog_correlation.trace_id,
14
14
  span_id: datadog_correlation.span_id
15
15
  },
16
16
  ddsource: ['ruby']
17
- ).to_json + "\n"
17
+ )
18
+ log.to_json + "\n"
19
+ # for local debug, dump log in JSON pretty format
20
+ # JSON.pretty_generate(log) + "\n"
18
21
  end
19
22
  config.controller_default_contextualizer = proc do |controller|
20
23
  ContextualizedController.default_contextualize_request(controller)
@@ -0,0 +1,67 @@
1
+ return if Rails.env.test?
2
+
3
+ require 'lograge/sql/extension'
4
+ require 'json'
5
+
6
+ # pretty
7
+ module Lograge
8
+ module Formatters
9
+ class PrettyJson
10
+ def call(data)
11
+ JSON.pretty_generate(data)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ Rails.application.configure do
19
+ # Lograge (format log for datadog)
20
+ # https://docs.datadoghq.com/logs/log_collection/ruby/
21
+ # Lograge config
22
+ config.lograge.enabled = true
23
+ config.colorize_logging = false
24
+ # We are asking here to log in RAW (which are actually ruby hashes). The Ruby logging is going to take care of the JSON formatting.
25
+ config.lograge.formatter = Lograge::Formatters::Json.new
26
+ # for local debug, dump log in JSON pretty format
27
+ # config.lograge.formatter = Lograge::Formatters::PrettyJson.new
28
+ # keep existing log
29
+ config.lograge.keep_original_rails_log = false
30
+ # issue with existing rails logger and prefixing.. logging to different file
31
+ # Logger::SimpleFormatter?
32
+ config.lograge.logger = ActiveSupport::Logger.new("#{Rails.root}/log/#{Rails.env}.log")
33
+
34
+ config.lograge.custom_options = lambda do |event|
35
+ data = {}
36
+ if (exception = event.payload[:exception_object])
37
+ data = {
38
+ # datadog naming convention
39
+ # https://docs.datadoghq.com/logs/processing/attributes_naming_convention/#source-code
40
+ error: {
41
+ message: exception.message,
42
+ kind: exception.class.to_s,
43
+ stack: (exception.backtrace || []).join("; ")
44
+ }
45
+ }
46
+ end
47
+ correlation = Datadog.tracer.active_correlation
48
+ data.deep_merge!(
49
+ # Adds IDs as tags to log output
50
+ # trace injection to correlation APM with Rails logs
51
+ # https://docs.datadoghq.com/tracing/advanced/connect_logs_and_traces/?tab=ruby
52
+ log_type: 'request',
53
+ dd: {
54
+ trace_id: correlation.trace_id,
55
+ span_id: correlation.span_id
56
+ },
57
+ ddsource: ['ruby'],
58
+ syslog: { env: Rails.env, host: Socket.gethostname },
59
+ params: event.payload[:params].except(*Rails.application.config.filter_parameters) # ⚠️⚠️⚠️ check `config/initializers/filter_parameter_logging.rb`
60
+ )
61
+ data.deep_merge!(ContextualizedLogs.current_context.context) # merge current request context
62
+
63
+ # add all job's id (not merged by current context, add it only to rquest log)
64
+ data[:jobs] = ContextualizedLogs.current_context.enqueued_jobs_ids unless ContextualizedLogs.current_context.enqueued_jobs_ids.nil?
65
+ data
66
+ end
67
+ end
Binary file
data/db/test.sqlite3 CHANGED
Binary file
@@ -17,6 +17,10 @@ module ContextualizedLogs
17
17
  @config || Config.default
18
18
  end
19
19
 
20
+ def current_context
21
+ config.current_context
22
+ end
23
+
20
24
  def configure(&block)
21
25
  config = Config.default
22
26
  block.call(config)
@@ -13,26 +13,18 @@ module ContextualizedLogs
13
13
  def current_context
14
14
  @current_context || ContextualizedLogs.config.current_context
15
15
  end
16
-
16
+
17
17
  def default_formatter
18
18
  proc do |severity, timestamp, progname, msg|
19
- # format (and enrich) log in JSON format (-> )
20
- # https://docs.hq.com/logs/processing/attributes_naming_convention/#source-code
21
- # correlation = Datadog.tracer.active_correlation
22
19
  data = {
23
- # dd: {
24
- # trace_id: correlation.trace_id,
25
- # span_id: correlation.span_id
26
- # },
27
- # ddsource: ['ruby'],
28
20
  syslog: { env: Rails.env, host: Socket.gethostname },
29
21
  type: severity.to_s,
30
22
  time: timestamp
31
23
  }
32
- data[:stack] = Kernel.caller.
33
- map { |caller| caller.gsub(/#{Rails.root}/, '') }.
34
- reject { |caller| caller.start_with?('/usr/local') || caller.include?('/shared/bundle/') || caller.start_with?('/Users/') }.
35
- first(15)
24
+ # data[:stack] = Kernel.caller.
25
+ # map { |caller| caller.gsub(/#{Rails.root}/, '') }.
26
+ # reject { |caller| caller.start_with?('/usr/local') || caller.include?('/shared/bundle/') || caller.start_with?('/Users/') }.
27
+ # first(15)
36
28
  data[:log_type] = 'log'
37
29
  data.merge!(parse_msg(msg)) # parse message (string, hash, error, ...)
38
30
  data.merge!(current_context.context) # merge current request context
@@ -26,7 +26,7 @@ module ContextualizedLogs
26
26
  if worker_klass.contextualize_worker_enabled
27
27
  job['context'] = current_context.to_json
28
28
  Rails.logger.info "sidekiq: enqueing job #{worker_class}: #{job['jid']}, on queue: #{queue}"
29
- Rails.logger.dump('Injecting context', JSON.parse(current_context.to_json), :debug)
29
+ # Rails.logger.dump('Injecting context', JSON.parse(current_context.to_json), :debug)
30
30
  end
31
31
  end
32
32
  yield
@@ -1,3 +1,3 @@
1
1
  module ContextualizedLogs
2
- VERSION = '0.0.2-alpha'
2
+ VERSION = '0.0.3-alpha'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contextualized_logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.pre.alpha
4
+ version: 0.0.3.pre.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugues Bernet-Rollande
@@ -74,6 +74,7 @@ files:
74
74
  - config/initializers/datadog.rb
75
75
  - config/initializers/filter_parameter_logging.rb
76
76
  - config/initializers/inflections.rb
77
+ - config/initializers/lograge.rb
77
78
  - config/initializers/mime_types.rb
78
79
  - config/initializers/sidekiq.rb
79
80
  - config/initializers/wrap_parameters.rb