libo11y 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +71 -1
- data/README.md +7 -4
- data/exe/libo11y +8 -4
- data/lib/libo11y/db.rb +2 -1
- data/lib/libo11y/detached.rb +158 -51
- data/lib/libo11y/http_client.rb +83 -61
- data/lib/libo11y/internal/bridges.rb +107 -3
- data/lib/libo11y/internal/env.rb +91 -16
- data/lib/libo11y/internal/histogram_buckets.rb +21 -0
- data/lib/libo11y/internal/http_common.rb +39 -0
- data/lib/libo11y/internal/logging_device.rb +81 -90
- data/lib/libo11y/internal/pipeline.rb +220 -0
- data/lib/libo11y/internal/policy.rb +36 -23
- data/lib/libo11y/internal/propagators.rb +8 -11
- data/lib/libo11y/internal/resource.rb +96 -92
- data/lib/libo11y/internal/runtime_metrics.rb +32 -46
- data/lib/libo11y/internal/summary.rb +55 -0
- data/lib/libo11y/options.rb +16 -2
- data/lib/libo11y/proc.rb +6 -6
- data/lib/libo11y/rack.rb +175 -135
- data/lib/libo11y/record.rb +11 -7
- data/lib/libo11y/spec/app_registry.rb +6 -5
- data/lib/libo11y/spec/yaml_v3.rb +81 -59
- data/lib/libo11y/telemetry.rb +185 -320
- data/lib/libo11y/testing.rb +105 -56
- data/lib/libo11y/version.rb +1 -1
- metadata +19 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "logger"
|
|
4
|
+
require_relative "endpoint"
|
|
5
|
+
|
|
6
|
+
module Libo11y
|
|
7
|
+
module Internal
|
|
8
|
+
# The resolved configuration, as the attributes of the
|
|
9
|
+
# o11y.telemetry.configured event and as one line. It reports what
|
|
10
|
+
# happens after options have been weighed against the environment, with
|
|
11
|
+
# the attribute names the registry defines and go/summary.go writes.
|
|
12
|
+
module Summary
|
|
13
|
+
LEVELS = {
|
|
14
|
+
::Logger::DEBUG => "DEBUG", ::Logger::INFO => "INFO", ::Logger::WARN => "WARN",
|
|
15
|
+
::Logger::ERROR => "ERROR", ::Logger::FATAL => "FATAL", ::Logger::UNKNOWN => "ANY"
|
|
16
|
+
}.freeze
|
|
17
|
+
DEFAULT_SAMPLER = "parentbased_always_on"
|
|
18
|
+
# What the summary reports for propagators that declare no header field.
|
|
19
|
+
FIELDLESS = "custom"
|
|
20
|
+
ENDPOINTS = {
|
|
21
|
+
"endpoint" => "OTEL_EXPORTER_OTLP_ENDPOINT",
|
|
22
|
+
"traces_endpoint" => "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
|
|
23
|
+
"metrics_endpoint" => "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
|
|
24
|
+
"logs_endpoint" => "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
# @return [Array(Hash, String)] the event attributes and the line
|
|
30
|
+
def build(options:, env_map:, identity:, level:, exporters:, propagator:)
|
|
31
|
+
attrs = { "service.name" => identity.service_name, "service.version" => identity.service_version }
|
|
32
|
+
%i[traces metrics logs].each { |signal| attrs["o11y.config.#{signal}"] = exporters.fetch(signal) }
|
|
33
|
+
attrs["o11y.config.console"] = options.console_enabled ? "json" : "off"
|
|
34
|
+
attrs["o11y.config.level"] = LEVELS.fetch(level, level.to_s)
|
|
35
|
+
attrs["o11y.config.sampler"] = sampler(options, env_map)
|
|
36
|
+
attrs["o11y.config.propagators"] = propagator.fields.empty? ? FIELDLESS : propagator.fields.join(",")
|
|
37
|
+
attrs["o11y.config.runtime_metrics"] = exporters.fetch(:runtime_metrics).to_s
|
|
38
|
+
attrs["o11y.config.service_version_fallback"] = "true" if identity.version_fallback
|
|
39
|
+
# An endpoint may carry credentials in its userinfo or query, and the
|
|
40
|
+
# summary goes to every sink, so each one is sanitized.
|
|
41
|
+
ENDPOINTS.each do |key, name|
|
|
42
|
+
raw = Env.value(env_map, name)
|
|
43
|
+
attrs["o11y.config.#{key}"] = Endpoint.sanitize(raw) if raw
|
|
44
|
+
end
|
|
45
|
+
[attrs.freeze, attrs.map { |k, v| "#{k}=#{v}" }.join(" ").freeze]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def sampler(options, env_map)
|
|
49
|
+
return options.sampler.description if options.sampler
|
|
50
|
+
|
|
51
|
+
(Env.value(env_map, "OTEL_TRACES_SAMPLER") || DEFAULT_SAMPLER).downcase
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
data/lib/libo11y/options.rb
CHANGED
|
@@ -57,7 +57,7 @@ module Libo11y
|
|
|
57
57
|
:runtime_metrics,
|
|
58
58
|
:console_writer,
|
|
59
59
|
:console_enabled,
|
|
60
|
-
:
|
|
60
|
+
:log_level,
|
|
61
61
|
:extra_log_sinks,
|
|
62
62
|
:span_exporter,
|
|
63
63
|
:span_processors,
|
|
@@ -67,7 +67,21 @@ module Libo11y
|
|
|
67
67
|
:shutdown_timeout,
|
|
68
68
|
keyword_init: true
|
|
69
69
|
) do
|
|
70
|
+
# An Options object with the keywords laid over it. The object given is
|
|
71
|
+
# not changed.
|
|
72
|
+
#
|
|
73
|
+
# @raise [ArgumentError] for a keyword that is not an option
|
|
74
|
+
def self.merge(options, **kwargs)
|
|
75
|
+
return new(**kwargs) if options.nil?
|
|
76
|
+
raise ArgumentError, "libo11y: options must be a Libo11y::Options, got #{options.class}" unless options.is_a?(Options)
|
|
77
|
+
|
|
78
|
+
new(**options.to_h, **kwargs)
|
|
79
|
+
end
|
|
80
|
+
|
|
70
81
|
def initialize(**kwargs)
|
|
82
|
+
unknown = kwargs.keys - self.class.members
|
|
83
|
+
raise ArgumentError, "libo11y: unknown option#{"s" if unknown.length > 1} #{unknown.join(", ")}" unless unknown.empty?
|
|
84
|
+
|
|
71
85
|
defaults = {
|
|
72
86
|
extra_environments: [],
|
|
73
87
|
strict_identity: true,
|
|
@@ -78,7 +92,7 @@ module Libo11y
|
|
|
78
92
|
app_registries: nil,
|
|
79
93
|
runtime_metrics: true,
|
|
80
94
|
console_enabled: true,
|
|
81
|
-
|
|
95
|
+
log_level: ::Logger::INFO,
|
|
82
96
|
extra_log_sinks: [],
|
|
83
97
|
span_processors: [],
|
|
84
98
|
log_processors: [],
|
data/lib/libo11y/proc.rb
CHANGED
|
@@ -19,12 +19,12 @@ module Libo11y
|
|
|
19
19
|
out
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
# The environment for a child process: base_env with TRACEPARENT and
|
|
23
|
+
# TRACESTATE set from the active span. A variable the span does not set
|
|
24
|
+
# maps to nil, which makes Process.spawn remove it from the child, so the
|
|
25
|
+
# child never inherits a stale trace from this process.
|
|
26
|
+
def child_env(base_env = {}, context: OpenTelemetry::Context.current)
|
|
27
|
+
base_env.to_h.merge(TRACEPARENT_ENV => nil, TRACESTATE_ENV => nil).merge(env_carrier(context: context))
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def extract_env(env_map = ENV, context: OpenTelemetry::Context.current)
|
data/lib/libo11y/rack.rb
CHANGED
|
@@ -4,33 +4,49 @@ require "uri"
|
|
|
4
4
|
require "opentelemetry"
|
|
5
5
|
require_relative "record"
|
|
6
6
|
require_relative "spec"
|
|
7
|
+
require_relative "internal/http_common"
|
|
7
8
|
|
|
8
9
|
module Libo11y
|
|
9
|
-
# Rack middleware (Rails, Sinatra, Hanami, Roda)
|
|
10
|
-
#
|
|
10
|
+
# Rack middleware (Rails, Sinatra, Hanami, Roda). It records a server span
|
|
11
|
+
# with the stable HTTP semantic conventions and the
|
|
12
|
+
# http.server.request.duration histogram, and can add a Server-Timing entry
|
|
13
|
+
# and a canonical log line.
|
|
11
14
|
module Rack
|
|
12
|
-
HTTP_DURATION_BUCKETS =
|
|
15
|
+
HTTP_DURATION_BUCKETS = Internal::HttpCommon::DURATION_BUCKETS
|
|
13
16
|
ROUTE_FIBER_KEY = :libo11y_http_route
|
|
17
|
+
SCOPE_NAME = "libo11y.rack"
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
# Sets the route template of the request this thread is serving, such as
|
|
20
|
+
# "/orders/:id", for a router the middleware does not know.
|
|
21
|
+
def self.set_route(route) # rubocop:disable Naming/AccessorMethodName -- the name every libo11y implementation gives it
|
|
16
22
|
Thread.current[ROUTE_FIBER_KEY] = route.to_s
|
|
17
23
|
end
|
|
18
24
|
|
|
25
|
+
# Matches a path against an ignore pattern as Go's path.Match does: `*`
|
|
26
|
+
# stays within one path segment and also matches a leading dot.
|
|
19
27
|
def self.match_glob?(pattern, path)
|
|
20
|
-
File.fnmatch?(pattern, path, File::FNM_PATHNAME)
|
|
28
|
+
File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
|
|
21
29
|
end
|
|
22
30
|
|
|
23
31
|
class Middleware
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
# @param telemetry [Libo11y::Telemetry, nil] the handle to record
|
|
33
|
+
# through. Without one the middleware uses the global providers.
|
|
34
|
+
# @param route [#call, nil] returns the route template for a Rack env
|
|
35
|
+
# @param public_endpoint [Boolean] start a new trace and link the
|
|
36
|
+
# caller's span, for a server that untrusted clients reach
|
|
37
|
+
# @param ignore_paths [Array<String>] patterns of paths to leave alone
|
|
38
|
+
# @param filter [#call, nil] returns false for an env to leave alone
|
|
39
|
+
# @param server_timing [Boolean] add a Server-Timing entry that names
|
|
40
|
+
# the server span to HTML responses
|
|
41
|
+
# @param canonical_line [Boolean] emit one o11y.request.summary event
|
|
42
|
+
# per request. It needs telemetry.
|
|
43
|
+
# @raise [ArgumentError] for canonical_line without telemetry
|
|
44
|
+
def initialize(app, telemetry: nil, route: nil, public_endpoint: false, ignore_paths: [], filter: nil,
|
|
45
|
+
server_timing: false, canonical_line: false)
|
|
46
|
+
if canonical_line && telemetry.nil?
|
|
47
|
+
raise ArgumentError, "libo11y: canonical_line needs telemetry: the event is written through a Telemetry handle"
|
|
48
|
+
end
|
|
49
|
+
|
|
34
50
|
@app = app
|
|
35
51
|
@telemetry = telemetry
|
|
36
52
|
@route_fn = route
|
|
@@ -39,163 +55,187 @@ module Libo11y
|
|
|
39
55
|
@filter = filter
|
|
40
56
|
@server_timing = server_timing
|
|
41
57
|
@canonical_line = canonical_line
|
|
58
|
+
@mutex = Mutex.new
|
|
59
|
+
@duration = nil
|
|
42
60
|
end
|
|
43
61
|
|
|
44
62
|
def call(env)
|
|
45
|
-
|
|
46
|
-
|
|
63
|
+
path = env["PATH_INFO"].to_s
|
|
64
|
+
return @app.call(env) if ignored?(decode_path(path), env)
|
|
47
65
|
|
|
48
|
-
|
|
66
|
+
previous_route = Thread.current[ROUTE_FIBER_KEY]
|
|
67
|
+
Thread.current[ROUTE_FIBER_KEY] = nil
|
|
68
|
+
begin
|
|
69
|
+
serve(env, path)
|
|
70
|
+
ensure
|
|
71
|
+
Thread.current[ROUTE_FIBER_KEY] = previous_route
|
|
72
|
+
end
|
|
73
|
+
end
|
|
49
74
|
|
|
50
|
-
|
|
51
|
-
scheme = env["rack.url_scheme"].to_s
|
|
52
|
-
scheme = "http" if scheme.empty?
|
|
75
|
+
private
|
|
53
76
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
extracted_ctx = propagator.extract(headers, context: OpenTelemetry::Context.empty)
|
|
57
|
-
|
|
58
|
-
parent_ctx = extracted_ctx
|
|
59
|
-
links = nil
|
|
60
|
-
if @public_endpoint
|
|
61
|
-
remote_sc = OpenTelemetry::Trace.current_span(extracted_ctx).context
|
|
62
|
-
links = [OpenTelemetry::Trace::Link.new(remote_sc)] if remote_sc.valid?
|
|
63
|
-
parent_ctx = OpenTelemetry::Context.empty
|
|
64
|
-
end
|
|
77
|
+
Request = Struct.new(:verb, :scheme, :path, :started, :status, :error)
|
|
78
|
+
private_constant :Request
|
|
65
79
|
|
|
66
|
-
|
|
67
|
-
|
|
80
|
+
def serve(env, path)
|
|
81
|
+
method, original = Internal::HttpCommon.normalize_method(env["REQUEST_METHOD"].to_s)
|
|
82
|
+
scheme = env["rack.url_scheme"].to_s
|
|
83
|
+
request = Request.new(method, scheme.empty? ? "http" : scheme, path, Internal::HttpCommon.monotonic, 500, nil)
|
|
84
|
+
parent, links = parent_context(env)
|
|
85
|
+
route = resolve_route(env)
|
|
68
86
|
|
|
69
|
-
attrs = {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"url.scheme" => scheme
|
|
73
|
-
}
|
|
74
|
-
attrs["http.route"] = resolved_route if resolved_route
|
|
87
|
+
attrs = { "http.request.method" => method, "url.path" => path, "url.scheme" => request.scheme }
|
|
88
|
+
attrs["http.request.method_original"] = original if original
|
|
89
|
+
attrs["http.route"] = route if route
|
|
75
90
|
attrs["server.address"] = env["SERVER_NAME"].to_s unless env["SERVER_NAME"].to_s.empty?
|
|
76
91
|
attrs["server.port"] = env["SERVER_PORT"].to_i unless env["SERVER_PORT"].to_s.empty?
|
|
77
92
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
93
|
-
prev_route = Thread.current[ROUTE_FIBER_KEY]
|
|
94
|
-
Thread.current[ROUTE_FIBER_KEY] = nil
|
|
95
|
-
|
|
96
|
-
status = 500
|
|
97
|
-
resp_headers = {}
|
|
98
|
-
body = nil
|
|
99
|
-
error = nil
|
|
100
|
-
|
|
101
|
-
OpenTelemetry::Context.with_current(parent_ctx) do
|
|
102
|
-
tracer.in_span(span_name, attributes: attrs, links: links, kind: :server) do |span|
|
|
103
|
-
Libo11y.start_record do |rec|
|
|
104
|
-
status, resp_headers, body = @app.call(env)
|
|
105
|
-
status = status.to_i
|
|
106
|
-
rescue StandardError => e
|
|
107
|
-
error = e
|
|
108
|
-
span.record_exception(e)
|
|
109
|
-
span.status = OpenTelemetry::Trace::Status.error(e.message)
|
|
110
|
-
span.set_attribute("error.type", e.class.name)
|
|
111
|
-
raise
|
|
112
|
-
ensure
|
|
113
|
-
final_route = Thread.current[ROUTE_FIBER_KEY] || resolved_route || resolve_route(env)
|
|
114
|
-
if final_route && !final_route.empty?
|
|
115
|
-
span.set_attribute("http.route", final_route)
|
|
116
|
-
span.name = "#{method} #{final_route}"
|
|
117
|
-
end
|
|
118
|
-
span.set_attribute("http.response.status_code", status)
|
|
119
|
-
if status >= 500 && error.nil?
|
|
120
|
-
span.status = OpenTelemetry::Trace::Status.error("HTTP #{status}")
|
|
121
|
-
span.set_attribute("error.type", status.to_s)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
125
|
-
metric_attrs = {
|
|
126
|
-
"http.request.method" => method,
|
|
127
|
-
"http.response.status_code" => status,
|
|
128
|
-
"url.scheme" => scheme
|
|
129
|
-
}
|
|
130
|
-
metric_attrs["http.route"] = final_route if final_route && !final_route.empty?
|
|
131
|
-
metric_attrs["error.type"] = error ? error.class.name : status.to_s if error || status >= 500
|
|
132
|
-
@duration_hist.record(duration, attributes: metric_attrs)
|
|
133
|
-
|
|
134
|
-
if @server_timing && resp_headers.is_a?(Hash) && error.nil?
|
|
135
|
-
sc = span.context
|
|
136
|
-
if sc.valid?
|
|
137
|
-
flags = sc.trace_flags.sampled? ? "01" : "00"
|
|
138
|
-
tp = "00-#{sc.hex_trace_id}-#{sc.hex_span_id}-#{flags}"
|
|
139
|
-
resp_headers = resp_headers.dup
|
|
140
|
-
existing = resp_headers["server-timing"] || resp_headers["Server-Timing"]
|
|
141
|
-
entry = "traceparent;desc=\"#{tp}\""
|
|
142
|
-
resp_headers["server-timing"] = existing ? "#{existing}, #{entry}" : entry
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
if @canonical_line && @telemetry
|
|
147
|
-
summary_attrs = rec.attributes.merge(
|
|
148
|
-
"http.request.method" => method,
|
|
149
|
-
"http.response.status_code" => status,
|
|
150
|
-
"url.path" => raw_path,
|
|
151
|
-
Spec::ATTR_DURATION => duration,
|
|
152
|
-
Spec::ATTR_OUTCOME => error || status >= 500 ? Spec::Outcome::FAILURE : Spec::Outcome::SUCCESS
|
|
153
|
-
)
|
|
154
|
-
summary_attrs["http.route"] = final_route if final_route && !final_route.empty?
|
|
155
|
-
summary_attrs["error.type"] = error.class.name if error
|
|
156
|
-
@telemetry.event(Spec::EVENT_REQUEST_SUMMARY, summary_attrs)
|
|
93
|
+
response = nil
|
|
94
|
+
OpenTelemetry::Context.with_current(parent) do
|
|
95
|
+
span = tracer.start_span(Internal::HttpCommon.span_name(method, route), attributes: attrs, links: links, kind: :server)
|
|
96
|
+
begin
|
|
97
|
+
OpenTelemetry::Trace.with_span(span) do
|
|
98
|
+
Libo11y.start_record do |record|
|
|
99
|
+
response = @app.call(env)
|
|
100
|
+
request.status = response[0].to_i
|
|
101
|
+
rescue StandardError => e
|
|
102
|
+
request.error = e
|
|
103
|
+
raise
|
|
104
|
+
ensure
|
|
105
|
+
finish(span, record, request, route || resolve_route(env))
|
|
157
106
|
end
|
|
158
107
|
end
|
|
108
|
+
response = with_server_timing(response, span) if @server_timing
|
|
109
|
+
ensure
|
|
110
|
+
span.finish
|
|
159
111
|
end
|
|
160
112
|
end
|
|
113
|
+
response
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Completes the span, records the duration and writes the canonical line.
|
|
117
|
+
def finish(span, record, request, route)
|
|
118
|
+
route = Thread.current[ROUTE_FIBER_KEY] || route
|
|
119
|
+
route = nil if route.to_s.empty?
|
|
120
|
+
if route
|
|
121
|
+
span.set_attribute("http.route", route)
|
|
122
|
+
span.name = Internal::HttpCommon.span_name(request.verb, route)
|
|
123
|
+
end
|
|
124
|
+
span.set_attribute("http.response.status_code", request.status)
|
|
125
|
+
|
|
126
|
+
error_type = request.error.class.name if request.error
|
|
127
|
+
error_type ||= request.status.to_s if request.status >= 500
|
|
128
|
+
if error_type
|
|
129
|
+
span.record_exception(request.error) if request.error
|
|
130
|
+
span.set_attribute("error.type", error_type)
|
|
131
|
+
span.status = OpenTelemetry::Trace::Status.error
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
elapsed = Internal::HttpCommon.monotonic - request.started
|
|
135
|
+
metric_attrs = { "http.request.method" => request.verb, "http.response.status_code" => request.status, "url.scheme" => request.scheme }
|
|
136
|
+
metric_attrs["http.route"] = route if route
|
|
137
|
+
metric_attrs["error.type"] = error_type if error_type
|
|
138
|
+
duration.record(elapsed, attributes: metric_attrs)
|
|
161
139
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
140
|
+
return unless @canonical_line
|
|
141
|
+
|
|
142
|
+
summary = record.attributes.merge(
|
|
143
|
+
"http.request.method" => request.verb,
|
|
144
|
+
"http.response.status_code" => request.status,
|
|
145
|
+
"url.path" => request.path,
|
|
146
|
+
Spec::ATTR_DURATION => elapsed,
|
|
147
|
+
Spec::ATTR_OUTCOME => error_type ? Spec::Outcome::FAILURE : Spec::Outcome::SUCCESS
|
|
148
|
+
)
|
|
149
|
+
summary["http.route"] = route if route
|
|
150
|
+
summary["error.type"] = error_type if error_type
|
|
151
|
+
@telemetry.event(Spec::EVENT_REQUEST_SUMMARY, summary)
|
|
165
152
|
end
|
|
166
153
|
|
|
167
|
-
|
|
154
|
+
# The context the server span starts in, and its links. A public
|
|
155
|
+
# endpoint starts a new trace and links the caller's span.
|
|
156
|
+
def parent_context(env)
|
|
157
|
+
propagator = @telemetry ? @telemetry.propagator : OpenTelemetry.propagation
|
|
158
|
+
extracted = propagator.extract(request_headers(env), context: OpenTelemetry::Context.empty)
|
|
159
|
+
return [extracted, nil] unless @public_endpoint
|
|
160
|
+
|
|
161
|
+
remote = OpenTelemetry::Trace.current_span(extracted).context
|
|
162
|
+
[OpenTelemetry::Context.empty, remote.valid? ? [OpenTelemetry::Trace::Link.new(remote)] : nil]
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Returns the response with a Server-Timing entry that names the server
|
|
166
|
+
# span, when the response is HTML. An entry the application set stays,
|
|
167
|
+
# and the new one goes after it. The application's own headers are not
|
|
168
|
+
# changed.
|
|
169
|
+
def with_server_timing(response, span)
|
|
170
|
+
status, headers, body = response
|
|
171
|
+
context = span.context
|
|
172
|
+
return response unless headers.is_a?(Hash) && context.valid? && html?(headers)
|
|
173
|
+
|
|
174
|
+
flags = context.trace_flags.sampled? ? "01" : "00"
|
|
175
|
+
entry = "traceparent;desc=\"00-#{context.hex_trace_id}-#{context.hex_span_id}-#{flags}\""
|
|
176
|
+
key = header_key(headers, "server-timing") || "server-timing"
|
|
177
|
+
existing = headers[key]
|
|
178
|
+
headers = headers.dup
|
|
179
|
+
headers[key] = case existing
|
|
180
|
+
when nil, "" then entry
|
|
181
|
+
when Array then existing + [entry]
|
|
182
|
+
else "#{existing}, #{entry}"
|
|
183
|
+
end
|
|
184
|
+
[status, headers, body]
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def html?(headers)
|
|
188
|
+
key = header_key(headers, "content-type")
|
|
189
|
+
!key.nil? && Array(headers[key]).first.to_s.strip.downcase.start_with?("text/html")
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Rack 3 header names are lower case. Rack 2 applications use any case.
|
|
193
|
+
def header_key(headers, name)
|
|
194
|
+
headers.each_key.find { |key| key.to_s.casecmp?(name) }
|
|
195
|
+
end
|
|
168
196
|
|
|
169
197
|
def ignored?(decoded_path, env)
|
|
170
|
-
return true if @ignore_paths.any? { |
|
|
171
|
-
return true if @filter && !@filter.call(env)
|
|
198
|
+
return true if @ignore_paths.any? { |pattern| Rack.match_glob?(pattern, decoded_path) }
|
|
172
199
|
|
|
173
|
-
|
|
200
|
+
!@filter.nil? && !@filter.call(env)
|
|
174
201
|
end
|
|
175
202
|
|
|
176
203
|
def decode_path(raw)
|
|
177
204
|
URI.decode_www_form_component(raw.gsub("+", "%2B"))
|
|
178
|
-
rescue
|
|
205
|
+
rescue ArgumentError
|
|
179
206
|
raw
|
|
180
207
|
end
|
|
181
208
|
|
|
182
|
-
def
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
next unless k.is_a?(String) && k.start_with?("HTTP_")
|
|
209
|
+
def request_headers(env)
|
|
210
|
+
env.each_with_object({}) do |(key, value), headers|
|
|
211
|
+
next unless key.is_a?(String) && key.start_with?("HTTP_")
|
|
186
212
|
|
|
187
|
-
|
|
188
|
-
headers[header_name] = v.to_s
|
|
213
|
+
headers[key.delete_prefix("HTTP_").downcase.tr("_", "-")] = value.to_s
|
|
189
214
|
end
|
|
190
|
-
headers
|
|
191
215
|
end
|
|
192
216
|
|
|
193
217
|
def resolve_route(env)
|
|
194
218
|
return @route_fn.call(env) if @route_fn.respond_to?(:call)
|
|
195
219
|
|
|
196
|
-
# Sinatra
|
|
220
|
+
# Sinatra and Rails
|
|
197
221
|
env["sinatra.route"]&.split(" ", 2)&.last || env["action_dispatch.route_uri_pattern"]
|
|
198
222
|
end
|
|
223
|
+
|
|
224
|
+
def tracer
|
|
225
|
+
provider = @telemetry ? @telemetry.tracer_provider : OpenTelemetry.tracer_provider
|
|
226
|
+
provider.tracer(SCOPE_NAME, Libo11y::VERSION)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def duration
|
|
230
|
+
@mutex.synchronize do
|
|
231
|
+
@duration ||= begin
|
|
232
|
+
provider = @telemetry ? @telemetry.meter_provider : OpenTelemetry.meter_provider
|
|
233
|
+
provider.meter(SCOPE_NAME, version: Libo11y::VERSION).create_histogram(
|
|
234
|
+
"http.server.request.duration", unit: "s", description: "Duration of HTTP server requests."
|
|
235
|
+
)
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
end
|
|
199
239
|
end
|
|
200
240
|
end
|
|
201
241
|
end
|
data/lib/libo11y/record.rb
CHANGED
|
@@ -13,7 +13,7 @@ module Libo11y
|
|
|
13
13
|
self
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def set_all(hash)
|
|
16
|
+
def set_all(hash) # rubocop:disable Naming/AccessorMethodName -- it merges into the record and returns it, as set does
|
|
17
17
|
return self if hash.nil?
|
|
18
18
|
|
|
19
19
|
@mutex.synchronize do
|
|
@@ -48,16 +48,20 @@ module Libo11y
|
|
|
48
48
|
Thread.current[RECORD_FIBER_KEY]
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
+
# Runs the block with a new Record as the current one, and yields it.
|
|
52
|
+
# Returns the block's value.
|
|
53
|
+
#
|
|
54
|
+
# @raise [ArgumentError] without a block
|
|
51
55
|
def self.start_record
|
|
52
|
-
|
|
53
|
-
prev = Thread.current[RECORD_FIBER_KEY]
|
|
54
|
-
Thread.current[RECORD_FIBER_KEY] = rec
|
|
55
|
-
return rec unless block_given?
|
|
56
|
+
raise ArgumentError, "libo11y: start_record needs a block" unless block_given?
|
|
56
57
|
|
|
58
|
+
record = Record.new
|
|
59
|
+
previous = Thread.current[RECORD_FIBER_KEY]
|
|
60
|
+
Thread.current[RECORD_FIBER_KEY] = record
|
|
57
61
|
begin
|
|
58
|
-
yield
|
|
62
|
+
yield record
|
|
59
63
|
ensure
|
|
60
|
-
Thread.current[RECORD_FIBER_KEY] =
|
|
64
|
+
Thread.current[RECORD_FIBER_KEY] = previous
|
|
61
65
|
end
|
|
62
66
|
end
|
|
63
67
|
end
|
|
@@ -123,7 +123,7 @@ module Libo11y
|
|
|
123
123
|
aid = raw.object_id
|
|
124
124
|
raise YamlV3::SyntaxError, "anchor '#{raw.anchor}' value contains itself" if @expanding[aid]
|
|
125
125
|
|
|
126
|
-
target = @aliases.fetch(
|
|
126
|
+
target = @aliases.fetch(raw)
|
|
127
127
|
@expanding[aid] = true
|
|
128
128
|
@alias_depth += 1
|
|
129
129
|
begin
|
|
@@ -154,9 +154,9 @@ module Libo11y
|
|
|
154
154
|
end
|
|
155
155
|
end
|
|
156
156
|
|
|
157
|
-
def duplicates?(
|
|
157
|
+
def duplicates?(mapping)
|
|
158
158
|
seen = {}
|
|
159
|
-
|
|
159
|
+
mapping.pairs.each do |pair|
|
|
160
160
|
ident = node_identity(pair[0])
|
|
161
161
|
if seen[ident]
|
|
162
162
|
@duplicate = true
|
|
@@ -243,7 +243,7 @@ module Libo11y
|
|
|
243
243
|
end
|
|
244
244
|
|
|
245
245
|
is_map = lambda do |n|
|
|
246
|
-
target = n.is_a?(YamlV3::AliasNode) ? @aliases[n
|
|
246
|
+
target = n.is_a?(YamlV3::AliasNode) ? @aliases[n] : n
|
|
247
247
|
target.is_a?(YamlV3::MappingNode)
|
|
248
248
|
end
|
|
249
249
|
|
|
@@ -399,7 +399,8 @@ module Libo11y
|
|
|
399
399
|
end
|
|
400
400
|
|
|
401
401
|
seen = {}
|
|
402
|
-
a.values
|
|
402
|
+
declared_values = a.values # an Attribute's member, not a Hash's values
|
|
403
|
+
declared_values.each do |v|
|
|
403
404
|
trimmed = go_trim_space(v)
|
|
404
405
|
fail_rule.call("libo11y_enum_value_whitespace", name, "value #{GoQuote.quote(v)}") if v != trimmed
|
|
405
406
|
fail_rule.call("libo11y_enum_duplicate_value", name, "value #{GoQuote.quote(v)}") if seen[trimmed]
|