pyroscope-otel 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: 4c24fd4fe4eaa5a86fdbd3ff36fbb2978bc59373887d549a2adc067e9593ec67
4
+ data.tar.gz: c743055c7ff448ba3394ca17c30a6e8be888c942cc19dcf3cf971569ce5bbc70
5
+ SHA512:
6
+ metadata.gz: c953e4562c093bfa84246b4014b9c4d060a6c23bafe7507087241334eb68c6656643d1a6567053fc1690ff04c90c4e4536c7429ad5dbc1673fb97a0a91f473f3
7
+ data.tar.gz: d1f0f006af18bf0461e74907cce9bc8bbb6d8e105786a8a10e66c3e9c9992fad6ef6516d88704420876ebec8cee6c901193b90c3ff3e1689be6c9f04bc744f54
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,28 @@
1
+ name: Publish Gem
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - "*"
7
+ tags:
8
+ - v*
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v1
16
+
17
+ - name: Configure git
18
+ run: |
19
+ git config user.name "Pyroscope Bot"
20
+ git config user.email "dmitry+bot@pyroscope.io"
21
+
22
+ - name: Release Gem
23
+ uses: cadwallion/publish-rubygems-action@master
24
+ if: contains(github.ref, 'refs/tags/v')
25
+ env:
26
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
27
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
28
+ RELEASE_COMMAND: rake build release:source_control_push release:rubygem_push
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ .idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pyroscope-otel.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 0.80"
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pyroscope-otel (0.1.0)
5
+ opentelemetry-api (~> 1.1.0)
6
+ pyroscope (~> 0.5.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
13
+ ffi (1.15.5)
14
+ opentelemetry-api (1.1.0)
15
+ parallel (1.22.1)
16
+ parser (3.1.2.1)
17
+ ast (~> 2.4.1)
18
+ pyroscope (0.5.1-x86_64-linux)
19
+ ffi
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.5.0)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-mocks (3.11.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-support (3.11.1)
37
+ rubocop (0.93.1)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.7.1.5)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8)
42
+ rexml
43
+ rubocop-ast (>= 0.6.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 2.0)
46
+ rubocop-ast (1.21.0)
47
+ parser (>= 3.1.1.0)
48
+ ruby-progressbar (1.11.0)
49
+ unicode-display_width (1.8.0)
50
+
51
+ PLATFORMS
52
+ x86_64-linux
53
+
54
+ DEPENDENCIES
55
+ pyroscope-otel!
56
+ rake (~> 13.0)
57
+ rspec (~> 3.0)
58
+ rubocop (~> 0.80)
59
+
60
+ BUNDLED WITH
61
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Tolya Korniltsev
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,40 @@
1
+ # Pyroscope::Otel
2
+
3
+ Pyroscope can integrate with distributed tracing systems supporting OpenTelemetry standard which allows you to link traces with the profiling data, and find specific lines of code related to a performance issue.
4
+
5
+ For full documentation visit https://pyroscope.io/docs/ruby-tracing/
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'pyroscope-otel'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install pyroscope-otel
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ Pyroscope.configure do |config|
26
+ # Configure pyroscope as described https://pyroscope.io/docs/ruby/
27
+ end
28
+
29
+ OpenTelemetry::SDK.configure do |config|
30
+ config.add_span_processor Pyroscope::Otel::SpanProcessor.new(
31
+ "#{app_name}.cpu", # your app name with ".cpu" suffix, for example rideshare-ruby.cpu
32
+ pyroscope_endpoint # link to your pyroscope server, for example "http://localhost:4040"
33
+ )
34
+ # Configure the rest of opentelemetry as described https://github.com/open-telemetry/opentelemetry-ruby
35
+ end
36
+ ```
37
+
38
+ ## License
39
+
40
+ 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]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "pyroscope/otel"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pyroscope
4
+ module Otel
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pyroscope"
4
+ # require_relative "otel/version"
5
+ require "uri"
6
+
7
+ module Pyroscope
8
+ module Otel
9
+ class Error < StandardError; end
10
+
11
+ # SpanProcessor annotates otel spans with profile_id, profile urls,
12
+ # baseline urls
13
+ class SpanProcessor
14
+ ZERO_SPAN_ID = [0, 0, 0, 0, 0, 0, 0, 0].pack("C*")
15
+ # pyroscope app name, including ".cpu" suffix.
16
+ attr_accessor :app_name
17
+ # http address of pyroscope server for span links
18
+ attr_accessor :pyroscope_endpoint
19
+
20
+ # boolean flag option to annotate spans with profile attributes only on root spans.
21
+ attr_accessor :root_span_only
22
+ # boolean flag option to annotate pyroscope profiles with span name
23
+ attr_accessor :add_span_name
24
+ # boolean flag option to add profiler url to span attributes
25
+ attr_accessor :add_url
26
+
27
+ # @param [String] app_name - pyroscope app name, including ".cpu" suffix.
28
+ # @param [String] pyroscope_endpoint - http address of pyroscope server for span links.
29
+ def initialize(app_name,
30
+ pyroscope_endpoint)
31
+ @app_name = app_name
32
+ @pyroscope_endpoint = URI.parse(pyroscope_endpoint)
33
+ @root_span_only = true
34
+ @add_span_name = true
35
+ @add_url = true
36
+ end
37
+
38
+ def on_start(span, parent_context)
39
+ return if @root_span_only && !root_span?(span, parent_context)
40
+
41
+ profile_id = profile_id(span)
42
+
43
+ labels = { "profile_id": profile_id }
44
+ labels["span"] = span.name if @add_span_name
45
+
46
+ Pyroscope._add_tags(Pyroscope.thread_id, labels)
47
+
48
+ annotate_span(profile_id, span)
49
+ rescue StandardError => e
50
+ OpenTelemetry.handle_error(exception: e, message: "unexpected error in span.on_start")
51
+ end
52
+
53
+ def on_finish(span)
54
+ profile_id = span.attributes["pyroscope.profile.id"]
55
+ return if profile_id.nil?
56
+
57
+ labels = { "profile_id": profile_id }
58
+ labels["span"] = span.name if @add_span_name
59
+ Pyroscope._remove_tags(Pyroscope.thread_id, labels)
60
+ end
61
+
62
+ def force_flush(_timeout: nil) end
63
+
64
+ def shutdown(_timeout: nil) end
65
+
66
+ private
67
+
68
+ def root_span?(parent, parent_context)
69
+ return true if parent.parent_span_id == ZERO_SPAN_ID
70
+
71
+ parent = OpenTelemetry::Trace.current_span(parent_context)
72
+ return false if parent.nil?
73
+
74
+ parent.context.remote?
75
+ rescue StandardError => _e
76
+ false
77
+ end
78
+
79
+ def annotate_span(profile_id, span)
80
+ span.set_attribute("pyroscope.profile.id", profile_id)
81
+ span.set_attribute("pyroscope.profile.url", profile_url(profile_id)) if @add_url
82
+ end
83
+
84
+ def profile_id(span)
85
+ span.context.span_id.unpack("H*")
86
+ end
87
+
88
+ def profile_url(profile_id)
89
+ url = @pyroscope_endpoint.clone
90
+ from = Time.now.to_i
91
+ to = from + 60 * 60
92
+ url.query = URI.encode_www_form({
93
+ "query": query(profile_id),
94
+ "from": from,
95
+ "until": to
96
+ })
97
+ url.to_s
98
+ end
99
+
100
+ def query(profile_id)
101
+ "#{app_name}{profile_id=\"#{profile_id}\"}"
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pyroscope/otel/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pyroscope-otel"
7
+ spec.version = Pyroscope::Otel::VERSION
8
+ spec.authors = ["Tolyan Korniltsev"]
9
+ spec.email = ["anatoly@pyroscope.io"]
10
+
11
+ spec.summary = "Pyroscope OTEL integration"
12
+ spec.description = "Pyroscope OTEL integration"
13
+ spec.homepage = "https://pyroscope.io/"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/pyroscope-io/otel-profiling-ruby"
21
+ spec.metadata["changelog_uri"] = "https://github.com/pyroscope-io/otel-profiling-ruby/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "opentelemetry-api", "~> 1.1.0"
33
+ spec.add_dependency "pyroscope", "~> 0.5.1"
34
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pyroscope-otel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tolyan Korniltsev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opentelemetry-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.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.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pyroscope
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.1
41
+ description: Pyroscope OTEL integration
42
+ email:
43
+ - anatoly@pyroscope.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".github/workflows/release.yml"
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - lib/pyroscope/otel.rb
61
+ - lib/pyroscope/otel/version.rb
62
+ - pyroscope-otel.gemspec
63
+ homepage: https://pyroscope.io/
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://pyroscope.io/
68
+ source_code_uri: https://github.com/pyroscope-io/otel-profiling-ruby
69
+ changelog_uri: https://github.com/pyroscope-io/otel-profiling-ruby/CHANGELOG.md
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.7
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Pyroscope OTEL integration
89
+ test_files: []