datadog_thread_tracer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a9269375e08409b1f01256df83467ac65683ff520c8cf3b2f97b3ee65f71ccc
4
+ data.tar.gz: f9a818c5c1c369903a72b35cf7c3d72cbfc0c2639d923210a8d4612230f74fd0
5
+ SHA512:
6
+ metadata.gz: '09c27a91709e8da2cb1126452f85e8cc79c888f9eaf957c96c61e9f53b082cdb0a357b854d807a5ac15d4b62fb452088abad146891bdaed922edb02f3403b1ed'
7
+ data.tar.gz: b7debfd6ee57ac0a3078a9916d420365f62aafc6b615b7cb6e5f03183e17311194e6473e56f08eb348ced002b9fb83f0e683f3810ac311ddf4f20f22f8da2a69
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ DD_API_KEY=
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+ TargetRubyVersion: 2.6
8
+
9
+ Layout/DotPosition:
10
+ EnforcedStyle: trailing
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - "spec/**/*"
15
+ - "*.gemspec"
16
+
17
+ Style/StringLiterals:
18
+ Enabled: true
19
+ EnforcedStyle: double_quotes
20
+
21
+ Style/StringLiteralsInInterpolation:
22
+ Enabled: true
23
+ EnforcedStyle: double_quotes
24
+
25
+ Layout/LineLength:
26
+ Max: 120
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --markup markdown
2
+ --no-private
3
+ --hide-void-return
4
+ -
5
+ CHANGELOG.md
6
+ LICENSE.txt
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-10
4
+
5
+ - Initial release
data/Dockerfile ADDED
@@ -0,0 +1,7 @@
1
+ FROM ruby:3.1.2
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . /app
6
+
7
+ RUN bundle install
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in datadog_thread_tracer.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 sue445
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.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # DatadogThreadTracer
2
+
3
+ [ddtrace](https://github.com/DataDog/dd-trace-rb) helper to collect traces in thread
4
+
5
+ [![test](https://github.com/sue445/datadog_thread_tracer/actions/workflows/test.yml/badge.svg)](https://github.com/sue445/datadog_thread_tracer/actions/workflows/test.yml)
6
+
7
+ ## Example
8
+ ### Before (without datadog_thread_tracer)
9
+ ![before](img/before.png)
10
+
11
+ ### After (with datadog_thread_tracer)
12
+ ![after](img/after.png)
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'datadog_thread_tracer'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install datadog_thread_tracer
29
+
30
+ ## Usage
31
+ ```ruby
32
+ require "datadog_thread_tracer"
33
+
34
+ DatadogThreadTracer.trace do |t|
35
+ t.trace do
36
+ # do something. (this block is called in thread)
37
+ end
38
+
39
+ t.trace(trace_name: "trace_name") do
40
+ # do something. (this block is called in thread)
41
+ end
42
+
43
+ # Pass single arg to Thread
44
+ t.trace(thread_args: 1) do |a|
45
+ a
46
+ #=> 1
47
+ end
48
+
49
+ # Pass multiple args to Thread
50
+ t.trace(thread_args: [2, 3]) do |a, b|
51
+ a + b
52
+ #=> 5
53
+ end
54
+ end
55
+ ```
56
+
57
+ ## Development
58
+
59
+ 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.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Integration test
64
+ ```bash
65
+ cp .env.example .env
66
+ vi .env
67
+ docker-compose up --build
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sue445/datadog_thread_tracer.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/datadog_thread_tracer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "datadog_thread_tracer"
7
+ spec.version = DatadogThreadTracer::VERSION
8
+ spec.authors = ["sue445"]
9
+ spec.email = ["sue445@sue445.net"]
10
+
11
+ spec.summary = "ddtrace helper to collect traces in thread"
12
+ spec.description = "ddtrace helper to collect traces in thread"
13
+ spec.homepage = "https://github.com/sue445/datadog_thread_tracer"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
20
+ spec.metadata["rubygems_mfa_required"] = "true"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features|img)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "ddtrace", ">= 1.0.0"
34
+
35
+ spec.add_development_dependency "rake"
36
+ spec.add_development_dependency "rspec"
37
+ spec.add_development_dependency "rubocop"
38
+ spec.add_development_dependency "rubocop_auto_corrector"
39
+ spec.add_development_dependency "rubocop-performance"
40
+ spec.add_development_dependency "webmock"
41
+ spec.add_development_dependency "yard"
42
+
43
+ # For more information and examples about making a new gem, check out our
44
+ # guide at: https://bundler.io/guides/creating_gem.html
45
+ end
@@ -0,0 +1,37 @@
1
+ version: "3"
2
+
3
+ services:
4
+ app:
5
+ build: .
6
+ command: bundle exec ruby spec/integration/bench.rb
7
+ environment:
8
+ - DD_AGENT_HOST=ddagent
9
+ - DD_TRACE_SAMPLE_RATE=1.0
10
+ depends_on:
11
+ - ddagent
12
+ volumes:
13
+ - .:/app
14
+ - bundle:/app/vendor/bundle
15
+
16
+ ddagent:
17
+ image: gcr.io/datadoghq/agent:latest
18
+
19
+ restart: always
20
+
21
+ ports:
22
+ - "8126:8126/tcp"
23
+
24
+ environment:
25
+ - DD_API_KEY
26
+ - DD_TAGS="service:datadog_thread_tracer"
27
+ - DD_LOG_LEVEL=WARN
28
+ - DD_APM_ENABLED=true
29
+ - DD_APM_NON_LOCAL_TRAFFIC=true
30
+
31
+ volumes:
32
+ - "/var/run/docker.sock:/var/run/docker.sock:ro"
33
+ - "/proc/:/host/proc/:ro"
34
+ - "/sys/fs/cgroup/:/host/sys/fs/cgroup:ro"
35
+
36
+ volumes:
37
+ bundle:
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatadogThreadTracer
4
+ # trace within threads
5
+ class ThreadTracer
6
+ def initialize
7
+ @threads = []
8
+ @thread_count = 0
9
+ end
10
+
11
+ # @param trace_name [String]
12
+ # @param thread_args [Array,Object] Passed to `Thread.start`
13
+ # @yield Processes you want to execute in a thread
14
+ #
15
+ # @example Pass `trace_name`
16
+ # DatadogThreadTracer.trace do |t|
17
+ # t.trace(trace_name: "trace_name") do
18
+ # # do something. (this block is called in thread)
19
+ # end
20
+ # end
21
+ #
22
+ # @example Pass single arg to `Thread`
23
+ # DatadogThreadTracer.trace do |t|
24
+ # t.trace(thread_args: 1) do |a|
25
+ # a
26
+ # #=> 1
27
+ # end
28
+ # end
29
+ #
30
+ # @example Pass multiple args to `Thread`
31
+ # DatadogThreadTracer.trace do |t|
32
+ # t.trace(thread_args: [2, 3]) do |a, b|
33
+ # a + b
34
+ # #=> 5
35
+ # end
36
+ # end
37
+ #
38
+ def trace(trace_name: nil, thread_args: [])
39
+ @thread_count += 1
40
+ trace_name ||= "thread_#{@thread_count}"
41
+
42
+ tracer = Datadog::Tracing.send(:tracer)
43
+
44
+ @threads << Thread.start(trace_name, tracer.provider.context, Array(thread_args)) do |name, context, args|
45
+ tracer = Datadog::Tracing.send(:tracer)
46
+
47
+ # c.f. https://github.com/DataDog/dd-trace-rb/issues/1460
48
+ tracer.provider.context = context
49
+ Datadog::Tracing.trace(name) do
50
+ yield(*args)
51
+ end
52
+ end
53
+ end
54
+
55
+ def join_threads
56
+ @threads.each(&:join)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatadogThreadTracer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ddtrace"
4
+
5
+ require_relative "datadog_thread_tracer/version"
6
+ require_relative "datadog_thread_tracer/thread_tracer"
7
+ require "datadog_thread_tracer"
8
+
9
+ module DatadogThreadTracer # rubocop:disable Style/Documentation
10
+ class Error < StandardError; end
11
+
12
+ DEFAULT_TRACE_NAME = "DatadogThreadTracer.trace"
13
+
14
+ # @param trace_name [String]
15
+ # @yield Processes you want to execute in a thread
16
+ # @yieldparam [DatadogThreadTracer::ThreadTracer] t
17
+ #
18
+ # @example
19
+ # DatadogThreadTracer.trace do |t|
20
+ # t.trace do
21
+ # # do something. (this block is called in thread)
22
+ # end
23
+ # end
24
+ #
25
+ def self.trace(trace_name = DEFAULT_TRACE_NAME)
26
+ Datadog::Tracing.trace(trace_name) do
27
+ t = DatadogThreadTracer::ThreadTracer.new
28
+
29
+ yield t
30
+
31
+ t.join_threads
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module DatadogThreadTracer
2
+ class ThreadTracer
3
+ def initialize: () -> void
4
+
5
+ def trace: (?trace_name: string, ?thread_args: untyped) { (*untyped) -> untyped } -> untyped
6
+
7
+ def join_threads: () -> untyped
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module DatadogThreadTracer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+
5
+ class Error < StandardError
6
+ end
7
+
8
+ DEFAULT_TRACE_NAME: ::String
9
+
10
+ def self.trace: (string trace_name) { (ThreadTracer) -> untyped } -> untyped
11
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datadog_thread_tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sue445
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ddtrace
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop_auto_corrector
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-performance
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ddtrace helper to collect traces in thread
126
+ email:
127
+ - sue445@sue445.net
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".env.example"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".yardopts"
136
+ - CHANGELOG.md
137
+ - Dockerfile
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - datadog_thread_tracer.gemspec
143
+ - docker-compose.yml
144
+ - lib/datadog_thread_tracer.rb
145
+ - lib/datadog_thread_tracer/thread_tracer.rb
146
+ - lib/datadog_thread_tracer/version.rb
147
+ - sig/datadog_thread_tracer.rbs
148
+ - sig/datadog_thread_tracer/thread_tracer.rbs
149
+ homepage: https://github.com/sue445/datadog_thread_tracer
150
+ licenses:
151
+ - MIT
152
+ metadata:
153
+ homepage_uri: https://github.com/sue445/datadog_thread_tracer
154
+ source_code_uri: https://github.com/sue445/datadog_thread_tracer
155
+ changelog_uri: https://github.com/sue445/datadog_thread_tracer/blob/main/CHANGELOG.md
156
+ rubygems_mfa_required: 'true'
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: 2.6.0
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubygems_version: 3.3.7
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: ddtrace helper to collect traces in thread
176
+ test_files: []