Skip to content

Commit bd2a9bf

Browse files
committed
add
1 parent 1752c1a commit bd2a9bf

File tree

5 files changed

+169
-24
lines changed

5 files changed

+169
-24
lines changed

.rubocop_todo.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-04-04 09:06:04 UTC using RuboCop version 1.75.2.
3+
# on 2025-04-10 06:21:04 UTC using RuboCop version 1.75.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 10
9+
# Offense count: 1
10+
# This cop supports safe autocorrection (--autocorrect).
11+
Lint/RedundantStringCoercion:
12+
Exclude:
13+
- 'lib/fluent/plugin/out_otlp.rb'
14+
15+
# Offense count: 11
1016
# Configuration parameters: AllowedConstants.
1117
Style/Documentation:
1218
Exclude:
@@ -16,3 +22,21 @@ Style/Documentation:
1622
- 'lib/fluent/plugin/otlp/request.rb'
1723
- 'lib/fluent/plugin/otlp/response.rb'
1824
- 'lib/fluent/plugin/out_otlp.rb'
25+
26+
# Offense count: 3
27+
# This cop supports safe autocorrection (--autocorrect).
28+
# Configuration parameters: MinBodyLength, AllowConsecutiveConditionals.
29+
Style/GuardClause:
30+
Exclude:
31+
- 'lib/fluent/plugin/out_otlp.rb'
32+
33+
# Offense count: 1
34+
# This cop supports safe autocorrection (--autocorrect).
35+
Style/IfUnlessModifier:
36+
Exclude:
37+
- 'lib/fluent/plugin/out_otlp.rb'
38+
39+
# Offense count: 1
40+
Style/MixinUsage:
41+
Exclude:
42+
- 'test/helper.rb'

lib/fluent/plugin/otlp/constant.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "fluent/plugin"
34
require "openssl"
45

56
module Fluent::Plugin::Otlp

test/fluent/assets/logs.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"resourceLogs": [
3+
{
4+
"resource": {},
5+
"scopeLogs": [
6+
{
7+
"scope": {
8+
"name": "test-unit",
9+
"version": "0.0.0"
10+
},
11+
"logRecords": [
12+
{
13+
"timeUnixNano": "1744261044119140009",
14+
"severityText": "INFO",
15+
"body": {
16+
"stringValue": "This is sample log."
17+
},
18+
"attributes": [
19+
{
20+
"key": "cedar",
21+
"value": {
22+
"boolValue": true
23+
}
24+
}
25+
],
26+
"observedTimeUnixNano": "1744261044119142403"
27+
}
28+
]
29+
}
30+
]
31+
}
32+
]
33+
}

test/fluent/plugin/test_in_otel.rb

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# frozen_string_literal: true
22

33
require "helper"
4-
require 'fluent/test/driver/input'
5-
require 'fluent/plugin/in_otlp'
6-
require 'excon'
7-
require 'timecop'
4+
5+
require "fluent/plugin/in_otlp"
6+
require "fluent/test/driver/input"
87

98
class Fluent::Plugin::OtlpInputTest < Test::Unit::TestCase
109
def config
@@ -21,11 +20,7 @@ def setup
2120
Fluent::Test.setup
2221
end
2322

24-
def teardown
25-
Timecop.return
26-
end
27-
28-
def create_driver(conf=config)
23+
def create_driver(conf = config)
2924
Fluent::Test::Driver::Input.new(Fluent::Plugin::OtlpInput).configure(conf)
3025
end
3126

@@ -36,23 +31,103 @@ def test_configure
3631
assert_equal 4318, d.instance.http_config.port
3732
end
3833

39-
def test_receive_json
34+
data("metrics" => {
35+
request_path: "/v1/metrics",
36+
request_data: TestData::JSON::METRICS,
37+
record_type: "otlp_metrics",
38+
record_data: TestData::JSON::METRICS
39+
},
40+
"traces" => {
41+
request_path: "/v1/traces",
42+
request_data: TestData::JSON::TRACES,
43+
record_type: "otlp_traces",
44+
record_data: TestData::JSON::TRACES
45+
},
46+
"logs" => {
47+
request_path: "/v1/logs",
48+
request_data: TestData::JSON::LOGS,
49+
record_type: "otlp_logs",
50+
record_data: TestData::JSON::LOGS
51+
})
52+
def test_receive_json(data)
53+
d = create_driver
54+
res = d.run(expect_records: 1) do
55+
post_json(data[:request_path], data[:request_data])
56+
end
57+
58+
assert_equal(200, res.status)
59+
assert_equal("otlp.test", d.events[0][0])
60+
assert_equal({ type: data[:record_type], message: data[:record_data] }, d.events[0][2])
61+
end
62+
63+
def test_receive_compressed_json
4064
d = create_driver
4165
res = d.run(expect_records: 1) do
42-
post_json("/v1/metrics", Fluent::Plugin::Otlp::JSON::METRICS)
66+
post_json("/v1/metrics", compress(TestData::JSON::METRICS), { "Content-Encoding" => "gzip" })
4367
end
4468

