Skip to content

Commit 48e15e1

Browse files
committed
WIP
1 parent fe726fd commit 48e15e1

File tree

4 files changed

+514
-5
lines changed

4 files changed

+514
-5
lines changed

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ gem "grpc", "~> 1.73"
99
gem "grpc-tools"
1010
gem "irb"
1111
gem "rake", "~> 13.0"
12-
gem "test-unit", "~> 3.0"
12+
gem "rr"
13+
gem "test-unit"
14+
gem "test-unit-rr"
1315
gem "timecop"
1416

1517
gem "rubocop", "~> 1.75"

test/fluent/plugin/test_in_opentelemetry_metrics.rb

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,125 @@
33
require "helper"
44

55
require "fluent/plugin/in_opentelemetry_metrics"
6+
require "fluent/plugin/opentelemetry/constant"
67
require "fluent/plugin/opentelemetry/request"
78
require "fluent/test/driver/input"
89

9-
class Fluent::Plugin::OpentelemetryMetricsInputTest < Test::Unit::TestCase
10+
require "test_plugin_classes"
1011

12+
class Fluent::Plugin::OpentelemetryMetricsInputTest < Test::Unit::TestCase
1113
def setup
1214
Fluent::Test.setup
15+
16+
# Enable mock_process_clock for freezing Fluent::EventTime
17+
Timecop.mock_process_clock = true
18+
Timecop.freeze(Time.parse("2025-01-01 00:00:00 UTC"))
19+
@event_time = Fluent::EventTime.now
1320
end
14-
21+
22+
setup do
23+
# check @type and type in one configuration
24+
conf = <<~CONFIG
25+
<source>
26+
@type test_in_gen
27+
@id test_in_gen
28+
num 10
29+
</source>
30+
<filter>
31+
@type test_filter
32+
@id test_filter
33+
</filter>
34+
<match **>
35+
@type relabel
36+
@id test_relabel
37+
@label @test
38+
</match>
39+
<label @test>
40+
<match **>
41+
@type test_out
42+
@id test_out
43+
</match>
44+
</label>
45+
<label @copy>
46+
<match **>
47+
@type copy
48+
<store>
49+
@type test_out
50+
@id copy_out_1
51+
</store>
52+
<store>
53+
@type test_out
54+
@id copy_out_2
55+
</store>
56+
</match>
57+
</label>
58+
<label @ERROR>
59+
<match>
60+
@type null
61+
@id null
62+
</match>
63+
</label>
64+
CONFIG
65+
66+
@root_agent = Fluent::RootAgent.new(log: $log) # rubocop:disable Style/GlobalVars
67+
stub(Fluent::Engine).root_agent { @root_agent }
68+
@root_agent = configure_root_agent(@root_agent, conf)
69+
end
70+
71+
def teardown
72+
Timecop.mock_process_clock = false
73+
Timecop.return
74+
end
75+
76+
def create_driver(conf = config)
77+
Fluent::Test::Driver::Input.new(Fluent::Plugin::OpentelemetryMetricsInput).configure(conf)
78+
end
79+
80+
def configure_root_agent(root_agent, conf_str)
81+
conf = Fluent::Config.parse(conf_str, "(test)", "(test_dir)", true)
82+
root_agent.configure(conf)
83+
root_agent
84+
end
85+
86+
def config
87+
<<~CONFIG
88+
tag opentelemetry.test
89+
emit_interval 1s
90+
CONFIG
91+
end
92+
1593
def test_metrics_record_conforms_to_spec
1694
metrics = Fluent::Plugin::OpentelemetryMetricsInput::Metrics.new(metric_name_prefix: "fluentd_")
1795

18-
assert_false metrics.values.empty?
19-
2096
# validate the metrics record
2197
assert_nothing_raised do
2298
# If record does not conform to OpenTelemetry Protocol, it raises Google::Protobuf::ParseError.
2399
# To validate strictly, it set ignore_unknown_fields to false.
24100
Fluent::Plugin::Opentelemetry::Request::Metrics.new(metrics.record, ignore_unknown_fields: false)
25101
end
26102
end
103+
104+
def test_emit_metrics
105+
d = create_driver
106+
d.run(expect_records: 1)
107+
108+
event = d.events.first
109+
assert_equal("opentelemetry.test", event[0])
110+
assert_equal(@event_time, event[1])
111+
assert_equal(Fluent::Plugin::Opentelemetry::RECORD_TYPE_METRICS, event[2]["type"])
112+
assert_nothing_raised do
113+
JSON.parse(event[2]["message"])
114+
end
115+
end
116+
117+
def test_metrics_name_prefix
118+
d = create_driver(config + "metric_name_prefix foobarbaz_")
119+
d.run(expect_records: 1)
120+
121+
event = d.events.first
122+
record = JSON.parse(event[2]["message"])
123+
124+
metrics = record["resourceMetrics"][0]["scopeMetrics"][0]["metrics"]
125+
assert_true(metrics.all? { |metric| metric["name"].start_with?("foobarbaz_") })
126+
end
27127
end

test/helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
require "timecop"
1515
require "zlib"
1616

17+
require "rr"
18+
require "test/unit/rr"
19+
1720
include Fluent::Test::Helpers
1821

1922
module TestData

0 commit comments

Comments
 (0)