gruf-stackdriver-trace 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ca00c8725668e0974387526b28f3247e9fdfe540cb074dbe44ef456c6c0a5ac0
4
+ data.tar.gz: 417d37540f9e577ed0af5c7553480e8dec5203dbeeaa6536ff84685fd0fd5794
5
+ SHA512:
6
+ metadata.gz: 4b20fece431eeca45b5903157ecaa96c3aa1aa5817284e90cf4bbe46892319a564b49696a42fe6811326c1feb4a75a333e885d7b7deba3f5239ed347dd0ba73f
7
+ data.tar.gz: 7f82e02e2b304aa18e9499012e387574ad7d556b34f9de66e3a64ffc4cf74bc75f0d71781fc07e5e12c6b24d53682923bd0b2de680be7d5728ce87ad6d82ee0e
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dameleon@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in gruf-stackdriver-trace.gemspec
6
+ gemspec
@@ -0,0 +1,115 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gruf-stackdriver-trace (0.1.0)
5
+ activesupport (> 4)
6
+ google-cloud-env (~> 1.3)
7
+ google-cloud-trace (~> 0.35)
8
+ grpc (~> 1.21)
9
+ gruf (~> 2.5)
10
+ stackdriver-core (~> 1.3)
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activesupport (6.0.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
+ addressable (2.7.0)
22
+ public_suffix (>= 2.0.2, < 5.0)
23
+ concurrent-ruby (1.1.5)
24
+ diff-lcs (1.3)
25
+ faraday (0.17.1)
26
+ multipart-post (>= 1.2, < 3)
27
+ google-cloud-core (1.4.1)
28
+ google-cloud-env (~> 1.0)
29
+ google-cloud-env (1.3.0)
30
+ faraday (~> 0.11)
31
+ google-cloud-trace (0.36.1)
32
+ concurrent-ruby (~> 1.1)
33
+ google-cloud-core (~> 1.2)
34
+ google-gax (~> 1.8)
35
+ googleapis-common-protos (>= 1.3.9, < 2.0)
36
+ googleapis-common-protos-types (>= 1.0.4, < 2.0)
37
+ stackdriver-core (~> 1.3)
38
+ google-gax (1.8.1)
39
+ google-protobuf (~> 3.9)
40
+ googleapis-common-protos (>= 1.3.9, < 2.0)
41
+ googleauth (~> 0.9)
42
+ grpc (~> 1.24)
43
+ rly (~> 0.2.3)
44
+ google-protobuf (3.11.0)
45
+ googleapis-common-protos (1.3.9)
46
+ google-protobuf (~> 3.0)
47
+ googleapis-common-protos-types (~> 1.0)
48
+ grpc (~> 1.0)
49
+ googleapis-common-protos-types (1.0.4)
50
+ google-protobuf (~> 3.0)
51
+ googleauth (0.10.0)
52
+ faraday (~> 0.12)
53
+ jwt (>= 1.4, < 3.0)
54
+ memoist (~> 0.16)
55
+ multi_json (~> 1.11)
56
+ os (>= 0.9, < 2.0)
57
+ signet (~> 0.12)
58
+ grpc (1.25.0)
59
+ google-protobuf (~> 3.8)
60
+ googleapis-common-protos-types (~> 1.0)
61
+ grpc-tools (1.25.0)
62
+ gruf (2.7.0)
63
+ activesupport (> 4)
64
+ concurrent-ruby (> 1)
65
+ grpc (~> 1.10)
66
+ grpc-tools (~> 1.10)
67
+ slop (~> 4.6)
68
+ i18n (1.7.0)
69
+ concurrent-ruby (~> 1.0)
70
+ jwt (2.2.1)
71
+ memoist (0.16.1)
72
+ minitest (5.13.0)
73
+ multi_json (1.14.1)
74
+ multipart-post (2.1.1)
75
+ os (1.0.1)
76
+ public_suffix (4.0.1)
77
+ rake (10.5.0)
78
+ rly (0.2.3)
79
+ rspec (3.9.0)
80
+ rspec-core (~> 3.9.0)
81
+ rspec-expectations (~> 3.9.0)
82
+ rspec-mocks (~> 3.9.0)
83
+ rspec-core (3.9.0)
84
+ rspec-support (~> 3.9.0)
85
+ rspec-expectations (3.9.0)
86
+ diff-lcs (>= 1.2.0, < 2.0)
87
+ rspec-support (~> 3.9.0)
88
+ rspec-mocks (3.9.0)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.9.0)
91
+ rspec-support (3.9.0)
92
+ signet (0.12.0)
93
+ addressable (~> 2.3)
94
+ faraday (~> 0.9)
95
+ jwt (>= 1.5, < 3.0)
96
+ multi_json (~> 1.10)
97
+ slop (4.7.0)
98
+ stackdriver-core (1.4.0)
99
+ google-cloud-core (~> 1.2)
100
+ thread_safe (0.3.6)
101
+ tzinfo (1.2.5)
102
+ thread_safe (~> 0.1)
103
+ zeitwerk (2.2.2)
104
+
105
+ PLATFORMS
106
+ ruby
107
+
108
+ DEPENDENCIES
109
+ bundler (~> 2)
110
+ gruf-stackdriver-trace!
111
+ rake (~> 10.0)
112
+ rspec (~> 3.0)
113
+
114
+ BUNDLED WITH
115
+ 2.0.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Atman Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kei Takahashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,110 @@
1
+ # Gruf::StackdriverTrace
2
+
3
+ Add Stackdriver trace support for [gruf](https://github.com/bigcommerce/gruf).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gruf-stackdriver-trace'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gruf-stackdriver-trace
20
+
21
+ ## Usage
22
+
23
+ ### ServerInterceptor
24
+
25
+ Configure and use interceptor in gruf's initializer.
26
+
27
+ ```
28
+ require 'gruf/stackdriver_trace'
29
+
30
+ Gruf.configure do |c|
31
+ # optional
32
+ Gruf::StackdriverTrace.configure do |config|
33
+ end
34
+
35
+ c.interceptors.use(Gruf::StackdriverTrace::ServerInterceptor)
36
+ end
37
+ ```
38
+
39
+ ### ClientInterceptor
40
+
41
+ Specify `Gruf::StackdriverTrace::ClientInterceptor` instance to `client_options[:interceptors]`.
42
+
43
+ ```
44
+ client = Gruf::Client.new(
45
+ service: Rpc::Products,
46
+ client_options: {
47
+ interceptors: [
48
+ Gruf::StackdriverTrace::ClientInterceptor.new
49
+ ]
50
+ }
51
+ )
52
+ resp = client.call(:GetProduct)
53
+ ```
54
+
55
+ NOTE: `Gruf::StackdriverTrace::ClientInterceptor` will not be enabled if the tracing context of `Gruf::StackdriverTrace::ServerInterceptor` or Stackdriver Trace(ex. use with Rails middleware) cannot be obtained.
56
+
57
+
58
+ ### Configuration
59
+
60
+ You can configure with `Gruf::StackdriverTrace.configure { |config| ... }`.
61
+
62
+ REF: https://github.com/googleapis/google-cloud-ruby/blob/master/stackdriver/INSTRUMENTATION_CONFIGURATION.md#trace
63
+
64
+ - `config[:service]` (default: `nil`)
65
+ - A reporter Class that provides to send traced data
66
+ - SEE: https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-trace/lib/google/cloud/trace/async_reporter.rb
67
+ - `config[:sampled]` (default: `true`)
68
+ - Whether to capture tracing
69
+ - `true`: enable tracing
70
+ - `false`: disable tracing
71
+ - ex. `config[:sampled] = Rails.env.production?` to be enable tracing in only production env.
72
+ - `config[:capture_stack]` (default: `true`)
73
+ - Whether to capture stack traces for each span
74
+ - `true`: enable to recording of stack trace
75
+ - `false`: disable to recording of stack trace
76
+ - `config[:sampler]` (default: `nil`)
77
+ - A sampler Class makes the decision whether to record a trace for each request
78
+ - SEE: https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-trace/lib/google/cloud/trace/time_sampler.rb
79
+ - `config[:span_id_generator]` (default: `nil`)
80
+ - A generator Proc that generates the name String for new TraceRecord
81
+ - SEE: https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-trace/lib/google/cloud/trace/trace_record.rb#L59-L60
82
+
83
+ #### Without GCP environment
84
+
85
+ If you are running the application locally, in self-hosted VMs, or a third party hosting service, you will need to provide the project ID and credentials (keyfile) to the Google Cloud client library. See [this section](https://github.com/googleapis/google-cloud-ruby/tree/master/google-cloud-trace#running-locally-and-elsewhere) for details.
86
+
87
+ ex.
88
+
89
+ ```
90
+ Google::Cloud.configure do |config|
91
+ config.project_id = "your-project-id"
92
+ config.keyfile = "path/to/service-account.json"
93
+ end
94
+ ```
95
+
96
+ ## Development
97
+
98
+ TODO
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/atman-inc/gruf-stackdriver-trace. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
103
+
104
+ ## License
105
+
106
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
107
+
108
+ ## Code of Conduct
109
+
110
+ Everyone interacting in the Gruf::StackdriverTrace project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gruf-stackdriver-trace/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gruf/stackdriver_trace/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gruf-stackdriver-trace"
8
+ spec.version = Gruf::StackdriverTrace::VERSION
9
+ spec.authors = ["Kei Takahashi"]
10
+ spec.email = ["dameleon@gmail.com"]
11
+
12
+ spec.summary = %q{Stackdriver trace for gruf}
13
+ spec.description = %q{Plugin for Stackdirver trace for bigcommerce/gruf}
14
+ spec.homepage = "https://github.com/atman-inc/gruf-stackdriver-trace"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 2"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_dependency "gruf", "~> 2.5"
26
+ spec.add_dependency "grpc", "~> 1.21"
27
+ spec.add_dependency "stackdriver-core", "~> 1.3"
28
+ spec.add_dependency "google-cloud-env", "~> 1.3"
29
+ spec.add_dependency "google-cloud-trace", "~> 0.35"
30
+ spec.add_dependency "activesupport", "> 4"
31
+ end
@@ -0,0 +1,57 @@
1
+ require 'grpc'
2
+ require 'gruf'
3
+ require "google/cloud/env"
4
+ require "google/cloud/trace"
5
+ require "google/cloud/trace/async_reporter"
6
+ require "stackdriver/core/trace_context"
7
+ require_relative "stackdriver_trace/client_interceptor"
8
+ require_relative "stackdriver_trace/server_interceptor"
9
+ require_relative "stackdriver_trace/version"
10
+
11
+ module Gruf
12
+ module StackdriverTrace
13
+ AGENT_NAME = "gruf-stackdriver-trace #{Gruf::StackdriverTrace::VERSION}".freeze
14
+ HEADER_KEY = 'x-trace-context'.freeze
15
+
16
+ def self.configure
17
+ yield config
18
+ end
19
+
20
+ def self.config
21
+ @config ||= {
22
+ service: nil,
23
+ sampled: true,
24
+ capture_stack: true,
25
+ sampler: nil,
26
+ span_id_generator: nil
27
+ }
28
+ end
29
+
30
+ def self.service
31
+ @service ||= begin
32
+ init_default_config
33
+
34
+ return config[:service] if config[:service]
35
+ return nil unless configuration.project_id
36
+
37
+ tracer = Google::Cloud::Trace.new(
38
+ project_id: configuration.project_id,
39
+ credentials: configuration.credentials
40
+ )
41
+ Google::Cloud::Trace::AsyncReporter.new(tracer.service)
42
+ end
43
+ end
44
+
45
+ def self.init_default_config
46
+ configuration.project_id ||= Google::Cloud::Trace.default_project_id
47
+ configuration.credentials ||= Google::Cloud.configure.credentials
48
+ configuration.capture_stack = config.fetch(:capture_stack, true)
49
+ configuration.sampler = config.fetch(:sampler, nil)
50
+ configuration.span_id_generator = config.fetch(:span_id_generator, nil)
51
+ end
52
+
53
+ def self.configuration
54
+ Google::Cloud::Trace.configure
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'label'
2
+
3
+ module Gruf
4
+ module StackdriverTrace
5
+ class ClientInterceptor < Gruf::Interceptors::ClientInterceptor
6
+ include Gruf::StackdriverTrace::Label
7
+
8
+ def call(request_context:)
9
+ Google::Cloud::Trace.in_span("grpc-request") do |span|
10
+ return yield request_context unless span
11
+ set_request_metadata(request_context.metadata, span)
12
+ configure_span(span, request_context)
13
+ result = Gruf::Interceptors::Timer.time do
14
+ yield request_context
15
+ end
16
+ add_response_labels(span.labels, result)
17
+ raise result.message unless result.successful?
18
+ result.message
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def set_request_metadata(metadata, span)
25
+ metadata[Gruf::StackdriverTrace::HEADER_KEY] = span.trace.trace_context.to_s
26
+ end
27
+
28
+ def configure_span(span, request_context)
29
+ method_name = request_context.method.to_s.gsub(/^\//, '')
30
+ span.name = "Sent: #{method_name}"
31
+ set_stack_trace(span, 4)
32
+ set_basic_labels(span.labels, request_context, method_name)
33
+ set_label(span.labels, label_key::RPC_REQUEST_TYPE, request_context.type.to_s)
34
+ span
35
+ end
36
+
37
+ def add_response_labels(labels, result)
38
+ code = result.successful? ? ::GRPC::Core::StatusCodes::OK : result.message.code
39
+ set_grpc_status_code(labels, code)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ module Gruf
2
+ module StackdriverTrace
3
+ module Label
4
+ def status_code_to_label(code)
5
+ @lookup ||= Hash[GRPC::Core::StatusCodes.constants.map do |c|
6
+ [GRPC::Core::StatusCodes.const_get(c), c.to_s]
7
+ end]
8
+ @lookup[code]
9
+ end
10
+
11
+ def label_key
12
+ Google::Cloud::Trace::LabelKey
13
+ end
14
+
15
+ def set_label(labels, key, value)
16
+ labels[key] = value.to_s
17
+ end
18
+
19
+ def set_basic_labels(labels, request, method_name)
20
+ set_label(labels, label_key::AGENT, Gruf::StackdriverTrace::AGENT_NAME)
21
+ set_label(labels, label_key::HTTP_HOST, get_host(request))
22
+ set_label(labels, label_key::HTTP_CLIENT_PROTOCOL, 'http2')
23
+ set_label(labels, label_key::HTTP_USER_AGENT, get_ua(request))
24
+ set_label(labels, label_key::HTTP_URL, method_name)
25
+ set_label(labels, label_key::PID, ::Process.pid)
26
+ set_label(labels, label_key::TID, ::Thread.current.object_id)
27
+ end
28
+
29
+ def set_stack_trace(span, skip_frames)
30
+ tc = span.trace.trace_context
31
+ return unless tc.capture_stack?
32
+ label_key.set_stack_trace(span.labels, skip_frames: skip_frames)
33
+ end
34
+
35
+ def set_grpc_status_code(labels, status_code)
36
+ set_label(labels, label_key::RPC_STATUS_CODE, status_code_to_label(status_code))
37
+ end
38
+
39
+ def get_ua(request)
40
+ metadata = request_context?(request) ? request.metadata : request.active_call.metadata
41
+ metadata['user-agent'] || nil
42
+ end
43
+
44
+ def get_host(request)
45
+ if request_context?(request)
46
+ # get request host from GRPC::ActiveCall::InterceptableView
47
+ request.call.instance_variable_get(:@wrapped).try(:peer)
48
+ else
49
+ request.active_call.peer
50
+ end
51
+ end
52
+
53
+ def request_context?(request)
54
+ request.is_a?(Gruf::Outbound::RequestContext)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,149 @@
1
+ require_relative 'label'
2
+
3
+ module Gruf
4
+ module StackdriverTrace
5
+ class ServerInterceptor < Gruf::Interceptors::ServerInterceptor
6
+ include Gruf::StackdriverTrace::Label
7
+
8
+ def call
9
+ return yield if service.nil?
10
+
11
+ trace = create_trace(request)
12
+ begin
13
+ Google::Cloud::Trace.set(trace)
14
+ Google::Cloud::Trace.in_span("grpc-request-received") do |span|
15
+ configure_span(span, request)
16
+ result = Gruf::Interceptors::Timer.time do
17
+ yield
18
+ end
19
+ set_grpc_status_code(
20
+ span.labels,
21
+ result.successful? ? ::GRPC::Core::StatusCodes::OK : result.message.code
22
+ )
23
+ raise result.message unless result.successful?
24
+ result.message
25
+ end
26
+ ensure
27
+ Google::Cloud::Trace.set(nil)
28
+ send_trace(trace)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def create_trace(request)
35
+ trace_context = get_trace_context(request)
36
+ Google::Cloud::Trace::TraceRecord.new(
37
+ service.project,
38
+ trace_context,
39
+ span_id_generator: configuration.span_id_generator
40
+ )
41
+ end
42
+
43
+ def get_trace_context(request)
44
+ rack_env = simulated_rack_env(request)
45
+ sampler = configuration.sampler || Google::Cloud::Trace::TimeSampler.default
46
+ header = request.active_call.metadata[Gruf::StackdriverTrace::HEADER_KEY]
47
+ return Stackdriver::Core::TraceContext.new(
48
+ sampled: Gruf::StackdriverTrace.config[:sampled] && sampler.call(rack_env),
49
+ capture_stack: Gruf::StackdriverTrace.config[:capture_stack]
50
+ ) unless header
51
+ tc = Stackdriver::Core::TraceContext.parse_string(header)
52
+ if tc.sampled?.nil?
53
+ sampled = sampler.call(rack_env)
54
+ tc = Stackdriver::Core::TraceContext.new(
55
+ trace_id: tc.trace_id,
56
+ span_id: tc.span_id,
57
+ sampled: sampled,
58
+ capture_stack: sampled && configuration.capture_stack
59
+ )
60
+ end
61
+ tc
62
+ end
63
+
64
+ def configure_span(span, request)
65
+ method_name = [
66
+ request.service.service_name,
67
+ request.method_key.to_s.camelize
68
+ ].join('/')
69
+ span.name = "Recv: #{method_name}"
70
+ set_stack_trace(span, 4)
71
+ set_basic_labels(span.labels, request, method_name)
72
+ set_extended_labels(span.labels)
73
+ span
74
+ end
75
+
76
+ def set_extended_labels(labels)
77
+ if Google::Cloud.env.app_engine?
78
+ set_label(
79
+ labels,
80
+ label_key::GAE_APP_MODULE,
81
+ Google::Cloud.env.app_engine_service_id
82
+ )
83
+ set_label(
84
+ labels,
85
+ label_key::GAE_APP_MODULE_VERSION,
86
+ Google::Cloud.env.app_engine_service_version
87
+ )
88
+ end
89
+ # TODO: set GKE env options
90
+ end
91
+
92
+ def send_trace(trace)
93
+ if trace.trace_context.sampled?
94
+ begin
95
+ service.patch_traces(trace)
96
+ rescue StandardError => e
97
+ handle_error(e, logger: Gruf.logger)
98
+ end
99
+ end
100
+ end
101
+
102
+ def handle_error(error, logger: nil)
103
+ if error_callback
104
+ error_callback.call(error)
105
+ else
106
+ msg = "Transmit to Stackdriver Trace failed: #{error.inspect}"
107
+ if logger
108
+ logger.error(msg)
109
+ else
110
+ warn(msg)
111
+ end
112
+ end
113
+ end
114
+
115
+ def error_callback
116
+ if @error_callback.nil?
117
+ @error_callback = :unset
118
+ configuration_callback = configuration.on_error
119
+ configuration_callback ||= Google::Cloud.configure.on_error
120
+ @error_callback = configuration_callback if configuration_callback
121
+ end
122
+
123
+ return nil if @error_callback == :unset
124
+ @error_callback
125
+ end
126
+
127
+ def simulated_rack_env(request)
128
+ # REF: https://github.com/googleapis/google-cloud-ruby/blob/2ad4e22280dc8a2e61613251461e8049129deb4b/google-cloud-trace/lib/google/cloud/trace/time_sampler.rb#L98-L102
129
+ # simulate rack env with Gruf's request for dependencies compatibility(ex. TimeSampler#call)
130
+ # for example
131
+ # SCRIPT_NAME => 'grpc.health.v1',
132
+ # PATH_INFO => '/Check'
133
+ # be passed 'grpc.health.v1/Check' to blacklist checking
134
+ {
135
+ 'SCRIPT_NAME' => request.service.service_name,
136
+ 'PATH_INFO' => "/#{request.method_key.to_s.camelize}"
137
+ }
138
+ end
139
+
140
+ def service
141
+ @service ||= Gruf::StackdriverTrace.service
142
+ end
143
+
144
+ def configuration
145
+ @configuration ||= Gruf::StackdriverTrace.configuration
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,5 @@
1
+ module Gruf
2
+ module StackdriverTrace
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gruf-stackdriver-trace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kei Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gruf
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: grpc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ - !ruby/object:Gem::Dependency
84
+ name: stackdriver-core
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: google-cloud-env
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.3'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: google-cloud-trace
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.35'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.35'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ version: '4'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">"
137
+ - !ruby/object:Gem::Version
138
+ version: '4'
139
+ description: Plugin for Stackdirver trace for bigcommerce/gruf
140
+ email:
141
+ - dameleon@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".ruby-version"
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - Gemfile.lock
152
+ - LICENSE
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - gruf-stackdriver-trace.gemspec
157
+ - lib/gruf/stackdriver_trace.rb
158
+ - lib/gruf/stackdriver_trace/client_interceptor.rb
159
+ - lib/gruf/stackdriver_trace/label.rb
160
+ - lib/gruf/stackdriver_trace/server_interceptor.rb
161
+ - lib/gruf/stackdriver_trace/version.rb
162
+ homepage: https://github.com/atman-inc/gruf-stackdriver-trace
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.7.6
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Stackdriver trace for gruf
186
+ test_files: []