Skip to content

Commit 597fdfa

Browse files
committed
use excon gem
1 parent 9cd8d4c commit 597fdfa

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

fluent-plugin-otlp.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727
spec.require_paths = ["lib"]
2828

2929
spec.add_dependency("async-http", "~> 0.88.0")
30+
spec.add_dependency("excon", "~> 1.2")
3031
spec.add_dependency("fluentd", "~> 1.18")
3132
spec.add_dependency("google-protobuf", "~> 4.30")
3233
end

lib/fluent/plugin/out_otlp.rb

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

3+
require "excon"
34
require "fluent/plugin/otlp/constant"
45
require "fluent/plugin/otlp/request"
56
require "fluent/plugin/output"
@@ -21,8 +22,8 @@ class OtlpOutput < Output
2122
config_section :http, required: false, multi: false, init: true, param_name: :http_config do
2223
desc "The endpoint"
2324
config_param :endpoint, :string, default: "http://127.0.0.1:4318"
24-
desc 'The proxy for HTTP request'
25-
config_param :proxy, :string, default: ENV['HTTP_PROXY'] || ENV['http_proxy']
25+
desc "The proxy for HTTP request"
26+
config_param :proxy, :string, default: ENV["HTTP_PROXY"] || ENV["http_proxy"]
2627
end
2728

2829
desc "Compress request body"
@@ -40,8 +41,6 @@ def configure(conf)
4041
OtlpOutput.const_set(:HTTP_LOGS_ENDPOINT, "#{@http_config.endpoint}/v1/logs".freeze)
4142
OtlpOutput.const_set(:HTTP_METRICS_ENDPOINT, "#{@http_config.endpoint}/v1/metrics".freeze)
4243
OtlpOutput.const_set(:HTTP_TRACES_ENDPOINT, "#{@http_config.endpoint}/v1/traces".freeze)
43-
44-
@http_proxy_uri = URI.parse(@http_config.proxy) if @http_config.proxy
4544
end
4645

4746
def multi_workers_ready?
@@ -53,16 +52,16 @@ def format(tag, time, record)
5352
end
5453

5554
def write(chunk)
56-
uri, req = create_uri_request(chunk)
57-
58-
Net::HTTP.start(uri.host, uri.port, @http_proxy_uri&.host, @http_proxy_uri&.port, @http_proxy_uri&.user, @http_proxy_uri&.password) do |http|
59-
http.request(req)
55+
uri, connection = create_connection(chunk)
56+
response = connection.post
57+
if response.status != 200
58+
log.error "got error response from '#{uri.to_s}'"
6059
end
6160
end
6261

6362
private
6463

65-
def create_uri_request(chunk)
64+
def create_connection(chunk)
6665
record = JSON.parse(chunk.read)
6766
msg = record["message"]
6867

@@ -77,22 +76,19 @@ def create_uri_request(chunk)
7776
uri = HTTP_TRACES_ENDPOINT
7877
body = Otlp::Request::Traces.new(msg).encode
7978
else
80-
raise "Unknown record type: #{record["type"]}"
79+
raise "Unknown record type: #{record['type']}"
8180
end
8281

83-
uri = URI.parse(uri)
84-
req = Net::HTTP::Post.new(uri.request_uri)
85-
req["Content-Type"] = Otlp::CONTENT_TYPE_PROTOBUF
86-
82+
headers = { "Content-Type" => Otlp::CONTENT_TYPE_PROTOBUF }
8783
if @compress == :gzip
88-
req["Content-Encoding"] = Otlp::CONTENT_ENCODING_GZIP
84+
headers["Content-Encoding"] = Otlp::CONTENT_ENCODING_GZIP
8985
gz = Zlib::GzipWriter.new(StringIO.new)
9086
gz << body
9187
body = gz.close.string
9288
end
9389

94-
req.body = body
95-
[uri, req]
90+
connection = Excon.new(uri, body: body, headers: headers, proxy: @http_config.proxy, persistent: true)
91+
[uri, connection]
9692
end
9793
end
9894
end

0 commit comments

Comments
 (0)