trace_id 1.0.0.pre.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +35 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +5 -0
- data/Gemfile +6 -0
- data/LICENSE.md +33 -0
- data/README.md +123 -0
- data/Rakefile +2 -0
- data/bin/ci +10 -0
- data/bin/console +14 -0
- data/bin/rspec +29 -0
- data/bin/setup +6 -0
- data/lib/trace_id/from_request.rb +8 -0
- data/lib/trace_id/method.rb +7 -0
- data/lib/trace_id/sidekiq_client_middleware.rb +9 -0
- data/lib/trace_id/sidekiq_server_middleware.rb +8 -0
- data/lib/trace_id/version.rb +3 -0
- data/lib/trace_id.rb +53 -0
- data/trace_id.gemspec +27 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 98f2f2e82647dc65ab7d1684940a9bccde4b9885dfb007f2ac9803c472cf0aad
|
4
|
+
data.tar.gz: 84c25864287f9ae03df046693106a2222478f11229f21f60cada79153717d7d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b09bbe3e0965dc209f221ff08a090e5c95c7e8fa9c2764fe4aac4515eafc57bd3329a7ffe5c49aeebbf8272f96936d1bb822d370d8ea11a2cd24630fcab0c64e
|
7
|
+
data.tar.gz: 34033232e7866f3ca81f0eec57bae411bf2b2af03eeef82645b8a9a3334eca3a626a97eb90b6f88441bb08490196e63186fae5978ddf36de48e9e890ac30ef67
|
@@ -0,0 +1,35 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
# See https://circleci.com/developer/orbs/orb/circleci/ruby
|
4
|
+
ruby: circleci/ruby@1.2.0
|
5
|
+
jobs: # keyword
|
6
|
+
test: # my name for the job
|
7
|
+
parameters: # keyword
|
8
|
+
ruby-version: # my parameter name
|
9
|
+
type: string # type is a keyword
|
10
|
+
docker: # keyword
|
11
|
+
- image: cimg/base:stable
|
12
|
+
steps: # keyword
|
13
|
+
- checkout # magic name
|
14
|
+
- ruby/install: # ruby/ is from the orb name, install is a command in that orb
|
15
|
+
version: << parameters.ruby-version >> # magic nonsense for param subst
|
16
|
+
- run:
|
17
|
+
command: "bin/setup"
|
18
|
+
- run:
|
19
|
+
name: "Create the test results directory because you can't just store_test_results with a file and if you do you do not get any sort of error because wtf is with this platform?"
|
20
|
+
command: mkdir -p /tmp/test-results
|
21
|
+
- run:
|
22
|
+
command: bin/ci /tmp/test-results/rspec_results.xml
|
23
|
+
- store_test_results: # store_test_results is magic from circle
|
24
|
+
path: /tmp/test-results # path is a param to store_test_results and it must be a directory not a file
|
25
|
+
- store_artifacts: # store_artifacts is magic from circle
|
26
|
+
path: /tmp/test-results # path is the param to store_artifacts
|
27
|
+
workflows: # keyword
|
28
|
+
all-rubies: # my name for the workflow
|
29
|
+
jobs: # keyword
|
30
|
+
- test: # my name for the job
|
31
|
+
matrix: # keyword
|
32
|
+
parameters: # keyword
|
33
|
+
# All rubies being maintained per this page:
|
34
|
+
# https://www.ruby-lang.org/en/downloads/branches/
|
35
|
+
ruby-version: [ "2.5", "2.6", "2.7", "3.0" ]
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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, sex characteristics, gender identity and expression,
|
9
|
+
level of experience, education, socio-economic status, nationality, personal
|
10
|
+
appearance, race, religion, or sexual identity and 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 davetron5000 (at) 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 https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
72
|
+
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
74
|
+
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
* Code changes for style, cleanliness, eleganance, or other aesthetic stuff will not be accepted.
|
4
|
+
* All proposed changes must have a clear problem statement and be based on a real-world scenario. Imagined problems will not be solved.
|
5
|
+
* Tests are appreciated with all pull requests.
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
[trace_id] Copyright (2021) (David Copeland)(“Licensor”)
|
2
|
+
|
3
|
+
Hippocratic License Version Number: 2.1.
|
4
|
+
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
|
6
|
+
|
7
|
+
Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
|
8
|
+
|
9
|
+
* Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
|
10
|
+
|
11
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
|
12
|
+
|
13
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
14
|
+
|
15
|
+
1. Human Rights Principles.
|
16
|
+
|
17
|
+
(a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
|
18
|
+
|
19
|
+
(b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
|
20
|
+
|
21
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
|
22
|
+
|
23
|
+
2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
|
24
|
+
|
25
|
+
3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
|
26
|
+
|
27
|
+
* Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
|
28
|
+
|
29
|
+
* Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
|
30
|
+
|
31
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
32
|
+
|
33
|
+
This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# trace\_id - Small class and tools for distributed tracing in your Rails app
|
2
|
+
|
3
|
+
[![<sustainable-rails>](https://circleci.com/gh/sustainable-rails/trace_id.svg?style=shield)](https://app.circleci.com/pipelines/github/sustainable-rails/trace_id)
|
4
|
+
|
5
|
+
Your web server or load balancer probably gives you a request ID for each web request. It would be nice to make that available everywhere in your
|
6
|
+
Rails app for logging. It would be nice to pass that to background jobs to you can trace a request through a background job. It would be nice to
|
7
|
+
not have to write that yourself.
|
8
|
+
|
9
|
+
That is what this gem does.
|
10
|
+
|
11
|
+
It is a thin wrapper around thread local storage along with some helper classes to hook it into various parts of your Rails app that will allow a
|
12
|
+
request id from your web server to pass through background jobs and show up in your log.
|
13
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
In your `Gemfile`
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem "trace_id"
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
|
25
|
+
This gem revolves around the `TraceId` class, which has four methods. The two you are likely to want are:
|
26
|
+
|
27
|
+
* `TraceId.get_or_initialize` - returns the current Trace Id for the current thread or generates a new one and stores that for the future
|
28
|
+
* `TraceId.reset!` - changes or sets the value to a new generated one. This is useful in a rake task or background job not initiated by a web
|
29
|
+
request.
|
30
|
+
|
31
|
+
The other two methods, for completeness:
|
32
|
+
|
33
|
+
* `TraceId.get` - returns the current Trace Id or `nil` if there isn't one
|
34
|
+
* `TraceId.set` - sets the value. This is useful in a controller.
|
35
|
+
|
36
|
+
## Hooking Up To Your Rails App
|
37
|
+
|
38
|
+
There are four classes you can use to make it easier to integrate this into your Rails app.
|
39
|
+
|
40
|
+
### Setting the Trace Id to the value of your controller request id
|
41
|
+
|
42
|
+
In, e.g. `ApplicationController`:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# app/controllers/application_controller.rb
|
46
|
+
class ApplicationController < ActionController::Base
|
47
|
+
|
48
|
+
before_action TraceId::FromRequest
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
This will call `request.request_id` and put that value into `TraceId`. This should be a relatively standard way to get the incoming request ID
|
53
|
+
generated by your infrastructure.
|
54
|
+
|
55
|
+
### Persisting Across Sidekiq Jobs
|
56
|
+
|
57
|
+
In e.g. `config/initializers/sidekiq.rb`:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Sidekiq.configure_server do |config|
|
61
|
+
config.server_middleware do |chain|
|
62
|
+
chain.add TraceId::SidekiqServerMiddleware
|
63
|
+
end
|
64
|
+
|
65
|
+
config.client_middleware do |chain|
|
66
|
+
chain.add TraceId::SidekiqClientMiddleware
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
Sidekiq.configure_client do |config|
|
71
|
+
config.client_middleware do |chain|
|
72
|
+
chain.add TraceId::SidekiqClientMiddleware
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
This will ensure that any code that queues a job will include the current Trace Id in that job's payload. When that job is processed, it's
|
78
|
+
thread's Trace Id will be set. This will allow you to see code executing in background jobs related to the web request that iniltiated those jobs.
|
79
|
+
|
80
|
+
### Accessing Trace Id in normal code
|
81
|
+
|
82
|
+
While you can certainly call `TraceId.get_or_initialize` wherever you want, if you have a service layer, for example, you may want a more expedient
|
83
|
+
means of doing this that you can place in your base class:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# app/services/base_service.rb
|
87
|
+
class BaseService
|
88
|
+
include TraceId::Method
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
Now, the method `trace_id` will call `TraceId.get_or_initialize` for you.
|
93
|
+
|
94
|
+
### Logging the Trace Id
|
95
|
+
|
96
|
+
The Trace Id isn't useful unless you log it. Rails logging infrastructure is not very sophisticated, so it's hard to ensure it's included in every
|
97
|
+
call to `Rails.logger.info`. If you have set up `TraceId::FromRequest`, then the request ID that is already logged from your controllers will be
|
98
|
+
the trace id, so that's good, but for all other code, you need more.
|
99
|
+
|
100
|
+
One option is to override the Rails logger's formatting to pull it in, but you could also adopt the use of the [https://github.com/sustainable-rails/log_method](log_method) gem, which allows you do the following:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
# config/initializers/log_method.rb
|
104
|
+
|
105
|
+
LogMethod.config do |c|
|
106
|
+
c.trace_id_proc = ->() { TraceId.get }
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
Then, when you call `log`, the trace id will be included.
|
111
|
+
|
112
|
+
### Contributing
|
113
|
+
|
114
|
+
Happy to look at an accept:
|
115
|
+
|
116
|
+
* Bugfixes
|
117
|
+
* Adapters to put TraceId into more systems that need it
|
118
|
+
* New features if there is a clear problem statement that aligns with this gem
|
119
|
+
|
120
|
+
Not looking for:
|
121
|
+
|
122
|
+
* Changes related to coding style or formatting
|
123
|
+
|
data/Rakefile
ADDED
data/bin/ci
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "with_clues"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
data/lib/trace_id.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "securerandom"
|
2
|
+
|
3
|
+
require_relative "trace_id/version"
|
4
|
+
require_relative "trace_id/from_request"
|
5
|
+
require_relative "trace_id/sidekiq_server_middleware"
|
6
|
+
require_relative "trace_id/sidekiq_client_middleware"
|
7
|
+
require_relative "trace_id/method"
|
8
|
+
|
9
|
+
class TraceId
|
10
|
+
TRACE_ID = "__trace_id"
|
11
|
+
|
12
|
+
# Get the current trace id, or nil if one has not been set.
|
13
|
+
# You are more likely to want get_or_initialize, which will initialize one for the rest
|
14
|
+
# of the thread if one hasn't been set.
|
15
|
+
def self.get
|
16
|
+
Thread.current.thread_variable_get(TRACE_ID)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Resets the current traced. The block, if given, is
|
20
|
+
# called with the old trace id if you want to log it before it
|
21
|
+
# becomes irrelevant.
|
22
|
+
def self.reset!(&block)
|
23
|
+
original = self.get
|
24
|
+
self.set(self.new_trace_id_value)
|
25
|
+
if !block.nil?
|
26
|
+
block.(original)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get the current trace id, initializing it if there isn't one set.
|
31
|
+
def self.get_or_initialize
|
32
|
+
trace_id = self.get
|
33
|
+
if trace_id.to_s.strip.length == 0
|
34
|
+
trace_id = self.new_trace_id_value
|
35
|
+
self.set(trace_id)
|
36
|
+
end
|
37
|
+
self.get
|
38
|
+
end
|
39
|
+
|
40
|
+
# Set the new trace id. Note, you might want reset! if you simply want
|
41
|
+
# a new value. This method is useful only when you have externalized a trace id
|
42
|
+
# you wish to restore, such us in a sidekiq job
|
43
|
+
def self.set(new_trace_id)
|
44
|
+
Thread.current.thread_variable_set(TRACE_ID, new_trace_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def self.new_trace_id_value
|
50
|
+
SecureRandom.uuid
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/trace_id.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "lib/trace_id/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "trace_id"
|
5
|
+
spec.version = TraceId::VERSION
|
6
|
+
spec.authors = ["Dave Copeland"]
|
7
|
+
spec.email = ["davec@naildrivin5.com"]
|
8
|
+
spec.summary = %q{Small class and helpers to manage a request id from web to service calls to background jobs}
|
9
|
+
spec.homepage = "https://github.com/sustainable-rails/trace_id"
|
10
|
+
spec.license = "Hippocratic"
|
11
|
+
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/sustainable-rails/trace_id"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/sustainable-rails/trace_id/releases"
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency("rspec")
|
26
|
+
spec.add_development_dependency("rspec_junit_formatter")
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trace_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre.beta1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Copeland
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec_junit_formatter
|
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
|
+
description:
|
42
|
+
email:
|
43
|
+
- davec@naildrivin5.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".circleci/config.yml"
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".ruby-version"
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- CONTRIBUTING.md
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.md
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/ci
|
59
|
+
- bin/console
|
60
|
+
- bin/rspec
|
61
|
+
- bin/setup
|
62
|
+
- lib/trace_id.rb
|
63
|
+
- lib/trace_id/from_request.rb
|
64
|
+
- lib/trace_id/method.rb
|
65
|
+
- lib/trace_id/sidekiq_client_middleware.rb
|
66
|
+
- lib/trace_id/sidekiq_server_middleware.rb
|
67
|
+
- lib/trace_id/version.rb
|
68
|
+
- trace_id.gemspec
|
69
|
+
homepage: https://github.com/sustainable-rails/trace_id
|
70
|
+
licenses:
|
71
|
+
- Hippocratic
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/sustainable-rails/trace_id
|
74
|
+
source_code_uri: https://github.com/sustainable-rails/trace_id
|
75
|
+
changelog_uri: https://github.com/sustainable-rails/trace_id/releases
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 2.5.0
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.1
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.2.29
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Small class and helpers to manage a request id from web to service calls
|
95
|
+
to background jobs
|
96
|
+
test_files: []
|