instatus-otel 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +257 -0
- data/lib/instatus/otel/attributes.rb +155 -0
- data/lib/instatus/otel/client.rb +108 -0
- data/lib/instatus/otel/configuration.rb +32 -0
- data/lib/instatus/otel/dedupe.rb +41 -0
- data/lib/instatus/otel/event.rb +51 -0
- data/lib/instatus/otel/exports.rb +59 -0
- data/lib/instatus/otel/logger.rb +77 -0
- data/lib/instatus/otel/providers.rb +135 -0
- data/lib/instatus/otel/rack.rb +43 -0
- data/lib/instatus/otel/rails.rb +15 -0
- data/lib/instatus/otel/rails_error_subscriber.rb +21 -0
- data/lib/instatus/otel/rate_cap.rb +30 -0
- data/lib/instatus/otel/resource.rb +50 -0
- data/lib/instatus/otel/scope.rb +92 -0
- data/lib/instatus/otel/scrub.rb +85 -0
- data/lib/instatus/otel/version.rb +8 -0
- data/lib/instatus/otel.rb +184 -0
- metadata +177 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
# endpoint nil leaves the OTLP exporter to read OTEL_EXPORTER_OTLP_* itself.
|
|
6
|
+
SignalExport = Struct.new(:endpoint, :headers, keyword_init: true) do
|
|
7
|
+
def exporter_options
|
|
8
|
+
options = {}
|
|
9
|
+
options[:endpoint] = endpoint if endpoint
|
|
10
|
+
options[:headers] = headers unless headers.empty?
|
|
11
|
+
options
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module Exports
|
|
16
|
+
DEFAULT_ENDPOINT = "https://otel.instatus.com"
|
|
17
|
+
SIGNALS = %i[logs traces metrics].freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# Without an API key or endpoint only OTEL_EXPORTER_OTLP_* exports: servers have no DSN mode.
|
|
22
|
+
def resolve(api_key:, site_id: nil, endpoint: nil, env: ENV)
|
|
23
|
+
if present?(api_key)
|
|
24
|
+
headers = { "Authorization" => "Bearer #{api_key}" }
|
|
25
|
+
headers["X-Instatus-Site-Id"] = site_id.to_s if present?(site_id)
|
|
26
|
+
return every_signal { |signal| SignalExport.new(endpoint: url_for(endpoint, env, signal), headers: headers) }
|
|
27
|
+
end
|
|
28
|
+
if present?(endpoint)
|
|
29
|
+
return every_signal { |signal| SignalExport.new(endpoint: signal_url(endpoint, signal), headers: {}) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
SIGNALS.select { |signal| env_endpoint?(env, signal) }.to_h { |signal| [signal, SignalExport.new(headers: {})] }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def every_signal(&build)
|
|
36
|
+
SIGNALS.to_h { |signal| [signal, build.call(signal)] }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def url_for(endpoint, env, signal)
|
|
40
|
+
return signal_url(endpoint, signal) if present?(endpoint)
|
|
41
|
+
return nil if env_endpoint?(env, signal)
|
|
42
|
+
|
|
43
|
+
signal_url(DEFAULT_ENDPOINT, signal)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def signal_url(endpoint, signal)
|
|
47
|
+
"#{endpoint.to_s.sub(%r{/+\z}, "")}/v1/#{signal}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def env_endpoint?(env, signal)
|
|
51
|
+
present?(env["OTEL_EXPORTER_OTLP_#{signal.to_s.upcase}_ENDPOINT"]) || present?(env["OTEL_EXPORTER_OTLP_ENDPOINT"])
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def present?(value)
|
|
55
|
+
!value.nil? && !value.to_s.empty?
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "logger"
|
|
4
|
+
|
|
5
|
+
module Instatus
|
|
6
|
+
module Otel
|
|
7
|
+
class Logger < ::Logger
|
|
8
|
+
SEVERITY = {
|
|
9
|
+
::Logger::DEBUG => [5, "DEBUG"],
|
|
10
|
+
::Logger::INFO => [9, "INFO"],
|
|
11
|
+
::Logger::WARN => [13, "WARN"],
|
|
12
|
+
::Logger::ERROR => [17, "ERROR"],
|
|
13
|
+
::Logger::FATAL => [21, "FATAL"],
|
|
14
|
+
::Logger::UNKNOWN => [0, "UNKNOWN"]
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
def initialize(logdev = nil, *args, logger_provider: nil, **options)
|
|
18
|
+
super(logdev, *args, **options)
|
|
19
|
+
@logger_provider = logger_provider
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def add(severity, message = nil, progname = nil)
|
|
23
|
+
severity ||= ::Logger::UNKNOWN
|
|
24
|
+
return true if severity < level
|
|
25
|
+
|
|
26
|
+
if message.nil?
|
|
27
|
+
if block_given?
|
|
28
|
+
message = yield
|
|
29
|
+
else
|
|
30
|
+
message = progname
|
|
31
|
+
progname = self.progname
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
emit(severity, message, progname)
|
|
35
|
+
super(severity, message, progname)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def emit(severity, message, progname)
|
|
41
|
+
number, text = SEVERITY.fetch(severity, SEVERITY[::Logger::UNKNOWN])
|
|
42
|
+
attributes = message.is_a?(Exception) ? exception_attributes(message) : {}
|
|
43
|
+
attributes["log.logger"] = progname.to_s if progname
|
|
44
|
+
otel_logger.on_emit(
|
|
45
|
+
timestamp: Time.now,
|
|
46
|
+
observed_timestamp: Time.now,
|
|
47
|
+
severity_number: number,
|
|
48
|
+
severity_text: text,
|
|
49
|
+
body: body_of(message),
|
|
50
|
+
attributes: attributes,
|
|
51
|
+
context: OpenTelemetry::Context.current
|
|
52
|
+
)
|
|
53
|
+
rescue StandardError
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def otel_logger
|
|
58
|
+
(@logger_provider || OpenTelemetry.logger_provider).logger(name: SDK_NAME, version: VERSION)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def body_of(message)
|
|
62
|
+
text =
|
|
63
|
+
case message
|
|
64
|
+
when String then message
|
|
65
|
+
when Exception then Attributes.headline(message.class.name.to_s, message.message.to_s)
|
|
66
|
+
else message.inspect
|
|
67
|
+
end
|
|
68
|
+
Scrub.scrub_text(text)[0, Attributes::MAX_VALUE_LENGTH]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def exception_attributes(error)
|
|
72
|
+
value = Event.exception_value(error, handled: nil, mechanism: nil)
|
|
73
|
+
Attributes.exception_attributes(Event.build(level: :error, exception: value))
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "opentelemetry/sdk"
|
|
4
|
+
require "opentelemetry-logs-sdk"
|
|
5
|
+
require "opentelemetry-metrics-sdk"
|
|
6
|
+
require "opentelemetry/exporter/otlp"
|
|
7
|
+
require "opentelemetry-exporter-otlp-logs"
|
|
8
|
+
require "opentelemetry-exporter-otlp-metrics"
|
|
9
|
+
|
|
10
|
+
module Instatus
|
|
11
|
+
module Otel
|
|
12
|
+
class Providers
|
|
13
|
+
SUCCESS = OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
14
|
+
|
|
15
|
+
attr_reader :tracer_provider, :logger_provider, :meter_provider
|
|
16
|
+
|
|
17
|
+
def initialize(config, env: ENV)
|
|
18
|
+
@config = config
|
|
19
|
+
exports = config.enabled? ? Exports.resolve(api_key: config.api_key, site_id: config.site_id, endpoint: config.endpoint, env: env) : {}
|
|
20
|
+
warn("[instatus] init needs an api_key (or INSTATUS_API_KEY) or an OTLP endpoint") if config.enabled? && !exports[:logs]
|
|
21
|
+
|
|
22
|
+
resource = build_resource
|
|
23
|
+
@tracer_provider = build_tracer_provider(resource, exports[:traces])
|
|
24
|
+
@logger_provider = build_logger_provider(resource, exports[:logs])
|
|
25
|
+
@meter_provider = build_meter_provider(resource, exports[:metrics]) if exports[:metrics]
|
|
26
|
+
@logger = @logger_provider.logger(name: SDK_NAME, version: VERSION)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def install
|
|
30
|
+
OpenTelemetry.propagation = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
|
|
31
|
+
OpenTelemetry.tracer_provider = tracer_provider
|
|
32
|
+
OpenTelemetry.logger_provider = logger_provider
|
|
33
|
+
OpenTelemetry.meter_provider = meter_provider if meter_provider
|
|
34
|
+
install_instrumentations if @config.enabled?
|
|
35
|
+
self
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def emit(record, span_context)
|
|
39
|
+
@logger.on_emit(
|
|
40
|
+
timestamp: record.timestamp,
|
|
41
|
+
observed_timestamp: Time.now,
|
|
42
|
+
severity_number: record.severity_number,
|
|
43
|
+
severity_text: record.severity_text,
|
|
44
|
+
body: record.body,
|
|
45
|
+
attributes: record.attributes,
|
|
46
|
+
context: context_for(span_context)
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def flush(timeout)
|
|
51
|
+
within(timeout) { |provider| provider.force_flush(timeout: timeout) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def close(timeout)
|
|
55
|
+
within(timeout) { |provider| provider.shutdown(timeout: timeout) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# Older metrics SDKs wait a whole export interval on shutdown, whatever the timeout.
|
|
61
|
+
def within(timeout)
|
|
62
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
63
|
+
threads = each_provider.map do |provider|
|
|
64
|
+
Thread.new { yield(provider) }.tap { |thread| thread.report_on_exception = false }
|
|
65
|
+
end
|
|
66
|
+
threads.all? do |thread|
|
|
67
|
+
remaining = [deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max
|
|
68
|
+
thread.join(remaining) && thread.value == SUCCESS
|
|
69
|
+
rescue StandardError
|
|
70
|
+
false
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def each_provider
|
|
75
|
+
[logger_provider, tracer_provider, meter_provider].compact
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def context_for(span_context)
|
|
79
|
+
return OpenTelemetry::Context.empty unless span_context&.valid?
|
|
80
|
+
|
|
81
|
+
OpenTelemetry::Trace.context_with_span(OpenTelemetry::Trace.non_recording_span(span_context), parent_context: OpenTelemetry::Context.empty)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def build_resource
|
|
85
|
+
attributes = Resource.attributes(service_name: @config.service_name, release: @config.release, environment: @config.environment)
|
|
86
|
+
OpenTelemetry::SDK::Resources::Resource.default
|
|
87
|
+
.merge(OpenTelemetry::SDK::Resources::Resource.create(Resource.host_attributes))
|
|
88
|
+
.merge(OpenTelemetry::SDK::Resources::Resource.create(attributes))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def build_tracer_provider(resource, target)
|
|
92
|
+
sampler = OpenTelemetry::SDK::Trace::Samplers.parent_based(
|
|
93
|
+
root: OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(@config.traces_sample_rate.to_f)
|
|
94
|
+
)
|
|
95
|
+
provider = OpenTelemetry::SDK::Trace::TracerProvider.new(resource: resource, sampler: sampler)
|
|
96
|
+
if target
|
|
97
|
+
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(**target.exporter_options)
|
|
98
|
+
provider.add_span_processor(OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter))
|
|
99
|
+
end
|
|
100
|
+
provider
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def build_logger_provider(resource, target)
|
|
104
|
+
provider = OpenTelemetry::SDK::Logs::LoggerProvider.new(resource: resource)
|
|
105
|
+
if target
|
|
106
|
+
exporter = OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new(**target.exporter_options)
|
|
107
|
+
provider.add_log_record_processor(OpenTelemetry::SDK::Logs::Export::BatchLogRecordProcessor.new(exporter))
|
|
108
|
+
end
|
|
109
|
+
provider
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def build_meter_provider(resource, target)
|
|
113
|
+
provider = OpenTelemetry::SDK::Metrics::MeterProvider.new(resource: resource)
|
|
114
|
+
exporter = OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(**target.exporter_options)
|
|
115
|
+
interval = (@config.metrics_export_interval.to_f * 1000).to_i
|
|
116
|
+
provider.add_metric_reader(
|
|
117
|
+
OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader.new(exporter: exporter, export_interval_millis: interval)
|
|
118
|
+
)
|
|
119
|
+
provider
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def install_instrumentations
|
|
123
|
+
instrumentations = @config.instrumentations
|
|
124
|
+
return unless instrumentations
|
|
125
|
+
|
|
126
|
+
registry = OpenTelemetry::Instrumentation.registry
|
|
127
|
+
case instrumentations
|
|
128
|
+
when :all, true then registry.install_all
|
|
129
|
+
when Hash then registry.install(instrumentations.keys, instrumentations)
|
|
130
|
+
else registry.install(Array(instrumentations))
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
# Its own scope per request, so set_user never reaches the next request on the thread.
|
|
6
|
+
class RackMiddleware
|
|
7
|
+
def initialize(app)
|
|
8
|
+
@app = app
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call(env)
|
|
12
|
+
Instatus.with_scope do |scope|
|
|
13
|
+
scope.set_request(url: request_url(env), method: env["REQUEST_METHOD"])
|
|
14
|
+
response = call_app(env)
|
|
15
|
+
error = env["rack.exception"] || env["sinatra.error"]
|
|
16
|
+
report(error) if error
|
|
17
|
+
response
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def call_app(env)
|
|
24
|
+
@app.call(env)
|
|
25
|
+
rescue Exception => e
|
|
26
|
+
report(e) unless e.is_a?(SystemExit) || e.is_a?(SignalException)
|
|
27
|
+
raise
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def report(error)
|
|
31
|
+
Instatus.capture_with_mechanism(error, handled: false, mechanism: "rack")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def request_url(env)
|
|
35
|
+
scheme = env["rack.url_scheme"] || "http"
|
|
36
|
+
host = env["HTTP_HOST"] || [env["SERVER_NAME"], env["SERVER_PORT"]].compact.join(":")
|
|
37
|
+
path = "#{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}"
|
|
38
|
+
query = env["QUERY_STRING"].to_s
|
|
39
|
+
query.empty? ? "#{scheme}://#{host}#{path}" : "#{scheme}://#{host}#{path}?#{query}"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
class Railtie < ::Rails::Railtie
|
|
6
|
+
initializer "instatus.rack_middleware" do |app|
|
|
7
|
+
app.config.middleware.insert(0, RackMiddleware)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
config.after_initialize do
|
|
11
|
+
::Rails.error.subscribe(RailsErrorSubscriber.new) if ::Rails.respond_to?(:error)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
class RailsErrorSubscriber
|
|
6
|
+
SKIP_SOURCES = /_cache_store\.active_support\z/
|
|
7
|
+
CONTEXT_VALUES = [String, Symbol, Numeric, TrueClass, FalseClass, Hash, Array].freeze
|
|
8
|
+
|
|
9
|
+
def report(error, handled:, severity:, context:, source: nil)
|
|
10
|
+
return if source && SKIP_SOURCES.match?(source)
|
|
11
|
+
|
|
12
|
+
tags = source ? { "rails.source" => source } : {}
|
|
13
|
+
rails_context = context.select { |_key, value| CONTEXT_VALUES.any? { |type| value.is_a?(type) } }
|
|
14
|
+
contexts = rails_context.empty? ? {} : { "rails" => rails_context }
|
|
15
|
+
Instatus.capture_with_mechanism(
|
|
16
|
+
error, handled: handled, mechanism: "rails", level: severity, tags: tags, contexts: contexts
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
class RateCap
|
|
6
|
+
def initialize(limit, window_seconds, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
7
|
+
@limit = limit
|
|
8
|
+
@window = window_seconds
|
|
9
|
+
@clock = clock
|
|
10
|
+
@window_start = clock.call
|
|
11
|
+
@count = 0
|
|
12
|
+
@mutex = Mutex.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def take
|
|
16
|
+
@mutex.synchronize do
|
|
17
|
+
now = @clock.call
|
|
18
|
+
if now - @window_start >= @window
|
|
19
|
+
@window_start = now
|
|
20
|
+
@count = 0
|
|
21
|
+
end
|
|
22
|
+
next false if @count >= @limit
|
|
23
|
+
|
|
24
|
+
@count += 1
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "etc"
|
|
4
|
+
require "socket"
|
|
5
|
+
|
|
6
|
+
module Instatus
|
|
7
|
+
module Otel
|
|
8
|
+
module Resource
|
|
9
|
+
LANGUAGE = "ruby"
|
|
10
|
+
FALLBACK_SERVICE_NAME = "ruby"
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def attributes(service_name: nil, release: nil, environment: nil)
|
|
15
|
+
attributes = {
|
|
16
|
+
"service.name" => present(service_name) || FALLBACK_SERVICE_NAME,
|
|
17
|
+
"telemetry.sdk.name" => SDK_NAME,
|
|
18
|
+
"telemetry.sdk.version" => VERSION,
|
|
19
|
+
"telemetry.sdk.language" => LANGUAGE
|
|
20
|
+
}
|
|
21
|
+
attributes["service.version"] = release.to_s if present(release)
|
|
22
|
+
attributes["deployment.environment.name"] = environment.to_s if present(environment)
|
|
23
|
+
attributes
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def host_attributes
|
|
27
|
+
attributes = { "host.arch" => RbConfig::CONFIG["host_cpu"], "os.type" => os_type }
|
|
28
|
+
hostname = Socket.gethostname
|
|
29
|
+
attributes["host.name"] = hostname if hostname && !hostname.empty?
|
|
30
|
+
attributes.compact
|
|
31
|
+
rescue SystemCallError, SocketError
|
|
32
|
+
attributes.compact
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def os_type
|
|
36
|
+
case RbConfig::CONFIG["host_os"]
|
|
37
|
+
when /darwin/ then "darwin"
|
|
38
|
+
when /linux/ then "linux"
|
|
39
|
+
when /mswin|mingw|cygwin/ then "windows"
|
|
40
|
+
when /freebsd/ then "freebsd"
|
|
41
|
+
else RbConfig::CONFIG["host_os"]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def present(value)
|
|
46
|
+
value.nil? || value.to_s.empty? ? nil : value.to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
class Scope
|
|
5
|
+
attr_reader :user, :level, :tags, :extra, :contexts, :request
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
clear
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize_copy(source)
|
|
12
|
+
super
|
|
13
|
+
@tags = source.tags.dup
|
|
14
|
+
@extra = source.extra.dup
|
|
15
|
+
@contexts = source.contexts.dup
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def set_user(user)
|
|
19
|
+
@user = user&.to_h
|
|
20
|
+
self
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def set_level(level)
|
|
24
|
+
@level = level&.to_sym
|
|
25
|
+
self
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def set_tag(key, value)
|
|
29
|
+
@tags[key.to_s] = value
|
|
30
|
+
self
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def set_tags(tags)
|
|
34
|
+
tags.each { |key, value| set_tag(key, value) }
|
|
35
|
+
self
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def set_extra(key, value)
|
|
39
|
+
@extra[key.to_s] = value
|
|
40
|
+
self
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def set_extras(extras)
|
|
44
|
+
extras.each { |key, value| set_extra(key, value) }
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def set_context(name, context)
|
|
49
|
+
if context.nil?
|
|
50
|
+
@contexts.delete(name.to_s)
|
|
51
|
+
else
|
|
52
|
+
@contexts[name.to_s] = context
|
|
53
|
+
end
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def set_request(url:, method: nil)
|
|
58
|
+
@request = { url: url, method: method }.compact
|
|
59
|
+
self
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def update(user: nil, tags: nil, extra: nil, contexts: nil, level: nil)
|
|
63
|
+
set_user(user) if user
|
|
64
|
+
set_tags(tags) if tags
|
|
65
|
+
set_extras(extra) if extra
|
|
66
|
+
set_level(level) if level
|
|
67
|
+
contexts&.each { |name, context| set_context(name, context) }
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def clear
|
|
72
|
+
@user = nil
|
|
73
|
+
@level = nil
|
|
74
|
+
@request = nil
|
|
75
|
+
@tags = {}
|
|
76
|
+
@extra = {}
|
|
77
|
+
@contexts = {}
|
|
78
|
+
self
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Event values win over the scope's, as in Sentry: capture_exception(error, tags: {...}) beats set_tag.
|
|
82
|
+
def apply(event)
|
|
83
|
+
event.level = level || event.level
|
|
84
|
+
event.user ||= user
|
|
85
|
+
event.request ||= request
|
|
86
|
+
event.tags = tags.merge(event.tags)
|
|
87
|
+
event.extra = extra.merge(event.extra)
|
|
88
|
+
event.contexts = contexts.merge(event.contexts)
|
|
89
|
+
event
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Instatus
|
|
7
|
+
module Otel
|
|
8
|
+
module Scrub
|
|
9
|
+
FILTERED = "[Filtered]"
|
|
10
|
+
MAX_DEPTH = 6
|
|
11
|
+
|
|
12
|
+
SENSITIVE_KEY = /pass(word|wd)?|secret|token|api[-_.]?key|auth|credential|session|cookie|csrf|xsrf|private[-_.]?key|signature/i
|
|
13
|
+
CARD_NUMBER = /\b\d(?:[ -]?\d){12,18}\b/
|
|
14
|
+
URL_CREDENTIALS = %r{\A([a-z][a-z0-9+.-]*://)[^/@?#]*@}i
|
|
15
|
+
URL_PARAM = /([?&#])([^=&#]+)=([^&#]*)/
|
|
16
|
+
URL = %r{\A[a-z][a-z0-9+.-]*://}i
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def sensitive_key?(key)
|
|
21
|
+
SENSITIVE_KEY.match?(key.to_s)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The Luhn check keeps timestamps and order numbers of card length.
|
|
25
|
+
def luhn?(digits)
|
|
26
|
+
sum = digits.reverse.each_char.each_with_index.sum do |char, index|
|
|
27
|
+
digit = index.odd? ? char.to_i * 2 : char.to_i
|
|
28
|
+
digit > 9 ? digit - 9 : digit
|
|
29
|
+
end
|
|
30
|
+
(sum % 10).zero?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def scrub_text(text)
|
|
34
|
+
text.to_s.gsub(CARD_NUMBER) { |match| luhn?(match.delete("^0-9")) ? FILTERED : match }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A card number can sit in any parameter, so URLs get the text filter too.
|
|
38
|
+
def scrub_url(url)
|
|
39
|
+
without_credentials = url.to_s.sub(URL_CREDENTIALS, '\1')
|
|
40
|
+
filtered = without_credentials.gsub(URL_PARAM) do |match|
|
|
41
|
+
separator = Regexp.last_match(1)
|
|
42
|
+
key = Regexp.last_match(2)
|
|
43
|
+
sensitive_key?(decode(key)) ? "#{separator}#{key}=#{FILTERED}" : match
|
|
44
|
+
end
|
|
45
|
+
scrub_text(filtered)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def scrub_string(text)
|
|
49
|
+
URL.match?(text) ? scrub_url(text) : scrub_text(text)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def scrub_value(key, value)
|
|
53
|
+
scrub_nested(key.to_s, value, 0, Set.new.compare_by_identity)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def decode(value)
|
|
57
|
+
URI.decode_www_form_component(value)
|
|
58
|
+
rescue ArgumentError
|
|
59
|
+
value
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# ancestors is the current path only, so a value used twice is kept and only a cycle is cut.
|
|
63
|
+
def scrub_nested(key, value, depth, ancestors)
|
|
64
|
+
return FILTERED if sensitive_key?(key)
|
|
65
|
+
return scrub_string(value) if value.is_a?(String)
|
|
66
|
+
return value unless value.is_a?(Hash) || value.is_a?(Array)
|
|
67
|
+
return "[Circular]" if ancestors.include?(value)
|
|
68
|
+
return "[Truncated]" if depth >= MAX_DEPTH
|
|
69
|
+
|
|
70
|
+
ancestors.add(value)
|
|
71
|
+
scrubbed = scrub_children(value) { |child_key, child| scrub_nested(child_key, child, depth + 1, ancestors) }
|
|
72
|
+
ancestors.delete(value)
|
|
73
|
+
scrubbed
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def scrub_children(value)
|
|
77
|
+
if value.is_a?(Array)
|
|
78
|
+
value.each_with_index.map { |child, index| yield(index.to_s, child) }
|
|
79
|
+
else
|
|
80
|
+
value.to_h { |key, child| [key, yield(key.to_s, child)] }
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|