grpc-dc 0.38.0 → 0.40.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/lib/grpcdc/channel.rb +14 -2
- data/lib/grpcdc/codec.rb +34 -15
- data/lib/grpcdc/endpoint.rb +2 -0
- data/lib/grpcdc/server.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d9149e99ff7a9a4af75efae4b681523e7a83461758c3c3f432480b25ab3f2501
|
|
4
|
+
data.tar.gz: c3a8655e4a507c56d7e1e64cf143981c1e5c441aecc8699cfb0a56f1cca32837
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 20638a380654830de9f4ef8f8ffe187f0940f7eb3b7c7797d7fd2fca46901ed6b2ef2c73fad39ab9b235afcc30f04a57b6a46786d366f405b5889105c4470dfb
|
|
7
|
+
data.tar.gz: 6674132740272eac4eb1783fb15abfc382d962b42f0b5db93a00df7cec717af3bd33828c561d3df12ad48f30f2158ef413f9c13c907fad6fdf75e51e259c1041
|
data/lib/grpcdc/channel.rb
CHANGED
|
@@ -13,9 +13,20 @@ module Grpcdc
|
|
|
13
13
|
|
|
14
14
|
def initialize(transport = nil, endpoint: nil)
|
|
15
15
|
@endpoint = endpoint || Endpoint.new(transport, creator: true)
|
|
16
|
+
@trace_context = nil
|
|
16
17
|
@endpoint.start
|
|
17
18
|
end
|
|
18
19
|
|
|
20
|
+
# Parent this channel's outbound spans to ctx (usually
|
|
21
|
+
# ctx.trace_context of the call being served, or a root from
|
|
22
|
+
# Grpcdc.new_trace_context). Explicit metadata still wins.
|
|
23
|
+
def with_trace_context(ctx)
|
|
24
|
+
ch = Channel.allocate
|
|
25
|
+
ch.instance_variable_set(:@endpoint, @endpoint)
|
|
26
|
+
ch.instance_variable_set(:@trace_context, ctx)
|
|
27
|
+
ch
|
|
28
|
+
end
|
|
29
|
+
|
|
19
30
|
def unary(path, req, resp, metadata = nil, timeout: nil)
|
|
20
31
|
stream = open_call(path, metadata, timeout)
|
|
21
32
|
begin
|
|
@@ -98,9 +109,10 @@ module Grpcdc
|
|
|
98
109
|
metadata["grpc-timeout"] = Codec.encode_timeout(timeout)
|
|
99
110
|
end
|
|
100
111
|
unless Grpcdc.tracer.nil?
|
|
101
|
-
|
|
112
|
+
parent = Grpcdc.parse_traceparent(metadata["traceparent"]) || Grpcdc.parent_of(@trace_context)
|
|
113
|
+
trace_ctx = Grpcdc.trace_start("client", path, parent)
|
|
102
114
|
unless trace_ctx.nil?
|
|
103
|
-
metadata["traceparent"] = Grpcdc.format_traceparent(trace_ctx["trace_id"], trace_ctx["span_id"])
|
|
115
|
+
metadata["traceparent"] = Grpcdc.format_traceparent(trace_ctx["trace_id"], trace_ctx["span_id"], trace_ctx["sampled"])
|
|
104
116
|
end
|
|
105
117
|
end
|
|
106
118
|
end
|
data/lib/grpcdc/codec.rb
CHANGED
|
@@ -43,10 +43,10 @@ module Grpcdc
|
|
|
43
43
|
# The call completed OK (client-side stream end).
|
|
44
44
|
class CallClosed < EOFError; end
|
|
45
45
|
|
|
46
|
-
# --- Homegrown tracing (W3C traceparent propagation) ----------
|
|
46
|
+
# --- Homegrown tracing (beta) (W3C traceparent propagation) ----------
|
|
47
47
|
# Dormant until Grpcdc.set_tracer registers an observer with
|
|
48
48
|
# on_start(kind, method, trace_id, span_id, parent_span_id) and
|
|
49
|
-
# on_end(kind, method, trace_id, span_id, status_name).
|
|
49
|
+
# on_end(kind, method, trace_id, span_id, status_name, duration_ms).
|
|
50
50
|
# Generated code stays dependency-free: sampling and export are
|
|
51
51
|
# the application's problem.
|
|
52
52
|
@tracer = nil
|
|
@@ -75,47 +75,66 @@ module Grpcdc
|
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
def self.format_traceparent(trace_id, span_id)
|
|
79
|
-
"00-#{trace_id}-#{span_id}
|
|
78
|
+
def self.format_traceparent(trace_id, span_id, sampled = true)
|
|
79
|
+
"00-#{trace_id}-#{span_id}-#{sampled ? "01" : "00"}"
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
-
# Parse a traceparent header ->
|
|
82
|
+
# Parse a traceparent header ->
|
|
83
|
+
# [trace_id, span_id, sampled] or nil.
|
|
83
84
|
def self.parse_traceparent(value)
|
|
84
85
|
return nil unless value.is_a?(String)
|
|
85
86
|
|
|
86
87
|
parts = value.split("-")
|
|
87
88
|
return nil unless parts.length == 4
|
|
88
89
|
|
|
89
|
-
trace_id, span_id = parts[1], parts[2]
|
|
90
|
+
trace_id, span_id, flags = parts[1], parts[2], parts[3]
|
|
90
91
|
return nil unless trace_id.length == 32 && span_id.length == 16
|
|
91
92
|
return nil unless trace_id.match?(/\A[0-9a-f]+\z/) && span_id.match?(/\A[0-9a-f]+\z/)
|
|
93
|
+
return nil unless flags.length == 2 && flags.match?(/\A[0-9a-f]+\z/)
|
|
92
94
|
|
|
93
|
-
[trace_id, span_id]
|
|
95
|
+
[trace_id, span_id, (flags.to_i(16) & 1) == 1]
|
|
94
96
|
end
|
|
95
97
|
|
|
96
98
|
def self.trace_start(kind, method, parent)
|
|
97
99
|
return nil if @tracer.nil? || !trace_allowed?(method)
|
|
98
100
|
|
|
99
101
|
require "securerandom"
|
|
100
|
-
trace_id, parent_span_id = parent || [SecureRandom.hex(16), ""]
|
|
102
|
+
trace_id, parent_span_id, parent_sampled = parent || [SecureRandom.hex(16), "", true]
|
|
101
103
|
ctx = {
|
|
102
104
|
"trace_id" => trace_id,
|
|
103
105
|
"span_id" => SecureRandom.hex(8),
|
|
104
|
-
"parent_span_id" => parent_span_id
|
|
106
|
+
"parent_span_id" => parent_span_id,
|
|
107
|
+
"sampled" => parent_sampled,
|
|
108
|
+
"start_time" => Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
105
109
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
if ctx["sampled"]
|
|
111
|
+
begin
|
|
112
|
+
@tracer.on_start(kind, method, ctx["trace_id"], ctx["span_id"], parent_span_id)
|
|
113
|
+
rescue StandardError
|
|
114
|
+
# observers must never break the call
|
|
115
|
+
end
|
|
110
116
|
end
|
|
111
117
|
ctx
|
|
112
118
|
end
|
|
113
119
|
|
|
120
|
+
# Start a root span context (client workflows). Pair with trace_end.
|
|
121
|
+
def self.new_trace_context(kind, method)
|
|
122
|
+
trace_start(kind, method, nil)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# View a span context as a traceparent parent for an outbound call.
|
|
126
|
+
def self.parent_of(ctx)
|
|
127
|
+
return nil if ctx.nil?
|
|
128
|
+
|
|
129
|
+
[ctx["trace_id"], ctx["span_id"], ctx["sampled"]]
|
|
130
|
+
end
|
|
131
|
+
|
|
114
132
|
def self.trace_end(kind, method, ctx, status)
|
|
115
|
-
return if ctx.nil? || @tracer.nil?
|
|
133
|
+
return if ctx.nil? || @tracer.nil? || ctx["sampled"] == false
|
|
116
134
|
|
|
135
|
+
duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - ctx["start_time"]) * 1000.0
|
|
117
136
|
begin
|
|
118
|
-
@tracer.on_end(kind, method, ctx["trace_id"], ctx["span_id"], status)
|
|
137
|
+
@tracer.on_end(kind, method, ctx["trace_id"], ctx["span_id"], status, duration_ms)
|
|
119
138
|
rescue StandardError
|
|
120
139
|
# observers must never break the call
|
|
121
140
|
end
|
data/lib/grpcdc/endpoint.rb
CHANGED
|
@@ -143,6 +143,8 @@ module Grpcdc
|
|
|
143
143
|
# Per-call context: metadata sent in HEADERS, plus deadline.
|
|
144
144
|
class CallContext
|
|
145
145
|
attr_reader :metadata, :deadline, :response_headers, :response_trailers
|
|
146
|
+
# This call's server span context (nil when dormant).
|
|
147
|
+
attr_accessor :trace_context
|
|
146
148
|
|
|
147
149
|
# Record response initial metadata (sent lazily, before
|
|
148
150
|
# the first response message).
|
data/lib/grpcdc/server.rb
CHANGED
|
@@ -93,6 +93,7 @@ module Grpcdc
|
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
trace_ctx = Grpcdc.trace_start("server", headers.path, Grpcdc.parse_traceparent((headers.metadata || {})["traceparent"]))
|
|
96
|
+
ctx.trace_context = trace_ctx
|
|
96
97
|
result = nil
|
|
97
98
|
begin
|
|
98
99
|
method.handler.call(ctx, RequestStream.new(stream_input), sink)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: grpc-dc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.40.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Davis
|
|
@@ -39,8 +39,8 @@ licenses:
|
|
|
39
39
|
- Apache-2.0
|
|
40
40
|
metadata:
|
|
41
41
|
homepage_uri: https://github.com/browser-grpc/grpc-over-datachannels
|
|
42
|
-
source_code_uri: https://github.com/browser-grpc/grpc-over-datachannels/tree/
|
|
43
|
-
changelog_uri: https://github.com/browser-grpc/grpc-over-datachannels/blob/
|
|
42
|
+
source_code_uri: https://github.com/browser-grpc/grpc-over-datachannels/tree/main/packages/grpc-ruby
|
|
43
|
+
changelog_uri: https://github.com/browser-grpc/grpc-over-datachannels/blob/main/CHANGELOG.md
|
|
44
44
|
rdoc_options: []
|
|
45
45
|
require_paths:
|
|
46
46
|
- lib
|