codecov_opentelem 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: 60048234598ab361913a778271de941824f809ffbcd10a99a196a633e85fca4c
4
+ data.tar.gz: 6929cb70f8ad00564757efdc05a5d8e848d1b35652e97e908f9f10f157d1855a
5
+ SHA512:
6
+ metadata.gz: 3dab6244c860ce0955fda8e3f21b26702a37223601395ab604e175057f3e3db0bb2272ad879da8cf824cde34ccf830e7f6bab35ec513ea9a9d33281ae7a0f5d8
7
+ data.tar.gz: d7e275ad9f8a9eb9d3b59b428078bd5682e72326e51d1a4a6aef7c9dfd954dceff66f7de705e103f36c296e33f1fdfaa4ea2309d2bb58ac35c1a0e4be9cef936
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+
2
+ ## [0.1.0] - 2022-05-13
3
+
4
+ - Initial release
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 codecov_opentelem.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem 'opentelemetry-sdk'
11
+ gem 'opentelemetry-exporter-otlp'
12
+ gem 'opentelemetry-instrumentation-all'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Codecov
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,27 @@
1
+ ## Installation
2
+
3
+ Install the gem and add to the application's Gemfile by executing:
4
+
5
+ $ bundle add codecov_opentelem
6
+
7
+ If bundler is not being used to manage dependencies, install the gem by executing:
8
+
9
+ $ gem install codecov_opentelem
10
+
11
+ ## Usage
12
+
13
+ TODO: Write usage instructions here
14
+
15
+ ## Development
16
+
17
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
18
+
19
+ 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).
20
+
21
+ ## Contributing
22
+
23
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codecov/opentelem-ruby.
24
+
25
+ ## License
26
+
27
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/codecov_opentelem/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "codecov_opentelem"
7
+ spec.version = CodecovOpentelem::VERSION
8
+ spec.authors = ["Ahmed-Elghrbawy"]
9
+ spec.email = ["hello@codecov.io"]
10
+
11
+ spec.summary = "Ruby Opentelemtry"
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/codecov/opentelem-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/codecov/opentelem-ruby."
21
+ spec.metadata["changelog_uri"] = "https://github.com/codecov/opentelem-ruby/blob/master/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(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'uri'
5
+ require 'net/http'
6
+
7
+ require 'opentelemetry/sdk'
8
+ require 'coverage'
9
+
10
+ module CoverageSpanFilter
11
+ REGEX_NAME = 'name_regex'
12
+ SPAN_KIND = 'span_kind'
13
+ end
14
+
15
+ class CodecovCoverageStorageManager
16
+ def initialize(filters)
17
+ @filters = filters
18
+ @inner = {}
19
+ end
20
+
21
+ def start_coverage_for_span(span)
22
+ if !@filters[CoverageSpanFilter::REGEX_NAME].nil? && @filters[CoverageSpanFilter::REGEX_NAME].match(span.name)
23
+ return false
24
+ end
25
+
26
+ Coverage.resume
27
+ true
28
+ end
29
+
30
+ def stop_coverage_for_span(_span)
31
+ span_id = span.context.span_id
32
+ Coverage.suspend
33
+ @inner[span_id] = Coverage.result(stop: false, clear: true)
34
+ end
35
+
36
+ def pop_coverage_for_span(_span)
37
+ span_id = span.context.span_id
38
+ @inner.delete(span_id)
39
+ end
40
+ end
41
+
42
+ class CodecovCoverageGenerator < SpanProcessor
43
+ def initialize(cov_storage, sample_rate)
44
+ super()
45
+ @cov_storage = cov_storage
46
+ @sample_rate = sample_rate
47
+ end
48
+
49
+ def on_start(span, _parent_context = nil)
50
+ @cov_storage.possibly_start_cov_for_span(span) if rand < @sample_rate
51
+ end
52
+
53
+ def on_end(span)
54
+ @cov_storage.stop_coverage_for_span(span)
55
+ end
56
+ end
57
+
58
+ class CoverageExporter < SpanExporter
59
+ def initialize(cov_storage, repository_token, code, codecov_url, untracked_export_rate)
60
+ super()
61
+ @cov_storage = cov_storage
62
+ @repository_token = repository_token
63
+ @code = code
64
+ @codecov_url = codecov_url
65
+ @untracked_export_rate = untracked_export_rate
66
+ end
67
+
68
+ def export(spans)
69
+ tracked_spans = []
70
+ untracked_spans = []
71
+ spans.each do |span|
72
+ cov = cov_storage.pop_cov_for_span(span)
73
+ s = JSON.parse(span)
74
+ if !cov.nil?
75
+ s['codecov'] = { 'type' => 'bytes', 'coverage' => cov }
76
+ tracked_spans.append(s)
77
+ elsif rand < @untracked_export_rate
78
+ untracked_spans.append(s)
79
+ end
80
+ end
81
+ return SpanExportResult.SUCCESS if !tracked_spans && !untracked_spans
82
+
83
+ res = Net::HTTP.post(URI.join(@codecov_url, '/profiling/uploads'),
84
+ { 'code' => @code }.to_json,
85
+ 'Content-Type' => 'application/json',
86
+ 'Authorization' => "repotoken #{@repository_token}")
87
+ return SpanExportResult.FAILURE if res.is_a?(Net::HTTPError)
88
+
89
+ location = res.body.raw_upload_location
90
+ Net::HTTP.put(URI(location),
91
+ { 'spans' => tracked_spans,
92
+ 'untracked' => untracked_spans }.to_json,
93
+ 'Content-Type' => 'text/plain')
94
+ SpanExportResult.SUCCESS
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodecovOpentelem
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "codecov_opentelem/version"
4
+ require 'codecov_opentelem/coverage_exporter'
5
+
6
+
7
+ # Entrypoint for getting a span processor/span exporter pair to send profiling data to codecov
8
+ # @param [String] repository_token The profiling authentication token
9
+ # @param [Numeric] sample_rate The sampling rate for codecov, a number from 0 to 1
10
+ # @param [Numeric] untracked_export_rate The export rate for codecov for non-sampled spans, a number from 0 to 1
11
+ # @param [optional Hash] filters A hash of filters for determining which spans should have its coverage tracked
12
+ # @param [optional String] version_identifier The identifier for what software version is being profiled
13
+ # @param [optional String] environment The environment name this profiling is running on
14
+ # @param [optional Boolean] needs_version_creation Whether the "create this version" needs to be called (one can choose
15
+ # to call it manually beforehand and disable it here)
16
+ # @param [optional String] codecov_url For configuring the endpoint in case the user is in enterprise (not
17
+ # supported yet). Default is "https://api.codecov.io/"
18
+ # @return [Array<CodecovCoverageGenerator,CoverageExporter>]
19
+ def get_codecov_opentelemetry_instances(
20
+ repository_token:,
21
+ sample_rate:,
22
+ untracked_export_rate:,
23
+ code:,
24
+ filters: {},
25
+ version_identifier: nil,
26
+ environment: nil,
27
+ needs_version_creation: true,
28
+ codecov_url: nil
29
+ )
30
+ codecov_url = 'https://api.codecov.io' if codecov_url.nil?
31
+ raise UnableToStartProcessorException, 'Codecov profiling needs a code set' if code.nil?
32
+
33
+ if needs_version_creation && version_identifier && environment
34
+ res = Net::HTTP.post(URI.join(codecov_url, '/profiling/versions'), {
35
+ 'version_identifier' => version_identifier,
36
+ 'environment' => environment,
37
+ 'code' => code
38
+ }.to_json,
39
+ 'Content-Type' => 'application/json',
40
+ 'Authorization' => "repotoken #{repository_token}")
41
+ raise UnableToStartProcessorException if res.is_a?(Net::HTTPError)
42
+ end
43
+ manager = CodecovCoverageStorageManager(filters)
44
+ generator = CodecovCoverageGenerator(manager, sample_rate)
45
+ exporter = CoverageExporter(
46
+ manager, repository_token, code, codecov_url, untracked_export_rate
47
+ )
48
+ [generator, exporter]
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codecov_opentelem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ahmed-Elghrbawy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - hello@codecov.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - codecov_opentelem.gemspec
26
+ - lib/codecov_opentelem.rb
27
+ - lib/codecov_opentelem/coverage_exporter.rb
28
+ - lib/codecov_opentelem/version.rb
29
+ homepage: https://github.com/codecov/opentelem-ruby
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/codecov/opentelem-ruby
34
+ source_code_uri: https://github.com/codecov/opentelem-ruby.
35
+ changelog_uri: https://github.com/codecov/opentelem-ruby/blob/master/CHANGELOG.md
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.3.13
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Ruby Opentelemtry
55
+ test_files: []