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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13f3cf161698ea06b562cfb86b3747fa829c98fb79c62ab7dde67a51407e299a
4
- data.tar.gz: 629e9cdd06bc7c0534cd81ea75d9fb6a703f38582610411219b09c300f8c12b0
3
+ metadata.gz: d9149e99ff7a9a4af75efae4b681523e7a83461758c3c3f432480b25ab3f2501
4
+ data.tar.gz: c3a8655e4a507c56d7e1e64cf143981c1e5c441aecc8699cfb0a56f1cca32837
5
5
  SHA512:
6
- metadata.gz: a19044ae13e8ae2e68224a7236fc45ac998532185374e0632cc574ab204574e04e9139b05d62a404141b5bf1788aede97a8c11fc14b4489a58c7683ff2efc921
7
- data.tar.gz: 24833aa45d3b892b2530c24db608f55eb210aa1d7ec758529e66e6bb25e6ab0dd9c0a47e6f64b5beebb4d2195dd9d1ca0d136e572d0f6d0a12a926590a0f503f
6
+ metadata.gz: 20638a380654830de9f4ef8f8ffe187f0940f7eb3b7c7797d7fd2fca46901ed6b2ef2c73fad39ab9b235afcc30f04a57b6a46786d366f405b5889105c4470dfb
7
+ data.tar.gz: 6674132740272eac4eb1783fb15abfc382d962b42f0b5db93a00df7cec717af3bd33828c561d3df12ad48f30f2158ef413f9c13c907fad6fdf75e51e259c1041
@@ -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
- trace_ctx = Grpcdc.trace_start("client", path, Grpcdc.parse_traceparent(metadata["traceparent"]))
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}-01"
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 -> [trace_id, span_id] or nil.
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
- begin
107
- @tracer.on_start(kind, method, ctx["trace_id"], ctx["span_id"], parent_span_id)
108
- rescue StandardError
109
- # observers must never break the call
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
@@ -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.38.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/master/packages/grpc-ruby
43
- changelog_uri: https://github.com/browser-grpc/grpc-over-datachannels/blob/master/CHANGELOG.md
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