Module: Fluent::Plugin::MarginaliaExtractor

Included in:
Marginalia, PgStatActivityInput
Defined in:
lib/fluent/plugin/marginalia_extractor.rb

Overview

MarginaliaExtractor provides the parse_marginalia_into_record utility method, useful for extracting marginalia into fluentd records

Constant Summary collapse

MARGINALIA_PREPENDED_REGEXP =
%r{^(?<comment>/\*.*?\*/)\s*(?<sql>.*)}m
MARGINALIA_APPENDED_REGEXP =
%r{(?<sql>.*)(?<comment>/\*.*\*/)\s*;?\s*$}m

Instance Method Summary collapse

Instance Method Details

#extract_entries(comment) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 35

def extract_entries(comment)
  comment = scrub_comment(comment)

  return [] unless comment

  comment.split(',')
end

#match_marginalia_comment(sql) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 27

def match_marginalia_comment(sql)
  matched = MARGINALIA_PREPENDED_REGEXP.match(sql)

  return matched if matched

  MARGINALIA_APPENDED_REGEXP.match(sql)
end

#parse_entries(entries, key, record) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 51

def parse_entries(entries, key, record)
  entries.each do |component|
    data = component.split(':', 2)

    break unless data.length == 2

    stored_key = store_key(record, key, data[0])
    record[stored_key] = data[1]
  end
end

#parse_marginalia_into_record(record, key, strip_comment) ⇒ Object

Injects marginalia into a fluentd record



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 13

def parse_marginalia_into_record(record, key, strip_comment)
  sql = record[key]
  return unless sql

  comment_match = match_marginalia_comment(sql)

  return unless comment_match

  entries = extract_entries(comment_match['comment'])
  parse_entries(entries, key, record)

  record[key] = comment_match['sql'].strip if strip_comment
end

#scrub_comment(comment) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 43

def scrub_comment(comment)
  return unless comment

  comment.strip!
  comment.gsub!(%r{^/\*}, '')
  comment.gsub!(%r{\*/$}, '')
end

#store_key(record, key, component_key) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin/marginalia_extractor.rb', line 62

def store_key(record, key, component_key)
  # In case there is a conflict with the Marginalia key
  # (e.g. `correlation_id`), we use the base key
  # (`sql_correlation_id`) instead.
  if record.key?(component_key)
    "#{key}_#{component_key}"
  else
    component_key
  end
end