graftcode-instrumentation 1.0.5
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/README.md +76 -0
- data/lib/graftcode/instrumentation/body_capture/command_writer.rb +62 -0
- data/lib/graftcode/instrumentation/body_capture/constants.rb +14 -0
- data/lib/graftcode/instrumentation/body_capture/string_writer.rb +39 -0
- data/lib/graftcode/instrumentation/body_capture/value_writer.rb +62 -0
- data/lib/graftcode/instrumentation/body_capture/write_context.rb +37 -0
- data/lib/graftcode/instrumentation/body_capture.rb +28 -0
- data/lib/graftcode/instrumentation/call_span/attributes.rb +35 -0
- data/lib/graftcode/instrumentation/call_span/constants.rb +30 -0
- data/lib/graftcode/instrumentation/call_span/factory.rb +65 -0
- data/lib/graftcode/instrumentation/call_span/trace_propagation.rb +69 -0
- data/lib/graftcode/instrumentation/call_span.rb +97 -0
- data/lib/graftcode/instrumentation/graftcode_instrumentation/log_recording.rb +81 -0
- data/lib/graftcode/instrumentation/graftcode_instrumentation.rb +103 -0
- data/lib/graftcode/instrumentation/hypertube_sdk.rb +28 -0
- data/lib/graftcode/instrumentation/library_catalog/command_tree.rb +26 -0
- data/lib/graftcode/instrumentation/library_catalog/gemspec_version.rb +40 -0
- data/lib/graftcode/instrumentation/library_catalog/path_helpers.rb +36 -0
- data/lib/graftcode/instrumentation/library_catalog/resolution.rb +67 -0
- data/lib/graftcode/instrumentation/library_catalog.rb +87 -0
- data/lib/graftcode/instrumentation/operation_name.rb +116 -0
- data/lib/graftcode/instrumentation/telemetry_settings.rb +62 -0
- data/lib/graftcode/instrumentation/version.rb +7 -0
- data/lib/graftcode/instrumentation.rb +21 -0
- metadata +191 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c56e2ee991ed37bd9b26afb6529688fe5b4283ba3ddb0974d7e219d4f99e9953
|
|
4
|
+
data.tar.gz: 4b886edc9a530f61782ddc791d8905c272c65a89578063e102f861b28c9ff57c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2a3d16878f5d8f3bb60a9e7502bf71a13fb1e6d88e1796a6b9acfd3fa88c5cb715dcd5697b118044877ff7ea7e620b8a0c00ea42c1ccef23ad7962a5819dff84
|
|
7
|
+
data.tar.gz: 4d6ba58dc31edb8c51cc7acd3665522698eb87923611b35ec1c93c505044959e6353a4b4a8fcf896c15edddcdbb5b1d28c2bcb67ca4fce07116e232901c89bd0
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# graftcode-instrumentation (Ruby)
|
|
2
|
+
|
|
3
|
+
OpenTelemetry instrumentation for Graftcode (Hypertube) calls: client and server spans, W3C trace context propagation, and Hypertube log forwarding.
|
|
4
|
+
|
|
5
|
+
Hypertube exposes a **broker** (`Hypertube::Utils::Abstractions::HypertubeBroker.attach` / `detach`); nothing is monkey-patched. Attach registers broker and log listeners; `close` removes them.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- [`hypertube-ruby-sdk`](https://rubygems.org/gems/hypertube-ruby-sdk) from rubygems.org, in a release with the broker abstractions (3.1.19 and earlier do not have them yet)
|
|
10
|
+
- `opentelemetry-api` at runtime; configure a tracer provider with `opentelemetry-sdk` (or another exporter pipeline)
|
|
11
|
+
|
|
12
|
+
In a library hosted by Graftcode Gateway, the Hypertube SDK is loaded from the gateway's `Binaries/Ruby` folder rather than as a gem. The instrumentation then reuses that loaded copy instead of requiring `hypertube-ruby-sdk`, so the broker and logger it attaches to are the ones handling calls.
|
|
13
|
+
|
|
14
|
+
## Development
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bundle install
|
|
18
|
+
bundle exec rspec
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Dependencies are declared in `graftcode-instrumentation.gemspec`. `bundle install` resolves them from rubygems.org.
|
|
22
|
+
|
|
23
|
+
To test against an unreleased `hypertube-ruby-sdk`, publish it to a local gem repository, install that build, then re-lock:
|
|
24
|
+
|
|
25
|
+
```powershell
|
|
26
|
+
C:\dev\graft-packages\build-ruby.ps1
|
|
27
|
+
C:\dev\graft-packages\serve-ruby.ps1 # keep running; Bundler uses the compact index only over HTTP
|
|
28
|
+
gem install hypertube-ruby-sdk -v 3.1.18 --source http://127.0.0.1:8808
|
|
29
|
+
bundle lock --update hypertube-ruby-sdk
|
|
30
|
+
bundle install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The local build is stamped 3.1.18, the version generated grafts pin, which rubygems.org also has. Bundler reuses an installed gem of the same version from any source, so run `gem uninstall hypertube-ruby-sdk -v 3.1.18` before going back to rubygems.org.
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
require 'opentelemetry/sdk'
|
|
39
|
+
require 'graftcode/instrumentation'
|
|
40
|
+
|
|
41
|
+
OpenTelemetry::SDK.configure do |c|
|
|
42
|
+
c.add_span_processor(
|
|
43
|
+
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(your_exporter)
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
listener = Graftcode::Instrumentation.attach(
|
|
48
|
+
OpenTelemetry.tracer_provider,
|
|
49
|
+
minimum_log_level: Hypertube::Utils::Logging::LogsLevel::INFO,
|
|
50
|
+
logger_provider: logger_provider # optional
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# ... Hypertube calls ...
|
|
54
|
+
|
|
55
|
+
listener.close
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
With a `logger_provider` (for example from `opentelemetry-logs-sdk`) and a `minimum_log_level`, Hypertube logs are exported as OpenTelemetry log records under the `Hypertube` scope, correlated with the current span. Without one, they are recorded as events on the current span.
|
|
59
|
+
|
|
60
|
+
## Configuration (environment)
|
|
61
|
+
|
|
62
|
+
| Variable | Default |
|
|
63
|
+
|----------|---------|
|
|
64
|
+
| `HYPERTUBE_TELEMETRY_AUTO_INJECT` | `true` |
|
|
65
|
+
| `HYPERTUBE_TELEMETRY_CAPTURE_BODIES` | `false` |
|
|
66
|
+
| `HYPERTUBE_TELEMETRY_BODY_MAX_CHARS` | `8192` (clamp 256–32768) |
|
|
67
|
+
|
|
68
|
+
## Span model
|
|
69
|
+
|
|
70
|
+
- **Caller:** `Graft <library> [version] <channel>` (or `<Runtime> <channel>`), attributes `graft.dependency.type` = `Graft`, `graft.dependency.target`, plus Hypertube metadata.
|
|
71
|
+
- **Callee:** `Service.method` (or global `method`), kind internal.
|
|
72
|
+
- A call that raises marks the span failed with `exception.type` and a redacted `exception.message`. A remote `Exception` response completes the span with status OK, as in .NET and Java.
|
|
73
|
+
|
|
74
|
+
## Trace context
|
|
75
|
+
|
|
76
|
+
Outgoing calls inject W3C `traceparent` / `tracestate` and `traceid` via thread-local connection headers when auto-inject is enabled. Only the W3C trace context is used, whatever `OpenTelemetry.propagation` is configured to, so no `baggage` header is sent. Incoming handling prefers W3C headers (matched case-insensitively); `traceid` alone joins an existing trace with a synthetic remote parent.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../hypertube_sdk'
|
|
4
|
+
|
|
5
|
+
module Graftcode
|
|
6
|
+
module Instrumentation
|
|
7
|
+
module BodyCapture
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def write_command(writer, command, depth, budget, mark_truncated)
|
|
11
|
+
append_command_header(writer, command)
|
|
12
|
+
|
|
13
|
+
local_budget = local_budget_for(depth, budget)
|
|
14
|
+
ctx = WriteContext.new(
|
|
15
|
+
writer: writer,
|
|
16
|
+
depth: depth,
|
|
17
|
+
budget: budget,
|
|
18
|
+
local_budget: local_budget,
|
|
19
|
+
mark_truncated: mark_truncated
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
complete, has_content = write_payload(ctx, command)
|
|
23
|
+
finalize_command(writer, has_content, complete)
|
|
24
|
+
complete
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def append_command_header(writer, command)
|
|
28
|
+
type_name = Hypertube::Utils::CommandType.get_name(command.command_type)
|
|
29
|
+
runtime_name = Hypertube::Utils::RuntimeNameHandler.get_name(command.runtime_name)
|
|
30
|
+
writer << %({"runtime":"#{runtime_name}","type":"#{type_name}","payload":[)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def write_payload(ctx, command)
|
|
34
|
+
has_content = false
|
|
35
|
+
|
|
36
|
+
command.payload.each_with_index do |item, index|
|
|
37
|
+
if ctx.over_budget?
|
|
38
|
+
ctx.mark_truncated.call
|
|
39
|
+
return [false, has_content]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
ctx.writer << ',' if has_content
|
|
43
|
+
has_content = true
|
|
44
|
+
|
|
45
|
+
item_ctx = ctx.child(depth: ctx.depth + 1, local_budget: ctx.local_budget)
|
|
46
|
+
sensitive = payload_item_sensitive?(command, index)
|
|
47
|
+
return [false, has_content] unless write_value(item_ctx, item, force_redact: sensitive)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
[true, has_content]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def finalize_command(writer, has_content, complete)
|
|
54
|
+
unless complete
|
|
55
|
+
writer << ',' if has_content
|
|
56
|
+
writer << TRUNCATION_MARKER
|
|
57
|
+
end
|
|
58
|
+
writer << ']}'
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Graftcode
|
|
4
|
+
module Instrumentation
|
|
5
|
+
module BodyCapture
|
|
6
|
+
TRUNCATION_MARKER = '"__truncated__"'
|
|
7
|
+
MAX_DEPTH = 24
|
|
8
|
+
CLOSING_COST_PER_LEVEL = 18
|
|
9
|
+
SCALAR_RESERVE = 32
|
|
10
|
+
|
|
11
|
+
Result = Struct.new(:body, :truncated, keyword_init: true)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Graftcode
|
|
4
|
+
module Instrumentation
|
|
5
|
+
module BodyCapture
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def write_string(writer, value, budget, mark_truncated)
|
|
9
|
+
writer << '"'
|
|
10
|
+
return writer << '"' if value.nil?
|
|
11
|
+
|
|
12
|
+
value.each_char do |character|
|
|
13
|
+
if writer.length >= budget
|
|
14
|
+
mark_truncated.call
|
|
15
|
+
writer << '"'
|
|
16
|
+
return false
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
writer << escape_character(character)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
writer << '"'
|
|
23
|
+
true
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def escape_character(character)
|
|
27
|
+
case character
|
|
28
|
+
when '"' then '\"'
|
|
29
|
+
when '\\' then '\\\\'
|
|
30
|
+
when "\n" then '\n'
|
|
31
|
+
when "\r" then '\r'
|
|
32
|
+
when "\t" then '\t'
|
|
33
|
+
else
|
|
34
|
+
character.ord < 32 ? format('\u%04x', character.ord) : character
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../hypertube_sdk'
|
|
4
|
+
|
|
5
|
+
module Graftcode
|
|
6
|
+
module Instrumentation
|
|
7
|
+
module BodyCapture
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def write_value(ctx, value, force_redact: false)
|
|
11
|
+
return write_null(ctx) if value.nil?
|
|
12
|
+
return write_at_depth_limit(ctx) if ctx.depth >= MAX_DEPTH
|
|
13
|
+
if value.is_a?(Hypertube::Utils::Command)
|
|
14
|
+
return write_command(ctx.writer, value, ctx.depth, ctx.budget, ctx.mark_truncated)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
return write_binary_bytes(ctx, value) if binary_string?(value)
|
|
18
|
+
return write_redacted_string(ctx, value, force_redact) if value.is_a?(String)
|
|
19
|
+
|
|
20
|
+
write_scalar(ctx, value, force_redact)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def write_null(ctx)
|
|
24
|
+
ctx.writer << 'null'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def write_at_depth_limit(ctx)
|
|
28
|
+
ctx.mark_truncated.call
|
|
29
|
+
ctx.writer << TRUNCATION_MARKER
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def binary_string?(value)
|
|
33
|
+
value.is_a?(String) && value.encoding == Encoding::BINARY
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def write_binary_bytes(ctx, value)
|
|
37
|
+
ctx.writer << %("[bytes:#{value.bytesize}]")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def write_redacted_string(ctx, value, force_redact)
|
|
41
|
+
redacted = force_redact ? Hypertube::Utils::Logging::LogsRedactor::PLACEHOLDER : Hypertube::Utils::Logging::LogsRedactor.redact_value(value)
|
|
42
|
+
write_string(ctx.writer, redacted, ctx.local_budget, ctx.mark_truncated)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def write_scalar(ctx, value, force_redact)
|
|
46
|
+
case value
|
|
47
|
+
when TrueClass, FalseClass, Numeric
|
|
48
|
+
ctx.writer << value.to_s
|
|
49
|
+
else
|
|
50
|
+
formatted = force_redact ? Hypertube::Utils::Logging::LogsRedactor::PLACEHOLDER : Hypertube::Utils::Logging::LogsRedactor.redact_value(format_value(value))
|
|
51
|
+
write_string(ctx.writer, formatted, ctx.local_budget, ctx.mark_truncated)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def format_value(value)
|
|
56
|
+
value.to_s
|
|
57
|
+
rescue StandardError
|
|
58
|
+
"[unrenderable:#{value.class.name}]"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../hypertube_sdk'
|
|
4
|
+
|
|
5
|
+
module Graftcode
|
|
6
|
+
module Instrumentation
|
|
7
|
+
module BodyCapture
|
|
8
|
+
WriteContext = Struct.new(:writer, :depth, :budget, :local_budget, :mark_truncated, keyword_init: true) do
|
|
9
|
+
def child(depth:, local_budget:)
|
|
10
|
+
WriteContext.new(
|
|
11
|
+
writer: writer,
|
|
12
|
+
depth: depth,
|
|
13
|
+
budget: budget,
|
|
14
|
+
local_budget: local_budget,
|
|
15
|
+
mark_truncated: mark_truncated
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def over_budget?
|
|
20
|
+
writer.length >= local_budget
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
def local_budget_for(depth, budget)
|
|
27
|
+
[0, budget - ((depth + 1) * CLOSING_COST_PER_LEVEL) - SCALAR_RESERVE].max
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def payload_item_sensitive?(command, index)
|
|
31
|
+
index.positive? &&
|
|
32
|
+
command.payload[index - 1].is_a?(String) &&
|
|
33
|
+
Hypertube::Utils::Logging::LogsRedactor.sensitive_key?(command.payload[index - 1])
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'hypertube_sdk'
|
|
4
|
+
require_relative 'body_capture/constants'
|
|
5
|
+
require_relative 'body_capture/write_context'
|
|
6
|
+
require_relative 'body_capture/string_writer'
|
|
7
|
+
require_relative 'body_capture/value_writer'
|
|
8
|
+
require_relative 'body_capture/command_writer'
|
|
9
|
+
|
|
10
|
+
module Graftcode
|
|
11
|
+
module Instrumentation
|
|
12
|
+
module BodyCapture
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def render(command, max_characters)
|
|
16
|
+
return nil unless command.is_a?(Hypertube::Utils::Command)
|
|
17
|
+
|
|
18
|
+
truncated = false
|
|
19
|
+
writer = +''
|
|
20
|
+
write_command(writer, command, 0, max_characters, -> { truncated = true })
|
|
21
|
+
Result.new(
|
|
22
|
+
body: Hypertube::Utils::Logging::LogsRedactor.redact_body(writer),
|
|
23
|
+
truncated: truncated
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../hypertube_sdk'
|
|
4
|
+
require_relative '../library_catalog'
|
|
5
|
+
|
|
6
|
+
module Graftcode
|
|
7
|
+
module Instrumentation
|
|
8
|
+
class CallSpan
|
|
9
|
+
def self.set_if_present(span, key, value)
|
|
10
|
+
span.set_attribute(key, value) unless value.nil?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.apply_invoke_attributes(span, command, connection_data, target, operation)
|
|
14
|
+
apply_invoke_metadata(span, command, connection_data)
|
|
15
|
+
apply_invoke_redacted(span, connection_data, target, operation)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.apply_invoke_metadata(span, command, connection_data)
|
|
19
|
+
span.set_attribute('hypertube.runtime', LibraryCatalog.runtime_label(command.runtime_name))
|
|
20
|
+
span.set_attribute('hypertube.command_type', Hypertube::Utils::CommandType.get_name(command.command_type))
|
|
21
|
+
span.set_attribute('hypertube.connection_type',
|
|
22
|
+
LibraryCatalog.connection_type_name(connection_data.connection_type))
|
|
23
|
+
span.set_attribute(DEPENDENCY_TYPE, BRIDGE_DEPENDENCY_TYPE)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.apply_invoke_redacted(span, connection_data, target, operation)
|
|
27
|
+
redactor = Hypertube::Utils::Logging::LogsRedactor
|
|
28
|
+
set_if_present(span, DEPENDENCY_TARGET, redactor.redact_value(target))
|
|
29
|
+
set_if_present(span, 'hypertube.operation', redactor.redact_value(operation))
|
|
30
|
+
set_if_present(span, 'hypertube.target',
|
|
31
|
+
redactor.redact_value(redactor.redact_target(connection_data.hostname)))
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry-api'
|
|
4
|
+
require_relative '../hypertube_sdk'
|
|
5
|
+
|
|
6
|
+
module Graftcode
|
|
7
|
+
module Instrumentation
|
|
8
|
+
class CallSpan
|
|
9
|
+
DEPENDENCY_TYPE = 'graft.dependency.type'
|
|
10
|
+
DEPENDENCY_TARGET = 'graft.dependency.target'
|
|
11
|
+
BRIDGE_DEPENDENCY_TYPE = 'Graft'
|
|
12
|
+
REQUEST_BODY = 'hypertube.request.body'
|
|
13
|
+
RESPONSE_BODY = 'hypertube.response.body'
|
|
14
|
+
REQUEST_BODY_TRUNCATED = 'hypertube.request.body.truncated'
|
|
15
|
+
RESPONSE_BODY_TRUNCATED = 'hypertube.response.body.truncated'
|
|
16
|
+
TRACE_PARENT_HEADER = 'traceparent'
|
|
17
|
+
TRACE_STATE_HEADER = 'tracestate'
|
|
18
|
+
TRACE_ID_HEADER = 'traceid'
|
|
19
|
+
|
|
20
|
+
SKIP_COMMAND_TYPES = [
|
|
21
|
+
Hypertube::Utils::CommandType::PASS_BY_VALUE,
|
|
22
|
+
Hypertube::Utils::CommandType::REGISTER_FOR_UPDATE,
|
|
23
|
+
Hypertube::Utils::CommandType::VALUE_FOR_UPDATE,
|
|
24
|
+
Hypertube::Utils::CommandType::INVOKE_DELEGATE
|
|
25
|
+
].freeze
|
|
26
|
+
|
|
27
|
+
TRACE_CONTEXT = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../hypertube_sdk'
|
|
4
|
+
require_relative '../library_catalog'
|
|
5
|
+
require_relative '../operation_name'
|
|
6
|
+
require_relative '../telemetry_settings'
|
|
7
|
+
|
|
8
|
+
module Graftcode
|
|
9
|
+
module Instrumentation
|
|
10
|
+
class CallSpan
|
|
11
|
+
def self.start_invoke(tracer, command, connection_data)
|
|
12
|
+
operation = resolve_operation(command)
|
|
13
|
+
return nil unless operation
|
|
14
|
+
|
|
15
|
+
LibraryCatalog.record(command, connection_data)
|
|
16
|
+
target = LibraryCatalog.describe_target(command, connection_data)
|
|
17
|
+
span = start_span(tracer, "Graft #{target}", connection_data.get_headers)
|
|
18
|
+
apply_invoke_attributes(span, command, connection_data, target, operation) if span.recording?
|
|
19
|
+
|
|
20
|
+
call_span = attach(span)
|
|
21
|
+
call_span.capture(command, REQUEST_BODY, REQUEST_BODY_TRUNCATED)
|
|
22
|
+
call_span.inject_into(connection_data) if TelemetrySettings.auto_inject_enabled?
|
|
23
|
+
call_span
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.start_handle(tracer, command, headers)
|
|
27
|
+
operation = resolve_operation(command)
|
|
28
|
+
return nil unless operation
|
|
29
|
+
|
|
30
|
+
call_span = attach(start_span(tracer, operation, headers || {}))
|
|
31
|
+
call_span.capture(command, REQUEST_BODY, REQUEST_BODY_TRUNCATED)
|
|
32
|
+
call_span
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.resolve_operation(command)
|
|
36
|
+
return nil unless command.is_a?(Hypertube::Utils::Command)
|
|
37
|
+
return nil if SKIP_COMMAND_TYPES.include?(command.command_type)
|
|
38
|
+
|
|
39
|
+
operation = OperationName.resolve(command)
|
|
40
|
+
return nil if operation.downcase.end_with?('telemetrysetup.initialize')
|
|
41
|
+
|
|
42
|
+
operation
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.attach(span)
|
|
46
|
+
token = OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))
|
|
47
|
+
new(span, token)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.start_span(tracer, name, headers)
|
|
51
|
+
parent = extract_parent_context(headers)
|
|
52
|
+
tracer.start_span(name, with_parent: parent, kind: :internal)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.complete_spans(context, response, exception)
|
|
56
|
+
context.items.reverse_each do |item|
|
|
57
|
+
next unless item.is_a?(CallSpan)
|
|
58
|
+
|
|
59
|
+
item.complete(response, exception)
|
|
60
|
+
context.items.delete(item)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry-api'
|
|
4
|
+
|
|
5
|
+
module Graftcode
|
|
6
|
+
module Instrumentation
|
|
7
|
+
class CallSpan
|
|
8
|
+
module HeaderGetter
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def get(carrier, key)
|
|
12
|
+
CallSpan.header(carrier, key)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def keys(carrier)
|
|
16
|
+
carrier.keys
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.header(headers, key)
|
|
21
|
+
value = headers[key]
|
|
22
|
+
return value unless value.nil?
|
|
23
|
+
|
|
24
|
+
headers.each do |entry_key, entry_value|
|
|
25
|
+
return entry_value if entry_key.to_s.casecmp?(key)
|
|
26
|
+
end
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.read_trace_id(raw)
|
|
31
|
+
return nil if raw.nil?
|
|
32
|
+
|
|
33
|
+
hex = raw.strip.tr('-', '').downcase
|
|
34
|
+
return nil unless hex.match?(/\A[0-9a-f]{32}\z/) && hex !~ /\A0+\z/
|
|
35
|
+
|
|
36
|
+
hex
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.extract_parent_context(headers)
|
|
40
|
+
carrier = headers || {}
|
|
41
|
+
extracted = TRACE_CONTEXT.extract(carrier, context: OpenTelemetry::Context.empty, getter: HeaderGetter)
|
|
42
|
+
return extracted if trace_context_valid?(extracted)
|
|
43
|
+
|
|
44
|
+
parent_context_from_trace_id_header(carrier)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.trace_context_valid?(context)
|
|
48
|
+
OpenTelemetry::Trace.current_span(context).context.valid?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.parent_context_from_trace_id_header(carrier)
|
|
52
|
+
trace_id = read_trace_id(header(carrier, TRACE_ID_HEADER))
|
|
53
|
+
current_trace_id = OpenTelemetry::Trace.current_span.context.hex_trace_id
|
|
54
|
+
return OpenTelemetry::Context.current if trace_id.nil? || trace_id == current_trace_id
|
|
55
|
+
|
|
56
|
+
remote_context = OpenTelemetry::Trace::SpanContext.new(
|
|
57
|
+
trace_id: [trace_id].pack('H*'),
|
|
58
|
+
span_id: OpenTelemetry::Trace.generate_span_id,
|
|
59
|
+
trace_flags: OpenTelemetry::Trace::TraceFlags::SAMPLED,
|
|
60
|
+
remote: true
|
|
61
|
+
)
|
|
62
|
+
OpenTelemetry::Trace.context_with_span(
|
|
63
|
+
OpenTelemetry::Trace.non_recording_span(remote_context),
|
|
64
|
+
parent_context: OpenTelemetry::Context.empty
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'opentelemetry-api'
|
|
4
|
+
require_relative 'hypertube_sdk'
|
|
5
|
+
require_relative 'body_capture'
|
|
6
|
+
require_relative 'telemetry_settings'
|
|
7
|
+
require_relative 'call_span/constants'
|
|
8
|
+
require_relative 'call_span/trace_propagation'
|
|
9
|
+
require_relative 'call_span/attributes'
|
|
10
|
+
require_relative 'call_span/factory'
|
|
11
|
+
|
|
12
|
+
module Graftcode
|
|
13
|
+
module Instrumentation
|
|
14
|
+
class CallSpan
|
|
15
|
+
attr_reader :span
|
|
16
|
+
|
|
17
|
+
def initialize(span, context_token)
|
|
18
|
+
@span = span
|
|
19
|
+
@context_token = context_token
|
|
20
|
+
@injected_into = nil
|
|
21
|
+
@previous_headers = nil
|
|
22
|
+
@completed = false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def complete(response, exception)
|
|
26
|
+
return if @completed
|
|
27
|
+
|
|
28
|
+
@completed = true
|
|
29
|
+
begin
|
|
30
|
+
capture(response, RESPONSE_BODY, RESPONSE_BODY_TRUNCATED) unless response.nil?
|
|
31
|
+
apply_completion_status(exception)
|
|
32
|
+
restore_injected_headers
|
|
33
|
+
ensure
|
|
34
|
+
detach_and_finish
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def close
|
|
39
|
+
complete(nil, nil)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def capture(command, body_key, truncated_key)
|
|
43
|
+
return unless @span.recording? && TelemetrySettings.body_capture_enabled?
|
|
44
|
+
|
|
45
|
+
rendered = BodyCapture.render(command, TelemetrySettings.body_max_characters)
|
|
46
|
+
return if rendered.nil? || rendered.body.nil?
|
|
47
|
+
|
|
48
|
+
@span.set_attribute(body_key, rendered.body)
|
|
49
|
+
@span.set_attribute(truncated_key, true) if rendered.truncated
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def inject_into(connection_data)
|
|
53
|
+
local = connection_data.get_thread_local_headers
|
|
54
|
+
span_context = @span.context
|
|
55
|
+
return unless span_context.valid?
|
|
56
|
+
|
|
57
|
+
merged = connection_data.get_headers
|
|
58
|
+
return if merged.key?(TRACE_PARENT_HEADER) && !local.key?(TRACE_PARENT_HEADER)
|
|
59
|
+
|
|
60
|
+
headers = local.dup
|
|
61
|
+
headers.delete(TRACE_STATE_HEADER)
|
|
62
|
+
TRACE_CONTEXT.inject(
|
|
63
|
+
headers,
|
|
64
|
+
context: OpenTelemetry::Trace.context_with_span(@span, parent_context: OpenTelemetry::Context.empty)
|
|
65
|
+
)
|
|
66
|
+
headers[TRACE_ID_HEADER] = span_context.hex_trace_id
|
|
67
|
+
|
|
68
|
+
connection_data.set_thread_local_headers(headers)
|
|
69
|
+
@injected_into = connection_data
|
|
70
|
+
@previous_headers = local
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def apply_completion_status(exception)
|
|
76
|
+
if exception.nil?
|
|
77
|
+
@span.status = OpenTelemetry::Trace::Status.ok
|
|
78
|
+
return
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
message = Hypertube::Utils::Logging::LogsRedactor.redact_value(exception.message)
|
|
82
|
+
@span.set_attribute('exception.type', exception.class.name)
|
|
83
|
+
self.class.set_if_present(@span, 'exception.message', message)
|
|
84
|
+
@span.status = OpenTelemetry::Trace::Status.error(message.to_s)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def restore_injected_headers
|
|
88
|
+
@injected_into&.set_thread_local_headers(@previous_headers || {})
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def detach_and_finish
|
|
92
|
+
OpenTelemetry::Context.detach(@context_token) if @context_token
|
|
93
|
+
@span.finish
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|