Skip to content

Commit

Permalink
Refactor: common exporter methods (#1265)
Browse files Browse the repository at this point in the history
* refactor: use common utilities for url validation

* refactor: use common utils config_opt
  • Loading branch information
robertlaurin authored May 18, 2022
1 parent b897830 commit 5a3c2fb
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(endpoint: ENV.fetch('OTEL_EXPORTER_JAEGER_ENDPOINT', 'http://loca
password: ENV['OTEL_EXPORTER_JAEGER_PASSWORD'],
timeout: ENV.fetch('OTEL_EXPORTER_JAEGER_TIMEOUT', 10),
ssl_verify_mode: CollectorExporter.ssl_verify_mode)
raise ArgumentError, "invalid url for Jaeger::CollectorExporter #{endpoint}" if invalid_url?(endpoint)
raise ArgumentError, "invalid url for Jaeger::CollectorExporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)
raise ArgumentError, 'username and password should either both be nil or both be set' if username.nil? != password.nil?

transport_opts = { ssl_verify_mode: Integer(ssl_verify_mode) }
Expand Down Expand Up @@ -88,15 +88,6 @@ def shutdown(timeout: nil)

private

def invalid_url?(url)
return true if url.nil? || url.strip.empty?

URI(url)
false
rescue URI::InvalidURIError
true
end

def encoded_batches(span_data)
span_data.group_by(&:resource).map do |resource, spans|
process = Encoder.encoded_process(resource)
Expand Down
2 changes: 1 addition & 1 deletion exporter/jaeger/opentelemetry-exporter-jaeger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.19.3'
spec.add_dependency 'opentelemetry-common', '~> 0.19.6'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_dependency 'opentelemetry-semantic_conventions'
spec.add_dependency 'thrift'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class Exporter
FAILURE = OpenTelemetry::SDK::Trace::Export::FAILURE
private_constant(:SUCCESS, :FAILURE)

def initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4317/v1/traces'),
timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
def initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4317/v1/traces'),
timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
metrics_reporter: nil)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?(endpoint)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)

uri = URI(endpoint)

Expand Down Expand Up @@ -71,25 +71,6 @@ def shutdown(timeout: nil)
@shutdown = true
SUCCESS
end

private

def config_opt(*env_vars, default: nil)
env_vars.each do |env_var|
val = ENV[env_var]
return val unless val.nil?
end
default
end

def invalid_url?(url)
return true if url.nil? || url.strip.empty?

URI(url)
false
rescue URI::InvalidURIError
true
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'grpc'
spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.19.3'
spec.add_dependency 'opentelemetry-common', '~> 0.19.6'
spec.add_dependency 'opentelemetry-exporter-otlp-common'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class Exporter # rubocop:disable Metrics/ClassLength
ERROR_MESSAGE_INVALID_HEADERS = 'headers must be a String with comma-separated URL Encoded UTF-8 k=v pairs or a Hash'
private_constant(:ERROR_MESSAGE_INVALID_HEADERS)

def initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
def initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
ssl_verify_mode: fetch_ssl_verify_mode,
headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
headers: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
compression: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
metrics_reporter: nil)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?(endpoint)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)
raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || %w[gzip none].include?(compression)

@uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
Expand Down Expand Up @@ -117,23 +117,6 @@ def http_connection(uri, ssl_verify_mode, certificate_file)
http
end

def config_opt(*env_vars, default: nil)
env_vars.each do |env_var|
val = ENV[env_var]
return val unless val.nil?
end
default
end

def invalid_url?(url)
return true if url.nil? || url.strip.empty?

URI(url)
false
rescue URI::InvalidURIError
true
end

# The around_request is a private method that provides an extension
# point for the exporters network calls. The default behaviour
# is to not trace these operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.19.3'
spec.add_dependency 'opentelemetry-common', '~> 0.19.6'
spec.add_dependency 'opentelemetry-exporter-otlp-common'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'

Expand Down
29 changes: 6 additions & 23 deletions exporter/otlp/lib/opentelemetry/exporter/otlp/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def self.ssl_verify_mode
end
end

def initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
def initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
ssl_verify_mode: Exporter.ssl_verify_mode,
headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
headers: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
compression: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
metrics_reporter: nil)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?(endpoint)
raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)
raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || %w[gzip none].include?(compression)

@uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
Expand Down Expand Up @@ -120,23 +120,6 @@ def http_connection(uri, ssl_verify_mode, certificate_file)
http
end

def config_opt(*env_vars, default: nil)
env_vars.each do |env_var|
val = ENV[env_var]
return val unless val.nil?
end
default
end

def invalid_url?(url)
return true if url.nil? || url.strip.empty?

URI(url)
false
rescue URI::InvalidURIError
true
end

# The around_request is a private method that provides an extension
# point for the exporters network calls. The default behaviour
# is to not trace these operations.
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlp/opentelemetry-exporter-otlp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'google-protobuf', '~> 3.19'
spec.add_dependency 'googleapis-common-protos-types', '~> 1.3'
spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.19.3'
spec.add_dependency 'opentelemetry-common', '~> 0.19.6'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_dependency 'opentelemetry-semantic_conventions'

Expand Down
25 changes: 4 additions & 21 deletions exporter/zipkin/lib/opentelemetry/exporter/zipkin/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class Exporter # rubocop:disable Metrics/ClassLength
WRITE_TIMEOUT_SUPPORTED = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6')
private_constant(:KEEP_ALIVE_TIMEOUT, :RETRY_COUNT, :WRITE_TIMEOUT_SUPPORTED)

def initialize(endpoint: config_opt('OTEL_EXPORTER_ZIPKIN_ENDPOINT', default: 'http://localhost:9411/api/v2/spans'),
headers: config_opt('OTEL_EXPORTER_ZIPKIN_TRACES_HEADERS', 'OTEL_EXPORTER_ZIPKIN_HEADERS'),
timeout: config_opt('OTEL_EXPORTER_ZIPKIN_TRACES_TIMEOUT', 'OTEL_EXPORTER_ZIPKIN_TIMEOUT', default: 10))
raise ArgumentError, "invalid url for Zipkin::Exporter #{endpoint}" if invalid_url?(endpoint)
def initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_ZIPKIN_ENDPOINT', default: 'http://localhost:9411/api/v2/spans'),
headers: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_ZIPKIN_TRACES_HEADERS', 'OTEL_EXPORTER_ZIPKIN_HEADERS'),
timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_ZIPKIN_TRACES_TIMEOUT', 'OTEL_EXPORTER_ZIPKIN_TIMEOUT', default: 10))
raise ArgumentError, "invalid url for Zipkin::Exporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)
raise ArgumentError, 'headers must be comma-separated k=v pairs or a Hash' unless valid_headers?(headers)

@uri = if endpoint == ENV['OTEL_EXPORTER_ZIPKIN_ENDPOINT']
Expand Down Expand Up @@ -94,14 +94,6 @@ def shutdown(timeout: nil)

private

def config_opt(*env_vars, default: nil)
env_vars.each do |env_var|
val = ENV[env_var]
return val unless val.nil?
end
default
end

def encode_spans(span_data)
span_data.map! { |span| Transformer.to_zipkin_span(span, span.resource) }
end
Expand All @@ -110,15 +102,6 @@ def around_request
OpenTelemetry::Common::Utilities.untraced { yield }
end

def invalid_url?(url)
return true if url.nil? || url.strip.empty?

URI(url)
false
rescue URI::InvalidURIError
true
end

def valid_headers?(headers)
return true if headers.nil? || headers.is_a?(Hash)
return false unless headers.is_a?(String)
Expand Down
2 changes: 1 addition & 1 deletion exporter/zipkin/opentelemetry-exporter-zipkin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.6.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.19.3'
spec.add_dependency 'opentelemetry-common', '~> 0.19.6'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_dependency 'opentelemetry-semantic_conventions'

Expand Down

0 comments on commit 5a3c2fb

Please sign in to comment.