Module: FFWD::Plugin::JSON::Connection

Included in:
FrameConnection, LineConnection
Defined in:
lib/ffwd/plugin/json/connection.rb

Constant Summary collapse

EVENT_FIELDS =
[
  ["key", :key],
  ["value", :value],
  ["host", :host],
  ["state", :state],
  ["description", :description],
  ["ttl", :ttl],
  ["tags", :tags],
  ["attributes", :attributes],
]
METRIC_FIELDS =
[
  ["proc", :proc],
  ["key", :key],
  ["value", :value],
  ["tags", :tags],
  ["attributes", :attributes]
]

Instance Method Summary collapse

Instance Method Details

#initialize(bind, core) ⇒ Object



42
43
44
45
# File 'lib/ffwd/plugin/json/connection.rb', line 42

def initialize bind, core
  @bind = bind
  @core = core
end

#read_event(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ffwd/plugin/json/connection.rb', line 96

def read_event data
  d = {}

  read_tags d, data["tags"]
  read_time d, data["time"]

  EVENT_FIELDS.each do |from, to|
    next if (v = data[from]).nil?
    d[to] = v
  end

  d
end

#read_metric(data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ffwd/plugin/json/connection.rb', line 82

def read_metric data
  d = {}

  read_tags d, data["tags"]
  read_time d, data["time"]

  METRIC_FIELDS.each do |from, to|
    next if (v = data[from]).nil?
    d[to] = v
  end

  d
end

#read_tags(d, source) ⇒ Object



72
73
74
75
# File 'lib/ffwd/plugin/json/connection.rb', line 72

def read_tags d, source
  return if (tags = d["tags"]).nil?
  d[:tags] = tags.to_set
end

#read_time(d, source) ⇒ Object



77
78
79
80
# File 'lib/ffwd/plugin/json/connection.rb', line 77

def read_time d, source
  return if (time = d["time"]).nil?
  d[:time] = Time.at time
end

#receive_json(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ffwd/plugin/json/connection.rb', line 47

def receive_json data
  data = JSON.load(data)

  unless type = data["type"]
    log.error "Field 'type' missing from received line"
    return
  end

  if type == "metric"
    @core.input.metric read_metric(data)
    @bind.increment :received_metrics
    return
  end

  if type == "event"
    @core.input.event read_event(data)
    @bind.increment :received_events
    return
  end

  log.error "No such type: #{type}"
rescue => e
  log.error "Failed to receive line", e
end