4569
assert_equal(200, res.status)
4670
assert_equal("otlp.test", d.events[0][0])
47-
assert_equal({ type: "otlp_metrics", message: Fluent::Plugin::Otlp::JSON::METRICS }, d.events[0][2])
71+
assert_equal({ type: "otlp_metrics", message: TestData::JSON::METRICS }, d.events[0][2])
72+
end
73+
74+
data("metrics" => {
75+
request_path: "/v1/metrics",
76+
request_data: TestData::ProtocolBuffers::METRICS,
77+
record_type: "otlp_metrics",
78+
record_data: TestData::JSON::METRICS
79+
},
80+
"traces" => {
81+
request_path: "/v1/traces",
82+
request_data: TestData::ProtocolBuffers::TRACES,
83+
record_type: "otlp_traces",
84+
record_data: TestData::JSON::TRACES
85+
},
86+
"logs" => {
87+
request_path: "/v1/logs",
88+
request_data: TestData::ProtocolBuffers::LOGS,
89+
record_type: "otlp_logs",
90+
record_data: TestData::JSON::LOGS
91+
})
92+
def test_receive_protocol_buffers(data)
93+
d = create_driver
94+
res = d.run(expect_records: 1) do
95+
post_protobuf(data[:request_path], data[:request_data])
96+
end
97+
98+
assert_equal(200, res.status)
99+
assert_equal("otlp.test", d.events[0][0])
100+
assert_equal({ type: data[:record_type], message: data[:record_data] }, d.events[0][2])
101+
end
102+
103+
def test_receive_compressed_protocol_buffers
104+
d = create_driver
105+
res = d.run(expect_records: 1) do
106+
post_json("/v1/metrics", compress(TestData::ProtocolBuffers::METRICS), { "Content-Encoding" => "gzip" })
107+
end
108+
109+
assert_equal(200, res.status)
110+
assert_equal("otlp.test", d.events[0][0])
111+
assert_equal({ type: "otlp_metrics", message: TestData::JSON::METRICS }, d.events[0][2])
112+
end
113+
114+
def compress(data)
115+
gz = Zlib::GzipWriter.new(StringIO.new)
116+
gz << data
117+
gz.close.string
118+
end
119+
120+
def post_json(path, json, headers = {})
121+
headers = headers.merge({ "Content-Type" => "application/json" })
122+
post(path, json, headers)
48123
end
49124

50-
def post_json(path, json)
51-
headers = { "Content-Type" => "application/json" }
52-
post(path, headers, json)
125+
def post_protobuf(path, binary, headers = {})
126+
headers = headers.merge({ "Content-Type" => "application/x-protobuf" })
127+
post(path, binary, headers)
53128
end
54129

55-
def post(path, headers, body)
130+
def post(path, body, headers = {})
56131
connection = Excon.new("http://127.0.0.1:4318#{path}", body: body, headers: headers)
57132
connection.post
58133
end

test/helper.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22

33
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
44

5+
require "fluent/plugin/otlp/request"
6+
require "fluent/test"
7+
require "fluent/test/helpers"
8+
9+
require "excon"
10+
require "json"
511
require "test-unit"
6-
require 'fluent/test'
7-
require 'fluent/test/helpers'
8-
require 'json'
12+
require "timecop"
13+
require "zlib"
914

1015
include Fluent::Test::Helpers
1116

12-
module Fluent::Plugin::Otlp
17+
module TestData
1318
module JSON
1419
# trim white spaces
1520
METRICS = ::JSON.generate(::JSON.parse(File.read(File.join(__dir__, "./fluent/assets/metrics.json"))))
16-
TRACES = ::JSON.parse(File.read(File.join(__dir__, "./fluent/assets/traces")))
21+
TRACES = ::JSON.generate(::JSON.parse(File.read(File.join(__dir__, "./fluent/assets/traces.json"))))
22+
LOGS = ::JSON.generate(::JSON.parse(File.read(File.join(__dir__, "./fluent/assets/logs.json"))))
23+
end
24+
25+
module ProtocolBuffers
26+
METRICS = Fluent::Plugin::Otlp::Request::Metrics.new(TestData::JSON::METRICS).encode
27+
TRACES = Fluent::Plugin::Otlp::Request::Traces.new(TestData::JSON::TRACES).encode
28+
LOGS = Fluent::Plugin::Otlp::Request::Logs.new(TestData::JSON::LOGS).encode
1729
end
18-
end
30+
end

0 commit comments

Comments
 (0)