opentelemetry-instrumentation-rack浅析
如何构建上下文
编写自己的服务端链路跟踪拦截器时,可参考它
call
方法中抽取上下文的做法# opentelemetry-instrumentation-rack-0.20.1/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb
module OpenTelemetry
module Instrumentation
module Rack
module Middlewares
class TracerMiddleware
def call(env)
if untraced_request?(env)
OpenTelemetry::Common::Utilities.untraced do
return @app.call(env)
end
end
original_env = env.dup
extracted_context = OpenTelemetry.propagation.extract(
env,
getter: OpenTelemetry::Common::Propagation.rack_env_getter
)
frontend_context = create_frontend_span(env, extracted_context)
# restore extracted context in this process:
OpenTelemetry::Context.with_current(frontend_context || extracted_context) do
request_span_name = create_request_span_name(env['REQUEST_URI'] || original_env['PATH_INFO'], env)
request_span_kind = frontend_context.nil? ? :server : :internal
tracer.in_span(request_span_name,
attributes: request_span_attributes(env: env),
kind: request_span_kind) do |request_span|
OpenTelemetry::Instrumentation::Rack.with_span(request_span) do
@app.call(env).tap do |status, headers, response|
set_attributes_after_request(request_span, status, headers, response)
end
end
end
end
ensure
finish_span(frontend_context)
end
end
end
end
end
end
配置
allowed_request_headers
如果header中有,就会记录到attribute里
例如
c.use_all({
'OpenTelemetry::Instrumentation::Rack' => {
allowed_request_headers: ['CALLER_NAME']
}
}
})
测试
curl -H "CALLER_NAME: local" $APISET_REQ_BIN
配置
url_quantization
例如
c.use_all({
'OpenTelemetry::Instrumentation::Rack' => {
url_quantization: ->(path, env) {
path = path.sub(/\?.*/, '')
path = path.split('/').map{ |s| s =~ /^\d+$/ ? '_num' : s }.join('/')
req_method = env['REQUEST_METHOD']
"HTTP #{req_method} #{path}"
}
}
})