grpc_opencensus_interceptor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Gemfile.lock +6 -6
- data/README.md +14 -1
- data/grpc_opencensus_interceptor.gemspec +1 -1
- data/lib/grpc_opencensus_interceptor.rb +6 -1
- data/lib/grpc_opencensus_interceptor/client_interceptor.rb +83 -0
- data/lib/grpc_opencensus_interceptor/server_interceptor.rb +6 -56
- data/lib/grpc_opencensus_interceptor/util.rb +56 -0
- data/lib/grpc_opencensus_interceptor/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 858e38644b3a323462339607aed2c45ae1cdb317de12d345094911a5a6a733ef
|
4
|
+
data.tar.gz: d43818860944836bdc399e7a54c642a572c4da4df416a8c710bddd1d0c4b172b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b73ac05b6a2b37dba65cfda255b93d01d30a050bb2c76a2c1e50ee3a44c8431909361e11b14a0f2e93894524bf60a484d1d50d4136539cb757a9ea9a39ad27c
|
7
|
+
data.tar.gz: 511f290f14c2d0ad8ae92fe31a40e694b10483b96431a86315315bd4f5da40991eb6c599aa004bab78c033950d7b9ba969b9c6ed601e8aa347fde9f4c183f52d
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grpc_opencensus_interceptor (0.0.
|
4
|
+
grpc_opencensus_interceptor (0.0.2)
|
5
5
|
grpc
|
6
6
|
opencensus
|
7
7
|
|
@@ -10,11 +10,11 @@ GEM
|
|
10
10
|
specs:
|
11
11
|
coderay (1.1.2)
|
12
12
|
diff-lcs (1.3)
|
13
|
-
google-protobuf (3.11.
|
13
|
+
google-protobuf (3.11.4)
|
14
14
|
googleapis-common-protos-types (1.0.4)
|
15
15
|
google-protobuf (~> 3.0)
|
16
|
-
grpc (1.
|
17
|
-
google-protobuf (~> 3.
|
16
|
+
grpc (1.27.0)
|
17
|
+
google-protobuf (~> 3.11)
|
18
18
|
googleapis-common-protos-types (~> 1.0)
|
19
19
|
method_source (0.9.2)
|
20
20
|
opencensus (0.5.0)
|
@@ -24,7 +24,7 @@ GEM
|
|
24
24
|
pry-doc (1.0.0)
|
25
25
|
pry (~> 0.11)
|
26
26
|
yard (~> 0.9.11)
|
27
|
-
rake (
|
27
|
+
rake (13.0.1)
|
28
28
|
rspec (3.9.0)
|
29
29
|
rspec-core (~> 3.9.0)
|
30
30
|
rspec-expectations (~> 3.9.0)
|
@@ -48,7 +48,7 @@ DEPENDENCIES
|
|
48
48
|
grpc_opencensus_interceptor!
|
49
49
|
pry
|
50
50
|
pry-doc
|
51
|
-
rake (~>
|
51
|
+
rake (~> 13.0)
|
52
52
|
rspec (~> 3.0)
|
53
53
|
|
54
54
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -19,6 +19,7 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
+
### Server
|
22
23
|
Please set a `GrpcOpencensusInterceptor` as an interceptor of your gRPC application.
|
23
24
|
|
24
25
|
```ruby
|
@@ -27,13 +28,25 @@ require 'grpc_opencensus_interceptor'
|
|
27
28
|
|
28
29
|
server = GRPC::RpcServer.new(
|
29
30
|
interceptors: [
|
30
|
-
GrpcOpencensusInterceptor.new,
|
31
|
+
GrpcOpencensusInterceptor::ServerInterceptor.new,
|
31
32
|
]
|
32
33
|
)
|
33
34
|
server.handle(MyHandler.new)
|
34
35
|
server.run_till_terminated_or_interrupted(['SIGINT'])
|
35
36
|
```
|
36
37
|
|
38
|
+
### Client
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'grpc'
|
42
|
+
require 'grpc_opencensus_interceptor'
|
43
|
+
url = "dns:test-service:80"
|
44
|
+
stub = TestService::Stub.new(url, :this_channel_is_insecure, interceptors: [
|
45
|
+
GrpcOpencensusInterceptor::ClientInterceptor.new,
|
46
|
+
])
|
47
|
+
stub.hello_rpc
|
48
|
+
```
|
49
|
+
|
37
50
|
## Development
|
38
51
|
|
39
52
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.require_paths = ["lib"]
|
27
27
|
|
28
28
|
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
-
spec.add_development_dependency "rake", "~>
|
29
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
30
30
|
spec.add_development_dependency "rspec", "~> 3.0"
|
31
31
|
spec.add_development_dependency "pry"
|
32
32
|
spec.add_development_dependency "pry-doc"
|
@@ -1,5 +1,10 @@
|
|
1
|
-
require "
|
1
|
+
require "opencensus"
|
2
|
+
require "grpc"
|
3
|
+
|
4
|
+
require "grpc_opencensus_interceptor/client_interceptor"
|
2
5
|
require "grpc_opencensus_interceptor/server_interceptor"
|
6
|
+
require "grpc_opencensus_interceptor/util"
|
7
|
+
require "grpc_opencensus_interceptor/version"
|
3
8
|
|
4
9
|
module GrpcOpencensusInterceptor
|
5
10
|
class << self
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module GrpcOpencensusInterceptor
|
2
|
+
class ClientInterceptor < GRPC::ClientInterceptor
|
3
|
+
# @param [SpanContext] span_context The span context within which
|
4
|
+
# to create spans. Optional: If omitted, spans are created in the
|
5
|
+
# current thread-local span context.
|
6
|
+
# @param [#call] sampler The sampler to use when creating spans.
|
7
|
+
# Optional: If omitted, uses the sampler in the current config.
|
8
|
+
def initialize(span_context: nil, sampler: nil)
|
9
|
+
@span_context = span_context || OpenCensus::Trace
|
10
|
+
@sampler = sampler
|
11
|
+
@serializer = OpenCensus::Trace::Formatters::Binary.new
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Intercept a unary request response call
|
16
|
+
#
|
17
|
+
# @param [Object] request
|
18
|
+
# @param [GRPC::ActiveCall] call
|
19
|
+
# @param [String] method
|
20
|
+
# @param [Hash] metadata
|
21
|
+
#
|
22
|
+
def request_response(request:, call:, method:, metadata:)
|
23
|
+
span_context = @span_context
|
24
|
+
if span_context == OpenCensus::Trace && !span_context.span_context
|
25
|
+
return yield
|
26
|
+
end
|
27
|
+
|
28
|
+
# NOTE: Use method as span name
|
29
|
+
span_name = method
|
30
|
+
|
31
|
+
span = span_context.start_span span_name, sampler: @sampler
|
32
|
+
start_request span, method, metadata
|
33
|
+
begin
|
34
|
+
grpc_ex = GRPC::Ok.new
|
35
|
+
yield
|
36
|
+
rescue StandardError => e
|
37
|
+
grpc_ex = Util.to_grpc_ex(e)
|
38
|
+
raise e
|
39
|
+
ensure
|
40
|
+
finish_request span, grpc_ex
|
41
|
+
span_context.end_span span
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# NOTE: For now, we don't support server_streamer, client_streamer and bidi_streamer
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
##
|
50
|
+
# @private Set span attributes
|
51
|
+
#
|
52
|
+
# @param [OpenCensus::Trace::SpanBuilder] span
|
53
|
+
# @param [String] method
|
54
|
+
# @param [Hash] metadata
|
55
|
+
#
|
56
|
+
def start_request span, method, metadata
|
57
|
+
span.kind = OpenCensus::Trace::SpanBuilder::CLIENT
|
58
|
+
span.put_attribute "http.method", "POST" # NOTE: Always use POST
|
59
|
+
|
60
|
+
span.put_attribute "http.path", method
|
61
|
+
|
62
|
+
# TODO(south37) Set attributes
|
63
|
+
# span.put_message_event SpanBuilder::SENT, 1, body_size
|
64
|
+
|
65
|
+
trace_context = @serializer.serialize span.context.trace_context
|
66
|
+
metadata[Util::OPENCENSUS_TRACE_BIN_KEY] = trace_context
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# @private Set span attributes from response
|
71
|
+
#
|
72
|
+
# @param [OpenCensus::Trace::SpanBuilder] span
|
73
|
+
# @param [GRPC::BadStatus] exception
|
74
|
+
#
|
75
|
+
def finish_request span, exception
|
76
|
+
span.set_status exception.code
|
77
|
+
span.put_attribute "http.status_code", Util.to_http_status(exception)
|
78
|
+
|
79
|
+
# TODO(south37) Set attributes
|
80
|
+
# span.put_message_event SpanBuilder::RECEIVED, 1, body_size
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -1,10 +1,7 @@
|
|
1
|
-
require "opencensus"
|
2
|
-
require "grpc"
|
3
1
|
require "grpc_opencensus_interceptor/server_interceptor/span_modifier"
|
4
2
|
|
5
3
|
module GrpcOpencensusInterceptor
|
6
4
|
class ServerInterceptor < GRPC::ServerInterceptor
|
7
|
-
OPENCENSUS_TRACE_BIN_KEY = "grpc-trace-bin".freeze
|
8
5
|
|
9
6
|
# @param [#export] exporter The exported used to export captured spans
|
10
7
|
# at the end of the request. Optional: If omitted, uses the exporter
|
@@ -26,11 +23,11 @@ module GrpcOpencensusInterceptor
|
|
26
23
|
# @param [Method] method
|
27
24
|
#
|
28
25
|
def request_response(request:, call:, method:)
|
29
|
-
context_bin = call.metadata[OPENCENSUS_TRACE_BIN_KEY]
|
26
|
+
context_bin = call.metadata[Util::OPENCENSUS_TRACE_BIN_KEY]
|
30
27
|
if context_bin
|
31
28
|
context = deserialize(context_bin)
|
32
29
|
else
|
33
|
-
|
30
|
+
context = nil
|
34
31
|
end
|
35
32
|
|
36
33
|
OpenCensus::Trace.start_request_trace(
|
@@ -45,7 +42,7 @@ module GrpcOpencensusInterceptor
|
|
45
42
|
grpc_ex = GRPC::Ok.new
|
46
43
|
yield
|
47
44
|
rescue StandardError => e
|
48
|
-
grpc_ex = to_grpc_ex(e)
|
45
|
+
grpc_ex = Util.to_grpc_ex(e)
|
49
46
|
raise e
|
50
47
|
ensure
|
51
48
|
finish_request(span, grpc_ex)
|
@@ -67,6 +64,7 @@ module GrpcOpencensusInterceptor
|
|
67
64
|
@deserializer.deserialize(context_bin)
|
68
65
|
end
|
69
66
|
|
67
|
+
##
|
70
68
|
# Span name is represented as $package.$service/$method
|
71
69
|
# cf. https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/gRPC.md#spans
|
72
70
|
#
|
@@ -88,6 +86,7 @@ module GrpcOpencensusInterceptor
|
|
88
86
|
term.split("_").map(&:capitalize).join
|
89
87
|
end
|
90
88
|
|
89
|
+
##
|
91
90
|
# Modify span by custom span modifier
|
92
91
|
#
|
93
92
|
# @param [OpenCensus::Trace::SpanBuilder] span
|
@@ -116,56 +115,7 @@ module GrpcOpencensusInterceptor
|
|
116
115
|
# Set gRPC server status
|
117
116
|
# https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/gRPC.md#spans
|
118
117
|
span.set_status exception.code
|
119
|
-
span.put_attribute "http.status_code", to_http_status(exception)
|
120
|
-
end
|
121
|
-
|
122
|
-
# cf. https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
|
123
|
-
# cf. https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#mapping-from-http-status-codes-to-trace-status-codes
|
124
|
-
#
|
125
|
-
# @param [GRPC::BadStatus] exception
|
126
|
-
# @return [Integer]
|
127
|
-
def to_http_status(exception)
|
128
|
-
case exception
|
129
|
-
when GRPC::Ok
|
130
|
-
200
|
131
|
-
when GRPC::InvalidArgument
|
132
|
-
400
|
133
|
-
when GRPC::DeadlineExceeded
|
134
|
-
504
|
135
|
-
when GRPC::NotFound
|
136
|
-
404
|
137
|
-
when GRPC::PermissionDenied
|
138
|
-
403
|
139
|
-
when GRPC::Unauthenticated
|
140
|
-
401
|
141
|
-
when GRPC::Aborted
|
142
|
-
# For GRPC::Aborted, grpc-gateway uses 409. We do the same.
|
143
|
-
# cf. https://github.com/grpc-ecosystem/grpc-gateway/blob/e8db07a3923d3f5c77dbcea96656afe43a2757a8/runtime/errors.go#L17-L58
|
144
|
-
409
|
145
|
-
when GRPC::ResourceExhausted
|
146
|
-
429
|
147
|
-
when GRPC::Unimplemented
|
148
|
-
501
|
149
|
-
when GRPC::Unavailable
|
150
|
-
503
|
151
|
-
when GRPC::Unknown
|
152
|
-
# NOTE: This is not same with the correct mapping
|
153
|
-
500
|
154
|
-
else
|
155
|
-
# NOTE: Here, we use 500 temporarily.
|
156
|
-
500
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
# @param [Exception] e
|
161
|
-
# @return [GRPC::BadStatus] e
|
162
|
-
def to_grpc_ex(e)
|
163
|
-
case e
|
164
|
-
when GRPC::BadStatus
|
165
|
-
e
|
166
|
-
else
|
167
|
-
GRPC::Unknown.new(e.message)
|
168
|
-
end
|
118
|
+
span.put_attribute "http.status_code", Util.to_http_status(exception)
|
169
119
|
end
|
170
120
|
end
|
171
121
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module GrpcOpencensusInterceptor
|
2
|
+
module Util
|
3
|
+
OPENCENSUS_TRACE_BIN_KEY = "grpc-trace-bin".freeze
|
4
|
+
|
5
|
+
class << self
|
6
|
+
# cf. https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
|
7
|
+
# cf. https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#mapping-from-http-status-codes-to-trace-status-codes
|
8
|
+
#
|
9
|
+
# @param [GRPC::BadStatus] exception
|
10
|
+
# @return [Integer]
|
11
|
+
def to_http_status(exception)
|
12
|
+
case exception
|
13
|
+
when GRPC::Ok
|
14
|
+
200
|
15
|
+
when GRPC::InvalidArgument
|
16
|
+
400
|
17
|
+
when GRPC::DeadlineExceeded
|
18
|
+
504
|
19
|
+
when GRPC::NotFound
|
20
|
+
404
|
21
|
+
when GRPC::PermissionDenied
|
22
|
+
403
|
23
|
+
when GRPC::Unauthenticated
|
24
|
+
401
|
25
|
+
when GRPC::Aborted
|
26
|
+
# For GRPC::Aborted, grpc-gateway uses 409. We do the same.
|
27
|
+
# cf. https://github.com/grpc-ecosystem/grpc-gateway/blob/e8db07a3923d3f5c77dbcea96656afe43a2757a8/runtime/errors.go#L17-L58
|
28
|
+
409
|
29
|
+
when GRPC::ResourceExhausted
|
30
|
+
429
|
31
|
+
when GRPC::Unimplemented
|
32
|
+
501
|
33
|
+
when GRPC::Unavailable
|
34
|
+
503
|
35
|
+
when GRPC::Unknown
|
36
|
+
# NOTE: This is not same with the correct mapping
|
37
|
+
500
|
38
|
+
else
|
39
|
+
# NOTE: Here, we use 500 temporarily.
|
40
|
+
500
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param [Exception] e
|
45
|
+
# @return [GRPC::BadStatus] e
|
46
|
+
def to_grpc_ex(e)
|
47
|
+
case e
|
48
|
+
when GRPC::BadStatus
|
49
|
+
e
|
50
|
+
else
|
51
|
+
GRPC::Unknown.new(e.message)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grpc_opencensus_interceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nao Minami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,8 +128,10 @@ files:
|
|
128
128
|
- bin/setup
|
129
129
|
- grpc_opencensus_interceptor.gemspec
|
130
130
|
- lib/grpc_opencensus_interceptor.rb
|
131
|
+
- lib/grpc_opencensus_interceptor/client_interceptor.rb
|
131
132
|
- lib/grpc_opencensus_interceptor/server_interceptor.rb
|
132
133
|
- lib/grpc_opencensus_interceptor/server_interceptor/span_modifier.rb
|
134
|
+
- lib/grpc_opencensus_interceptor/util.rb
|
133
135
|
- lib/grpc_opencensus_interceptor/version.rb
|
134
136
|
homepage: https://github.com/south37/grpc_opencensus_interceptor
|
135
137
|
licenses:
|