Skip to content

Commit 33376e7

Browse files
committed
add config check
1 parent 8d9a02e commit 33376e7

File tree

4 files changed

+65
-57
lines changed

4 files changed

+65
-57
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ Style/ClassVars:
3838
Style/FetchEnvVar:
3939
Enabled: false
4040

41+
Style/NumericLiterals:
42+
Enabled: false
43+
44+
Style/MixinUsage:
45+
Exclude:
46+
- 'test/helper.rb'
47+
4148
Style/PercentLiteralDelimiters:
4249
Enabled: false
4350

.rubocop_todo.yml

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-04-15 08:39:58 UTC using RuboCop version 1.75.2.
3+
# on 2025-04-16 04:41:33 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
@@ -54,25 +54,6 @@ Style/IfUnlessModifier:
5454
Exclude:
5555
- 'lib/fluent/plugin/out_otlp.rb'
5656

57-
# Offense count: 1
58-
Style/MixinUsage:
59-
Exclude:
60-
- 'test/helper.rb'
61-
62-
# Offense count: 1
63-
# This cop supports safe autocorrection (--autocorrect).
64-
# Configuration parameters: Strict, AllowedNumbers, AllowedPatterns.
65-
Style/NumericLiterals:
66-
MinDigits: 6
67-
68-
# Offense count: 1
69-
# This cop supports unsafe autocorrection (--autocorrect-all).
70-
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
71-
# AllowedMethods: present?, blank?, presence, try, try!
72-
Style/SafeNavigation:
73-
Exclude:
74-
- 'lib/fluent/plugin/out_otlp.rb'
75-
7657
# Offense count: 3
7758
# This cop supports safe autocorrection (--autocorrect).
7859
# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, AllowedMethods.

lib/fluent/plugin/in_otlp.rb

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,67 @@ class OtlpInput < Input
3535
desc "The tag of the event."
3636
config_param :tag, :string
3737

38-
config_section :http, required: false, multi: false, init: true, param_name: :http_config do
38+
config_section :http, required: false, multi: false, init: false, param_name: :http_config do
3939
desc "The address to bind to."
4040
config_param :bind, :string, default: "0.0.0.0"
4141
desc "The port to listen to."
4242
config_param :port, :integer, default: 4318
4343
end
4444

45+
46+
config_section :grpc, required: false, multi: false, init: false, param_name: :grpc_config do
47+
desc "The address to bind to."
48+
config_param :bind, :string, default: "0.0.0.0"
49+
desc "The port to listen to."
50+
config_param :port, :integer, default: 4317
51+
end
52+
4553
config_section :transport, required: false, multi: false, init: true, param_name: :transport_config do
4654
config_argument :protocol, :enum, list: [:tls], default: nil
4755
end
4856

57+
def configure(conf)
58+
super
59+
60+
unless [@http_config, @grpc_config].any?
61+
raise Fluent::ConfigError, "Please configure either <http> or <grpc> section, or both."
62+
end
63+
end
64+
65+
def start
66+
super
67+
68+
if @http_config
69+
http_handler = HttpHandler.new
70+
http_server_create_http_server(:in_otlp_http_server_helper, addr: @http_config.bind, port: @http_config.port, logger: log) do |serv|
71+
serv.post("/v1/logs") do |req|
72+
http_handler.logs(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_LOGS, message: record }) }
73+
end
74+
serv.post("/v1/metrics") do |req|
75+
http_handler.metrics(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_METRICS, message: record }) }
76+
end
77+
serv.post("/v1/traces") do |req|
78+
http_handler.traces(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_TRACES, message: record }) }
79+
end
80+
end
81+
end
82+
83+
if @grpc_config
84+
grpc_handler = GrpcHandler.new
85+
grpc_handler.run(
86+
logs: lambda { |record|
87+
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_LOGS, message: record })
88+
},
89+
metrics: lambda { |record|
90+
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_METRICS, message: record })
91+
},
92+
traces: lambda { |record|
93+
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_TRACES, message: record })
94+
}
95+
)
96+
end
97+
end
98+
4999
class HttpHandler
50100
def logs(req, &block)
51101
common(req, Otlp::Request::Logs, Otlp::Response::Logs, &block)
@@ -152,35 +202,5 @@ def run(logs:, metrics:, traces:)
152202
end
153203
end
154204
end
155-
156-
def start
157-
super
158-
159-
http_handler = HttpHandler.new
160-
http_server_create_http_server(:in_otlp_http_server_helper, addr: @http_config.bind, port: @http_config.port, logger: log) do |serv|
161-
serv.post("/v1/logs") do |req|
162-
http_handler.logs(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_LOGS, message: record }) }
163-
end
164-
serv.post("/v1/metrics") do |req|
165-
http_handler.metrics(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_METRICS, message: record }) }
166-
end
167-
serv.post("/v1/traces") do |req|
168-
http_handler.traces(req) { |record| router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_TRACES, message: record }) }
169-
end
170-
end
171-
172-
grpc_handler = GrpcHandler.new
173-
grpc_handler.run(
174-
logs: lambda { |record|
175-
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_LOGS, message: record })
176-
},
177-
metrics: lambda { |record|
178-
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_METRICS, message: record })
179-
},
180-
traces: lambda { |record|
181-
router.emit(@tag, Fluent::EventTime.now, { type: Otlp::RECORD_TYPE_TRACES, message: record })
182-
}
183-
)
184-
end
185205
end
186206
end

lib/fluent/plugin/out_otlp.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class RetryableResponse < StandardError; end
5252
def configure(conf)
5353
super
5454

55+
unless [@http_config, @grpc_config].one?
56+
raise Fluent::ConfigError, "Please configure either <http> or <grpc> section."
57+
end
58+
5559
@http_handler = HttpHandler.new(@http_config, @transport_config, log) if @http_config
5660
@grpc_handler = GrpcHandler.new(@grpc_config, @transport_config, log) if @grpc_config
5761
end
@@ -65,14 +69,10 @@ def format(tag, time, record)
6569
end
6670

6771
def write(chunk)
68-
if @http_handler
69-
@http_handler.export(chunk)
70-
71-
return
72-
end
73-
7472
if @grpc_handler
7573
@grpc_handler.export(chunk)
74+
else
75+
@http_handler.export(chunk)
7676
end
7777
end
7878

0 commit comments

Comments
 (0)