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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 22

def configure(conf)
  super

  unless is_custom_metric? @custom_metrics.type
    raise Fluent::ConfigError.new "custom_metrics.type must start with \"#{TYPE_PREFIX}\""
  end

  if @custom_metrics.metric_kind == :CUMULATIVE
    if @custom_metrics.time_interval == 0
      raise Fluent::ConfigError.new 'time_interval must be greater than 0 if metric_kind is set to CUMULATIVE'
    end
    if @custom_metrics.value_type == :BOOL
      raise Fluent::ConfigError.new 'custom metric does not support BOOL value type if metric_kind is set to CUMULATIVE'
    end
  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



49
50
51
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 49

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

#startObject



42
43
44
45
46
47
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 42

def start
  super

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

#write(chunk) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/out_stackdriver_monitoring.rb', line 53

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, @custom_metrics.time_interval
    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