Class: Fluent::StackdriverMonitoringOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_stackdriver_monitoring.rb

Constant Summary collapse

TYPE_PREFIX =
'custom.googleapis.com/'.freeze

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 21

def configure(conf)
  super

  unless @custom_metrics.type.start_with? TYPE_PREFIX
    raise "custom_metrics.type must start with \"#{TYPE_PREFIX}\""
  end

  @project_name = Google::Cloud::Monitoring::V3::MetricServiceClient.project_path @project
  @metric_name = Google::Cloud::Monitoring::V3::MetricServiceClient.metric_descriptor_path @project, @custom_metrics.type
end

#format(tag, time, record) ⇒ Object



39
40
41
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 39

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#startObject



32
33
34
35
36
37
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 32

def start
  super

  @metric_service_client = Google::Cloud::Monitoring::V3::MetricServiceClient.new
  @metric_descriptor = create_metric_descriptor
end

#write(chunk) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 43

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    time_series = create_time_series
    value = record[@custom_metrics.key]

    point = Google::Monitoring::V3::Point.new
    point.interval = create_time_interval time
    point.value = create_typed_value value
    time_series.points.push point

    log.debug "Create time series", time: Time.at(time).to_s, value: value, metric_name: @metric_name
    # Only one point can be written per TimeSeries per request.
    @metric_service_client.create_time_series @project_name, [time_series]
  end
end