strongmind-platform-sdk 3.34.2 → 3.35.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 +2 -0
- data/README.md +51 -0
- data/lib/platform_sdk/identity/error_handleable.rb +0 -1
- data/lib/platform_sdk/logging/pii_formatter.rb +1 -3
- data/lib/platform_sdk/observability/otel/datadog_span_exporter.rb +103 -0
- data/lib/platform_sdk/observability/otel/record_exceptions.rb +23 -0
- data/lib/platform_sdk/observability/otel.rb +82 -0
- data/lib/platform_sdk/observability.rb +1 -0
- data/lib/platform_sdk/pii/text_scrubber.rb +91 -0
- data/lib/platform_sdk/pii.rb +5 -0
- data/lib/platform_sdk/version.rb +2 -2
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d9b7f80ea3ffc26b41ddffd0f7fd4bc2359125cd2f4b44efbd4a1bbfa7578cbe
|
|
4
|
+
data.tar.gz: 0d7c7a328f483e53d013d9981e7cddcd526b88a1d73ef092303fb802c05aff2f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 61fc39cc8fb40ba0b3284839becef09404c39a695b32d07ef1db977d5d8efa163af92b1f049571dc06b1c4e563188842cf09638068ef5c332321f2c0090a65e7
|
|
7
|
+
data.tar.gz: b49f02671946da633258616e0b7ea06301e0f874f3c51c9b0fabdc2b5f78dce30217a573092adfa4065793f2d5424e55b1aa09d3a52316038c7da01661e83eaf
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
- Add `PlatformSdk::Observability::Otel.setup(Rails.application.config, sidekiq: false)`, the OpenTelemetry setup that sends traces and errors to a Datadog agent over OTLP (ADR-0004), extracted from the copies in transcripts and repository-dashboard. Opt-in: nothing changes for an app until it calls `setup` from an initializer, which does nothing without `OTEL_EXPORTER_OTLP_ENDPOINT` or `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`, with `OTEL_SDK_DISABLED=true`, or when the SDK fails to configure; only the first call counts. It requires every bundled `opentelemetry-instrumentation-*` gem that ships `lib/<gem name>.rb`, obfuscates SQL in PG and Redis spans, leaves email subjects and mailer arguments off ActionMailer spans, drops URL query strings and user agents, and puts `error.type`/`error.message`/`error.stack` on the request span or a root job span, the same spans the app copies report on. `error.message`, `error.stack` and the status description on any span are scrubbed by the new `PlatformSdk::Pii::TextScrubber` (invalid UTF-8 is replaced, not raised on): a Postgres `DETAIL`, `LINE`, `CONTEXT` or `QUERY` line becomes `[FILTERED]`, an `ERROR` line is masked from its first quoted value that isn't a named constraint, relation, column, index, table, type, function or schema, and every line after a masked one is dropped up to the next backtrace frame; URL query strings (any scheme case), IPv4 and IPv6 addresses, emails, SSNs and phone numbers become `[FILTERED]`. The email, SSN and phone patterns are shared with `Logging::PiiFormatter` through `Pii.mask_patterns`, whose log output is unchanged. `sidekiq: true` traces Sidekiq jobs as root spans linked to the enqueuing request and turns ActiveJob instrumentation off. See the README for what scrubbing can't catch and for apps that shouldn't call `setup` (Langfuse users, `datadog` gem users). No new runtime dependencies.
|
|
4
|
+
- Stop logging Identity error response bodies. IS4 answers a create that collides on the username unique index with SQL Server's raw message, which includes the duplicate value (a student's name and SIS id), and the error log line carried that body verbatim. The log line now has the exception class, method, path and status only. The raised error still carries the response, with its request scrubbed, for a consumer that decides it is safe to inspect.
|
|
3
5
|
- Raise typed errors from every Canvas error branch. The read/list/PUT-write methods raised bare `RuntimeError` strings, which consumers could not distinguish from a bug — Central's grade-passback retry wrapper, for example, only retries `CanvasClientError` and so treated every throttled read as fatal. All error branches now raise through one handler: 404 → `MissingModuleItemError`, 401 → `UnauthorizedError`, rate-limited → `RateLimitedError` (new), anything else → `CanvasClientError`. Every class subclasses `CanvasClientError` (itself a `StandardError`), so rescues written against the old classes keep working; only code matching on `RuntimeError` specifically (none known) would notice. Message text changed (see below); no known consumer parses it.
|
|
4
6
|
- Stop echoing raw Canvas response bodies and page query strings into error messages. Consumers persist `error.message` (Central's `OutcomeLog.exception_message`) and forward it to Sentry, and a raw body from a submission, quiz or roster endpoint can carry student records (FERPA). Every error path now shares one message policy: `"<descriptor>: <Canvas's own error messages> (status: N, host: H)"`, with a fixed `unparseable Canvas error body` / `empty Canvas error body` / `Canvas rate limit exceeded` descriptor in place of any body that isn't a JSON error object. The write-path tail (`handle_post_response_errors`) previously appended the whole body and skipped the `error_messages_from` hardening; it now goes through the same builder. Pagination failures name the page path only, dropping the query that carries `as_user_id`/`student_id`. The raw body and status are still available as `CanvasClientError#response_body` and `#status` for a consumer that decides it is safe to inspect.
|
|
5
7
|
- `error_messages_from` also reads a hash-shaped `errors` (field => messages, as Canvas validation errors return) and a top-level `message` key, instead of producing an inspect blob or nothing.
|
data/README.md
CHANGED
|
@@ -89,6 +89,57 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
89
89
|
|
|
90
90
|
Everyone interacting in the Platform::Ruby::Sdk project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/platform-ruby-sdk/blob/main/CODE_OF_CONDUCT.md).
|
|
91
91
|
|
|
92
|
+
## OpenTelemetry to Datadog
|
|
93
|
+
|
|
94
|
+
`PlatformSdk::Observability::Otel.setup` sends a Rails app's traces and errors to a Datadog agent over OTLP (ADR-0004).
|
|
95
|
+
Pair it with `datadog=True` in `strongmind-deployment`, which runs the agent and sets `OTEL_EXPORTER_OTLP_ENDPOINT`.
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
# config/initializers/opentelemetry.rb
|
|
99
|
+
PlatformSdk::Observability::Otel.setup(Rails.application.config) # web only
|
|
100
|
+
# or
|
|
101
|
+
PlatformSdk::Observability::Otel.setup(Rails.application.config, sidekiq: true) # apps with Sidekiq
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Keep the `opentelemetry-instrumentation-*` gems you want in your Gemfile (`require: false` is fine); `setup` loads every
|
|
105
|
+
one you bundle. It does nothing and returns `false` when no OTLP endpoint is set, when `OTEL_SDK_DISABLED=true`, or when
|
|
106
|
+
the SDK can't configure (for example a blank `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`). Only the first call counts.
|
|
107
|
+
|
|
108
|
+
### Personal data
|
|
109
|
+
|
|
110
|
+
Before an error reaches Datadog, `error.message`, `error.stack` and the span status description are scrubbed by
|
|
111
|
+
`PlatformSdk::Pii::TextScrubber`, on every span that carries them:
|
|
112
|
+
|
|
113
|
+
- A Postgres `DETAIL`, `LINE`, `CONTEXT` or `QUERY` line keeps its label and becomes `[FILTERED]`. On an `ERROR` line,
|
|
114
|
+
everything from the first quoted value that isn't a named constraint, relation, column, index, table, type, function
|
|
115
|
+
or schema becomes `[FILTERED]`. Every line after a masked one is dropped up to the next backtrace frame.
|
|
116
|
+
- The query string of any `http(s)://` URL, in any case, becomes `?[FILTERED]` (Faraday puts the full request URL in
|
|
117
|
+
its errors). A quoted URL is masked to its closing quote (skipping escaped `\"`); an unquoted one to the end of the
|
|
118
|
+
line.
|
|
119
|
+
- IPv4 and IPv6 addresses, emails, SSNs and phone numbers become `[FILTERED]` (the same patterns
|
|
120
|
+
`PlatformSdk::Logging::PiiFormatter` masks in logs).
|
|
121
|
+
- URL query strings and user agents are dropped from span attributes; PG and Redis spans obfuscate SQL; mailer spans
|
|
122
|
+
leave out email subjects and the mailer method's arguments (recipient addresses are left out by default).
|
|
123
|
+
|
|
124
|
+
Scrubbing can't recognize a name an app writes into its own message, or Postgres text an app re-embeds on one line
|
|
125
|
+
(inside JSON or `inspect` output). Put IDs in error messages, never names or emails:
|
|
126
|
+
|
|
127
|
+
```ruby
|
|
128
|
+
raise TranscriptMissing, "no transcript for student #{student.id}" # yes
|
|
129
|
+
raise TranscriptMissing, "no transcript for #{student.full_name}" # no
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Known behavior
|
|
133
|
+
|
|
134
|
+
- A job that fails while running inline inside a request (`perform_now`) is reported on both the job span and the
|
|
135
|
+
request span. Both carry the same error type, message and stack, so Datadog should group them into one issue.
|
|
136
|
+
- Jobs traced with `propagation_style: :child` (not the default) don't report their own errors; leave it at `:link`.
|
|
137
|
+
- Only request spans and a job's own span carry `error.type`, `error.message` and `error.stack`. A rake task or
|
|
138
|
+
`rails runner` failure keeps its error status, but those details aren't attached, so it won't open an Error Tracking
|
|
139
|
+
issue the way Sentry did.
|
|
140
|
+
- Don't call `setup` in an app that uses `PlatformSdk::Observability::Langfuse` or traces with the `datadog` gem.
|
|
141
|
+
Langfuse shares OpenTelemetry's current span, and the `datadog` gem would trace every request a second time.
|
|
142
|
+
|
|
92
143
|
## Data Pipeline Concern
|
|
93
144
|
|
|
94
145
|
The `DataPipelineable` concern is a shared concern that can be included in models
|
|
@@ -30,7 +30,6 @@ module PlatformSdk
|
|
|
30
30
|
exception: exception_class.name.split('::').last,
|
|
31
31
|
method: error.response.dig(:request, :method),
|
|
32
32
|
path: error.response.dig(:request, :url_path),
|
|
33
|
-
response_body: error.response[:body],
|
|
34
33
|
status: error.response[:status]
|
|
35
34
|
}.compact
|
|
36
35
|
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry/sdk'
|
|
4
|
+
require 'platform_sdk/pii/text_scrubber'
|
|
5
|
+
|
|
6
|
+
module PlatformSdk
|
|
7
|
+
module Observability
|
|
8
|
+
module Otel
|
|
9
|
+
# Datadog opens an issue per exception event, so only the entry span carries error.*.
|
|
10
|
+
class DatadogSpanExporter
|
|
11
|
+
URL_KEYS = %w[url.full http.url http.target].freeze
|
|
12
|
+
DROPPED_KEYS = %w[url.query user_agent.original http.user_agent].freeze
|
|
13
|
+
ERROR_TEXT_KEYS = %w[error.message error.stack].freeze
|
|
14
|
+
|
|
15
|
+
def initialize(exporter)
|
|
16
|
+
@exporter = exporter
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def export(spans, timeout: nil)
|
|
20
|
+
@exporter.export(spans.filter_map { |span| safe_transform(span) }, timeout:)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def force_flush(timeout: nil)
|
|
24
|
+
@exporter.force_flush(timeout:)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def shutdown(timeout: nil)
|
|
28
|
+
@exporter.shutdown(timeout:)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def safe_transform(span)
|
|
34
|
+
transform(span)
|
|
35
|
+
rescue StandardError => e
|
|
36
|
+
OpenTelemetry.handle_error(exception: e, message: "#{self.class} dropped an untransformable span")
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def transform(span)
|
|
41
|
+
original = span.attributes || {}
|
|
42
|
+
attributes = scrub(original.dup)
|
|
43
|
+
exceptions, events = span.events.to_a.partition { |event| event.name == 'exception' }
|
|
44
|
+
add_error_attributes(attributes, exceptions.last) if reportable_error?(span)
|
|
45
|
+
copy_with(span, attributes, events, attribute_change: attributes.size - original.size,
|
|
46
|
+
removed_events: exceptions.size)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def copy_with(span, attributes, events, attribute_change:, removed_events:)
|
|
50
|
+
span.dup.tap do |copy|
|
|
51
|
+
copy.attributes = attributes.freeze
|
|
52
|
+
copy.status = scrub_status(span.status)
|
|
53
|
+
copy.events = events.freeze
|
|
54
|
+
copy.total_recorded_attributes = span.total_recorded_attributes + attribute_change
|
|
55
|
+
copy.total_recorded_events = span.total_recorded_events - removed_events
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def reportable_error?(span)
|
|
60
|
+
entry_span?(span) && error?(span)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def entry_span?(span)
|
|
64
|
+
return true if span.kind == :server
|
|
65
|
+
|
|
66
|
+
span.kind == :consumer && span.parent_span_id == OpenTelemetry::Trace::INVALID_SPAN_ID
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def error?(span)
|
|
70
|
+
span.status&.code == OpenTelemetry::Trace::Status::ERROR
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def scrub(attributes)
|
|
74
|
+
DROPPED_KEYS.each { |key| attributes.delete(key) }
|
|
75
|
+
URL_KEYS.each do |key|
|
|
76
|
+
attributes[key] = attributes[key].scrub.split('?', 2).first if attributes[key].is_a?(String)
|
|
77
|
+
end
|
|
78
|
+
ERROR_TEXT_KEYS.each do |key|
|
|
79
|
+
attributes[key] = Pii::TextScrubber.call(attributes[key]) if attributes.key?(key)
|
|
80
|
+
end
|
|
81
|
+
attributes
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def scrub_status(status)
|
|
85
|
+
return status if status.nil? || status.description.empty?
|
|
86
|
+
|
|
87
|
+
description = Pii::TextScrubber.call(status.description)
|
|
88
|
+
status.ok? ? OpenTelemetry::Trace::Status.ok(description) : OpenTelemetry::Trace::Status.error(description)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def add_error_attributes(attributes, exception)
|
|
92
|
+
return unless exception
|
|
93
|
+
|
|
94
|
+
details = exception.attributes || {}
|
|
95
|
+
attributes['error.type'] = details['exception.type']
|
|
96
|
+
attributes['error.message'] = Pii::TextScrubber.call(details['exception.message'])
|
|
97
|
+
attributes['error.stack'] = Pii::TextScrubber.call(details['exception.stacktrace'])
|
|
98
|
+
attributes.compact!
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry/sdk'
|
|
4
|
+
|
|
5
|
+
module PlatformSdk
|
|
6
|
+
module Observability
|
|
7
|
+
module Otel
|
|
8
|
+
# Records exceptions that escape the app on the request span.
|
|
9
|
+
class RecordExceptions
|
|
10
|
+
def initialize(app)
|
|
11
|
+
@app = app
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call(env)
|
|
15
|
+
@app.call(env)
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
OpenTelemetry::Trace.current_span.record_exception(e)
|
|
18
|
+
raise
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry/sdk'
|
|
4
|
+
require 'opentelemetry/exporter/otlp'
|
|
5
|
+
require 'platform_sdk/observability/otel/datadog_span_exporter'
|
|
6
|
+
require 'platform_sdk/observability/otel/record_exceptions'
|
|
7
|
+
|
|
8
|
+
module PlatformSdk
|
|
9
|
+
module Observability
|
|
10
|
+
# Opt-in OpenTelemetry setup that sends traces and errors to a Datadog agent.
|
|
11
|
+
module Otel
|
|
12
|
+
SHUTDOWN_TIMEOUT_SECONDS = 3
|
|
13
|
+
INSTRUMENTATION_GEM_PREFIX = 'opentelemetry-instrumentation-'
|
|
14
|
+
ENDPOINT_VARS = %w[OTEL_EXPORTER_OTLP_ENDPOINT OTEL_EXPORTER_OTLP_TRACES_ENDPOINT].freeze
|
|
15
|
+
|
|
16
|
+
# Raw SQL, email subjects and mailer arguments can contain student data.
|
|
17
|
+
INSTRUMENTATION_CONFIG = {
|
|
18
|
+
'OpenTelemetry::Instrumentation::PG' => { db_statement: :obfuscate },
|
|
19
|
+
'OpenTelemetry::Instrumentation::Redis' => { db_statement: :obfuscate },
|
|
20
|
+
'OpenTelemetry::Instrumentation::ActionMailer' => {
|
|
21
|
+
disallowed_notification_payload_keys: ['email.subject'], disallowed_process_payload_keys: [:args]
|
|
22
|
+
}
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
SIDEKIQ_CONFIG = {
|
|
26
|
+
'OpenTelemetry::Instrumentation::ActiveJob' => { enabled: false },
|
|
27
|
+
'OpenTelemetry::Instrumentation::Sidekiq' => { propagation_style: :link, span_naming: :job_class }
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
@configured = false
|
|
31
|
+
|
|
32
|
+
def self.setup(rails_config, sidekiq: false)
|
|
33
|
+
return false unless enabled?
|
|
34
|
+
return true if @configured
|
|
35
|
+
|
|
36
|
+
require_installed_instrumentations
|
|
37
|
+
previous = OpenTelemetry.tracer_provider
|
|
38
|
+
configure_sdk(sidekiq:)
|
|
39
|
+
provider = OpenTelemetry.tracer_provider
|
|
40
|
+
return false if provider.equal?(previous) || !provider.is_a?(OpenTelemetry::SDK::Trace::TracerProvider)
|
|
41
|
+
|
|
42
|
+
rails_config.middleware.insert_after ActionDispatch::DebugExceptions, RecordExceptions
|
|
43
|
+
at_exit { provider.shutdown(timeout: SHUTDOWN_TIMEOUT_SECONDS) }
|
|
44
|
+
@configured = true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.enabled?
|
|
48
|
+
return false if ENV['OTEL_SDK_DISABLED'] == 'true'
|
|
49
|
+
|
|
50
|
+
ENDPOINT_VARS.any? { |name| !ENV[name].to_s.strip.empty? }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.configure_sdk(sidekiq:)
|
|
54
|
+
OpenTelemetry::SDK.configure do |c|
|
|
55
|
+
c.add_span_processor(
|
|
56
|
+
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
|
|
57
|
+
DatadogSpanExporter.new(OpenTelemetry::Exporter::OTLP::Exporter.new)
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
c.use_all(instrumentation_config(sidekiq:))
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.instrumentation_config(sidekiq:)
|
|
65
|
+
sidekiq ? INSTRUMENTATION_CONFIG.merge(SIDEKIQ_CONFIG) : INSTRUMENTATION_CONFIG
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.require_installed_instrumentations
|
|
69
|
+
Gem.loaded_specs.each do |name, spec|
|
|
70
|
+
require name if name.start_with?(INSTRUMENTATION_GEM_PREFIX) && entry_file?(spec, name)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.entry_file?(spec, name)
|
|
75
|
+
spec.full_require_paths.any? { |dir| File.exist?(File.join(dir, "#{name}.rb")) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private_class_method :enabled?, :configure_sdk, :instrumentation_config, :require_installed_instrumentations,
|
|
79
|
+
:entry_file?
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'ipaddr'
|
|
4
|
+
require 'platform_sdk/pii'
|
|
5
|
+
|
|
6
|
+
module PlatformSdk
|
|
7
|
+
module Pii
|
|
8
|
+
# Masks Postgres echoes (row values, SQL, input), URL query strings, IPs, emails, SSNs and phone numbers.
|
|
9
|
+
module TextScrubber
|
|
10
|
+
ECHO_FIELD = /\A(DETAIL|LINE \d+|CONTEXT|QUERY):/
|
|
11
|
+
# Anchored to the first marker and possessive so a line is scanned once; quoted identifier names are kept.
|
|
12
|
+
IDENTIFIER_NAME = /\b(?:constraint|relation|column|index|table|type|function|schema) "[^"]*"/
|
|
13
|
+
VALUE_QUOTE = /\A(?>.*?ERROR: )(?:#{IDENTIFIER_NAME}|[^"\n])*+"/
|
|
14
|
+
# A frame is "\tfrom <path>:<line>:in `<method>'"; found in one forward pass, then the closing quote is checked.
|
|
15
|
+
FRAME_START = /\A\tfrom .+?:\d+:in [`']/
|
|
16
|
+
FRAME_END = /'\R?\z/
|
|
17
|
+
# Quoted URLs skip escaped quotes; an unquoted path stops at the next unquoted scheme so a line is scanned once.
|
|
18
|
+
URL_QUERY_RULES = [
|
|
19
|
+
[%r{"(https?://(?:\\.|[^"\\?\n])*\?)(?:\\.|[^"\\\n])*}i, "\"\\1#{FILTERED}"],
|
|
20
|
+
[%r{'(https?://(?:\\.|[^'\\?\n])*\?)(?:\\.|[^'\\\n])*}i, "'\\1#{FILTERED}"],
|
|
21
|
+
[%r{(?<!["'])(https?://(?:(?!(?<!["'])https?://)[^\s?])*\?).*}i, "\\1#{FILTERED}"]
|
|
22
|
+
].freeze
|
|
23
|
+
IPV4 = /(?<![\w.-])\d{1,3}(?:\.\d{1,3}){3}(?!\w|\.\d)/
|
|
24
|
+
IPV6 = /(?<![\w:.])(?:\h{0,4}:){2,7}\h{0,4}(?![\w:])/
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def call(text)
|
|
28
|
+
return text unless text.is_a?(String)
|
|
29
|
+
|
|
30
|
+
Pii.mask_patterns(mask_ip_addresses(mask_url_queries(mask_postgres_echoes(utf8(text)))))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def utf8(text)
|
|
36
|
+
return text.scrub if text.encoding == Encoding::UTF_8
|
|
37
|
+
return text.dup.force_encoding(Encoding::UTF_8).scrub if text.encoding == Encoding::BINARY
|
|
38
|
+
|
|
39
|
+
text.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def mask_postgres_echoes(text)
|
|
43
|
+
dropping = false
|
|
44
|
+
text.each_line.filter_map do |line|
|
|
45
|
+
dropping = false if dropping && backtrace_frame?(line)
|
|
46
|
+
next if dropping
|
|
47
|
+
|
|
48
|
+
masked = mask_line(line)
|
|
49
|
+
dropping = true if masked
|
|
50
|
+
masked || line
|
|
51
|
+
end.join
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def backtrace_frame?(line)
|
|
55
|
+
frame = line.match(FRAME_START)
|
|
56
|
+
frame && line.match?(FRAME_END) && line.chomp.length > frame.end(0)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def mask_line(line)
|
|
60
|
+
if (field = line[ECHO_FIELD, 1])
|
|
61
|
+
"#{field}: #{FILTERED}#{line[/\R\z/]}"
|
|
62
|
+
elsif (match = line.match(VALUE_QUOTE))
|
|
63
|
+
"#{line[0...match.end(0)]}#{FILTERED}\"#{line[/\R\z/]}"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def mask_url_queries(text)
|
|
68
|
+
URL_QUERY_RULES.reduce(text) { |result, (pattern, replacement)| result.gsub(pattern, replacement) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def mask_ip_addresses(text)
|
|
72
|
+
text.gsub(IPV4) { |match| ip?(match) ? FILTERED : match }
|
|
73
|
+
.gsub(IPV6) { |match| ipv6_address?(match) ? FILTERED : match }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def ipv6_address?(match)
|
|
77
|
+
groups = match.split(':').reject(&:empty?)
|
|
78
|
+
compressed = match.include?('::') && groups.any? { |group| group.match?(/\A\d+\z/) }
|
|
79
|
+
(groups.size >= 3 || compressed) && ip?(match)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def ip?(match)
|
|
83
|
+
IPAddr.new(match)
|
|
84
|
+
true
|
|
85
|
+
rescue IPAddr::Error
|
|
86
|
+
false
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
data/lib/platform_sdk/pii.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module PlatformSdk
|
|
4
|
+
# PII field names and text patterns, and the mask applied to them.
|
|
4
5
|
module Pii
|
|
5
6
|
DEFAULT_FIELDS = [
|
|
6
7
|
:email, /\Aname\z/i, :first_name, :last_name, :student_name, :username,
|
|
@@ -18,5 +19,9 @@ module PlatformSdk
|
|
|
18
19
|
PHONE_REGEX = /(?<!\d)(\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/
|
|
19
20
|
|
|
20
21
|
PII_PATTERNS = [EMAIL_REGEX, SSN_REGEX, PHONE_REGEX].freeze
|
|
22
|
+
|
|
23
|
+
def self.mask_patterns(text)
|
|
24
|
+
PII_PATTERNS.reduce(text) { |result, pattern| result.gsub(pattern, FILTERED) }
|
|
25
|
+
end
|
|
21
26
|
end
|
|
22
27
|
end
|
data/lib/platform_sdk/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: strongmind-platform-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.35.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Platform Team
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: base64
|
|
@@ -343,6 +343,9 @@ files:
|
|
|
343
343
|
- lib/platform_sdk/observability/langfuse/spec_support.rb
|
|
344
344
|
- lib/platform_sdk/observability/langfuse/trace_summarizable.rb
|
|
345
345
|
- lib/platform_sdk/observability/langfuse/traceable.rb
|
|
346
|
+
- lib/platform_sdk/observability/otel.rb
|
|
347
|
+
- lib/platform_sdk/observability/otel/datadog_span_exporter.rb
|
|
348
|
+
- lib/platform_sdk/observability/otel/record_exceptions.rb
|
|
346
349
|
- lib/platform_sdk/one_roster.rb
|
|
347
350
|
- lib/platform_sdk/one_roster/client.rb
|
|
348
351
|
- lib/platform_sdk/ops_genie.rb
|
|
@@ -353,6 +356,7 @@ files:
|
|
|
353
356
|
- lib/platform_sdk/pencil_spaces/models.rb
|
|
354
357
|
- lib/platform_sdk/pencil_spaces/models/user_with_role.rb
|
|
355
358
|
- lib/platform_sdk/pii.rb
|
|
359
|
+
- lib/platform_sdk/pii/text_scrubber.rb
|
|
356
360
|
- lib/platform_sdk/power_school.rb
|
|
357
361
|
- lib/platform_sdk/power_school/client.rb
|
|
358
362
|
- lib/platform_sdk/power_school/special_program.rb
|