|
3 | 3 | require "helper" |
4 | 4 |
|
5 | 5 | require "fluent/plugin/in_opentelemetry_metrics" |
| 6 | +require "fluent/plugin/opentelemetry/constant" |
6 | 7 | require "fluent/plugin/opentelemetry/request" |
7 | 8 | require "fluent/test/driver/input" |
8 | 9 |
|
9 | | -class Fluent::Plugin::OpentelemetryMetricsInputTest < Test::Unit::TestCase |
| 10 | +require "test_plugin_classes" |
10 | 11 |
|
| 12 | +class Fluent::Plugin::OpentelemetryMetricsInputTest < Test::Unit::TestCase |
11 | 13 | def setup |
12 | 14 | 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 |
13 | 20 | 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 | + |
15 | 93 | def test_metrics_record_conforms_to_spec |
16 | 94 | metrics = Fluent::Plugin::OpentelemetryMetricsInput::Metrics.new(metric_name_prefix: "fluentd_") |
17 | 95 |
|
18 | | - assert_false metrics.values.empty? |
19 | | - |
20 | 96 | # validate the metrics record |
21 | 97 | assert_nothing_raised do |
22 | 98 | # If record does not conform to OpenTelemetry Protocol, it raises Google::Protobuf::ParseError. |
23 | 99 | # To validate strictly, it set ignore_unknown_fields to false. |
24 | 100 | Fluent::Plugin::Opentelemetry::Request::Metrics.new(metrics.record, ignore_unknown_fields: false) |
25 | 101 | end |
26 | 102 | 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 |
27 | 127 | end |
0 commit comments