ruby的opentelemetry-exporter-zipkin
使用
启动zipkin
[[使用zipkin]]
文档
配置参数
Parameter | Environment variable | Default |
---|---|---|
endpoint: | OTELEXPORTERZIPKIN_ENDPOINT | "http://localhost:9411" |
headers: | OTELEXPORTERZIPKINTRACESHEADERS | nil |
timeoout: | OTELEXPORTERZIPKINTRACESTIMEOUT | 10 |
OTELTRACESEXPORTER | zipkin |
Gemfile
gem 'opentelemetry-exporter-zipkin'
gem 'opentelemetry-instrumentation-all'
对接
(注意通过环境变量
OTEL_SERVICE_NAME
设置服务名的问题:因为zipkin会先加载OpenTelemetry::SDK::Resources::Resource.default
,这时就已经要读取OTEL_SERVICE_NAME
了)require 'opentelemetry/exporter/zipkin'
ENV['OTEL_BSP_MAX_EXPORT_BATCH_SIZE'] = 8.to_s
OpenTelemetry::SDK.configure do |c|
c.service_name = 'little-survey'
export = OpenTelemetry::Exporter::Zipkin::Exporter.new
span_processor = OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter)
c.add_span_processor(span_processor)
# c.use_all #使用所有可用的监控
end
源码
初始化时可配置zipkin地址、http header、超时控制
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']
URI("#{endpoint}/api/v2/spans")
else
URI(endpoint)
end
@http = Net::HTTP.new(@uri.host, @uri.port)
@http.use_ssl = @uri.scheme == 'https'
@http.keep_alive_timeout = KEEP_ALIVE_TIMEOUT
@timeout = timeout.to_f
@path = @uri.path
@headers = case headers
when String then CSV.parse(headers, col_sep: '=', row_sep: ',').to_h
when Hash then headers
end
@shutdown = false
end