Skip to content

Commit ab51c5d

Browse files
committed
add test
1 parent 3a67bac commit ab51c5d

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ gem "irb"
99
gem "rake", "~> 13.0"
1010
gem "rubocop", "~> 1.75"
1111
gem "test-unit", "~> 3.0"
12+
gem "timecop"

lib/fluent/plugin/otlp/request.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class Fluent::Plugin::Otlp::Request
1010
class Logs
1111
def initialize(body)
1212
@request =
13-
if body.encoding == Encoding::BINARY
14-
Opentelemetry::Proto::Collector::Logs::V1::ExportLogsServiceRequest.decode(body)
15-
else
13+
if body.start_with?("{")
1614
Opentelemetry::Proto::Collector::Logs::V1::ExportLogsServiceRequest.decode_json(body)
15+
else
16+
Opentelemetry::Proto::Collector::Logs::V1::ExportLogsServiceRequest.decode(body)
1717
end
1818
end
1919

@@ -29,10 +29,10 @@ def record
2929
class Metrics
3030
def initialize(body)
3131
@request =
32-
if body.encoding == Encoding::BINARY
33-
Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest.decode(body)
34-
else
32+
if body.start_with?("{")
3533
Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest.decode_json(body)
34+
else
35+
Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest.decode(body)
3636
end
3737
end
3838

@@ -48,10 +48,10 @@ def record
4848
class Traces
4949
def initialize(body)
5050
@request =
51-
if body.encoding == Encoding::BINARY
52-
Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest.decode(body)
53-
else
51+
if body.start_with?("{")
5452
Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest.decode_json(body)
53+
else
54+
Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest.decode(body)
5555
end
5656
end
5757

test/fluent/helper/metrics.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"resourceMetrics": [
3+
{
4+
"resource": {
5+
"attributes": [
6+
{
7+
"key": "service.name",
8+
"value": {
9+
"stringValue": "unknown_service"
10+
}
11+
},
12+
{
13+
"key": "telemetry.sdk.version",
14+
"value": {
15+
"stringValue": "1.19.0"
16+
}
17+
},
18+
{
19+
"key": "telemetry.sdk.name",
20+
"value": {
21+
"stringValue": "opentelemetry"
22+
}
23+
},
24+
{
25+
"key": "telemetry.sdk.language",
26+
"value": {
27+
"stringValue": "cpp"
28+
}
29+
}
30+
]
31+
},
32+
"scopeMetrics": [
33+
{
34+
"scope": {
35+
"name": "otlp_grpc_metric_example"
36+
},
37+
"metrics": [
38+
{
39+
"name": "otlp_grpc_metric_example_counter",
40+
"sum": {
41+
"dataPoints": [
42+
{
43+
"startTimeUnixNano": "1744167147880764228",
44+
"timeUnixNano": "1744167148881527028",
45+
"asDouble": 45
46+
}
47+
],
48+
"aggregationTemporality": "AGGREGATION_TEMPORALITY_CUMULATIVE",
49+
"isMonotonic": true
50+
}
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
}

test/fluent/plugin/test_in_otel.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
require "helper"
44
require 'fluent/test/driver/input'
55
require 'fluent/plugin/in_otlp'
6+
require 'excon'
7+
require 'timecop'
68

7-
class Fluent::Plugin::OtelInputTest < Test::Unit::TestCase
9+
class Fluent::Plugin::OtlpInputTest < Test::Unit::TestCase
810
def config
911
<<~CONFIG
1012
tag otlp.test
@@ -15,6 +17,14 @@ def config
1517
CONFIG
1618
end
1719

20+
def setup
21+
Fluent::Test.setup
22+
end
23+
24+
def teardown
25+
Timecop.return
26+
end
27+
1828
def create_driver(conf=config)
1929
Fluent::Test::Driver::Input.new(Fluent::Plugin::OtlpInput).configure(conf)
2030
end
@@ -26,4 +36,24 @@ def test_configure
2636
assert_equal 4318, d.instance.http_config.port
2737
end
2838

39+
def test_receive_json
40+
d = create_driver
41+
res = d.run(expect_records: 1) do
42+
post_json("/v1/metrics", Fluent::Plugin::Otlp::JSON::METRICS)
43+
end
44+
45+
assert_equal(200, res.status)
46+
assert_equal("otlp.test", d.events[0][0])
47+
assert_equal({ type: "otlp_metrics", message: Fluent::Plugin::Otlp::JSON::METRICS }, d.events[0][2])
48+
end
49+
50+
def post_json(path, json)
51+
headers = { "Content-Type" => "application/json" }
52+
post(path, headers, json)
53+
end
54+
55+
def post(path, headers, body)
56+
connection = Excon.new("http://127.0.0.1:4318#{path}", body: body, headers: headers)
57+
connection.post
58+
end
2959
end

test/helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@
55
require "test-unit"
66
require 'fluent/test'
77
require 'fluent/test/helpers'
8+
require 'json'
9+
10+
include Fluent::Test::Helpers
11+
12+
module Fluent::Plugin::Otlp
13+
module JSON
14+
# trim white spaces
15+
METRICS = ::JSON.generate(::JSON.parse(File.read(File.join(__dir__, "./fluent/helper/metrics.json"))))
16+
end
17+
end

0 commit comments

Comments
 (0)