Skip to content

Commit 75fdc90

Browse files
committed
WIP
1 parent d327f47 commit 75fdc90

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

lib/fluent/plugin/in_opentelemetry_metrics.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ class OpentelemetryMetricsInput < Input
2121
desc "The tag of the event."
2222
config_param :tag, :string
2323

24+
desc "The prefix of metric name."
25+
config_param :metric_name_prefix, :string, default: "fluentd_"
26+
2427
def start
2528
super
2629

27-
@metrics = Metrics.new
30+
@metrics = Metrics.new(metric_name_prefix: metric_name_prefix)
2831
timer_execute(:in_opentelemetry_metrics, @emit_interval) do
2932
router.emit(@tag, Fluent::EventTime.now, { "type" => Opentelemetry::RECORD_TYPE_METRICS, "message" => JSON.generate(@metrics.metrics_data) })
3033
end
@@ -41,8 +44,9 @@ def to_nano_sec
4144
class Metrics
4245
using Extension
4346

44-
def initialize
47+
def initialize(metric_name_prefix:)
4548
@start_time_unix_nano = Time.now.to_nano_sec
49+
@metric_name_prefix = metric_name_prefix.to_s
4650
end
4751

4852
def metrics_data
@@ -67,14 +71,14 @@ def scope_metrics
6771
time = Time.now
6872

6973
metrics = []
70-
monitor.plugins_info_all.map do |record|
74+
monitor.plugins_info_all.each do |record|
7175
attrs = {
7276
plugin_id: record["plugin_id"],
7377
plugin: "#{record['plugin_category']}_#{record['type']}",
7478
plugin_category: record["plugin_category"],
7579
plugin_type: record["type"]
7680
}
77-
metrics_value(time, record, attrs, metrics)
81+
metrics.concat(metrics_value(time, record, attrs))
7882
end
7983

8084
[
@@ -88,27 +92,27 @@ def scope_metrics
8892
]
8993
end
9094

91-
def metrics_value(time, record, attrs, metrics = [])
95+
def metrics_value(time, record, attrs)
9296
attributes = attrs.map { |k, v| string_value_attribute(k, v) }
9397

94-
record.each do |key, value|
95-
if value.is_a?(Numeric)
96-
metrics << {
97-
"name" => "fluentd_" + key.to_s,
98-
"unit" => "1",
99-
# TODO: "description"
100-
"gauge" => {
101-
"dataPoints" => [
102-
{
103-
"startTimeUnixNano" => @start_time_unix_nano.to_s,
104-
"timeUnixNano" => time.to_nano_sec.to_s,
105-
"asDouble" => value,
106-
"attributes" => attributes
107-
}
108-
]
109-
}
98+
record.each_with_object([]) do |(key, value), metrics|
99+
next unless value.is_a?(Numeric)
100+
101+
metrics << {
102+
"name" => @metric_name_prefix + key.to_s,
103+
"unit" => "1",
104+
# TODO: "description"
105+
"gauge" => {
106+
"dataPoints" => [
107+
{
108+
"startTimeUnixNano" => @start_time_unix_nano.to_s,
109+
"timeUnixNano" => time.to_nano_sec.to_s,
110+
"asDouble" => value,
111+
"attributes" => attributes
112+
}
113+
]
110114
}
111-
end
115+
}
112116
end
113117
end
114118

0 commit comments

Comments
 (0)