Class: FFWD::Plugin::JSON::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/ffwd/plugin/json/connection.rb

Direct Known Subclasses

FrameConnection, LineConnection

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],
  ["host", :host],
  ["value", :value],
  ["tags", :tags],
  ["attributes", :attributes]
]

Instance Method Summary collapse

Methods inherited from Connection

#datasink=, #send_data

Constructor Details

#initialize(bind, core, config) ⇒ Connection

Returns a new instance of Connection.



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

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

Instance Method Details

#read_event(data) ⇒ Object



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

def read_event data
  d = {}

  read_tags d, data
  read_time d, data

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

  d
end

#read_metric(data) ⇒ Object



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

def read_metric data
  d = {}

  read_tags d, data
  read_time d, data

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

  d
end

#read_tags(d, source) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/ffwd/plugin/json/connection.rb', line 69

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

  unless tags.is_a? Array
    raise "'tags' must be an array"
  end

  d[:tags] = tags.to_set
end

#read_time(d, source) ⇒ Object



79
80
81
82
# File 'lib/ffwd/plugin/json/connection.rb', line 79

def read_time d, source
  return if (time = source["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
# File 'lib/ffwd/plugin/json/connection.rb', line 47

def receive_json data
  data = JSON.load(data)

  unless type = data["type"]
    raise "Field 'type' missing from received line"
  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

  raise "No such type: #{type}"
end