newrelic-telemetry_sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ # This file is distributed under New Relic's license terms.
4
+ # See https://github.com/newrelic/newrelic-telemetry-sdk-ruby/blob/main/LICENSE for complete details.
5
+
6
+ module NewRelic
7
+ module TelemetrySdk
8
+ # This class represents a timed operation that is part of a distributed trace.
9
+ # This operation will be represented as a Span in the New Relic UI.
10
+ #
11
+ # @api public
12
+ class Span
13
+ include NewRelic::TelemetrySdk::Logger
14
+
15
+ attr_accessor :id,
16
+ :trace_id,
17
+ :start_time,
18
+ :duration_ms,
19
+ :name,
20
+ :parent_id,
21
+ :service_name,
22
+ :custom_attributes
23
+
24
+ # @param id [optional, String]
25
+ # A random, unique identifier associated with this specific New Relic span.
26
+ # @param trace_id [optional, String]
27
+ # A random, unique identifier associated with a collection of spans that
28
+ # will be grouped together as a trace in the New Relic UI.
29
+ # @param start_time [optional, Time]
30
+ # A Time object corresponding to the start time of the operation represented
31
+ # by this span.
32
+ # @param duration_ms [optional, Integer]
33
+ # The duration of the operation represented by this span, in milliseconds.
34
+ # @param name [optional, String]
35
+ # The name of the span.
36
+ # @param parent_id [optional, String]
37
+ # A random, unique identifier associated with the parent of this span.
38
+ # @param service_name [optional, String]
39
+ # The name of the entity that created this span.
40
+ # @param custom_attributes [optional, Hash]
41
+ # Custom attributes that will appear on this span.
42
+ # @see https://docs.newrelic.com/docs/understand-dependencies/distributed-tracing/trace-api/report-new-relic-format-traces-trace-api#other-attributes Report Traces (New Relic format)
43
+ #
44
+ # @api public
45
+ def initialize id: Util.generate_guid(16),
46
+ trace_id: Util.generate_guid(32),
47
+ start_time: Util.current_time,
48
+ duration_ms: nil,
49
+ name: nil,
50
+ parent_id: nil,
51
+ service_name: nil,
52
+ custom_attributes: nil
53
+
54
+ @id = id
55
+ @trace_id = trace_id
56
+ @start_time = start_time
57
+ @duration_ms = duration_ms
58
+ @name = name
59
+ @parent_id = parent_id
60
+ @service_name = service_name
61
+ @custom_attributes = custom_attributes
62
+ end
63
+
64
+ # Mark the operation represented by this Span as finished and calculate is duration.
65
+ # @param end_time [optional, Time]
66
+ # A Time object corresponding to the end time of the operation represented
67
+ # by this span.
68
+ #
69
+ # @api public
70
+ def finish end_time: Util.current_time
71
+ @duration_ms = Util.time_to_ms(end_time - @start_time)
72
+ rescue => e
73
+ log_error "Encountered error finishing span", e
74
+ end
75
+
76
+ def to_h
77
+ data = {
78
+ :id => @id,
79
+ :'trace.id' => @trace_id,
80
+ :timestamp => Util.time_to_ms(@start_time),
81
+ :attributes => {
82
+ :'duration.ms' => @duration_ms,
83
+ :name => @name,
84
+ :'parent.id' => @parent_id,
85
+ :'service.name' => @service_name
86
+ }
87
+ }
88
+
89
+ data[:attributes].merge! @custom_attributes if @custom_attributes
90
+
91
+ data
92
+ rescue => e
93
+ log_error "Encountered error converting span to hash", e
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ # This file is distributed under New Relic's license terms.
4
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
5
+
6
+ module NewRelic
7
+ module TelemetrySdk
8
+ module Util
9
+ extend self
10
+
11
+ HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
12
+
13
+ module_function
14
+
15
+ # This method intentionally does not use SecureRandom, because it relies
16
+ # on urandom, which raises an exception in MRI when the interpreter runs
17
+ # out of allocated file descriptors.
18
+ # The guids generated by this method may not be _secure_, but they are
19
+ # random enough for our purposes.
20
+ def generate_guid length = 16
21
+ guid = String.new
22
+ length.times do |a|
23
+ guid << HEX_DIGITS[rand(16)]
24
+ end
25
+ guid
26
+ end
27
+
28
+ def current_time
29
+ Time.now
30
+ end
31
+
32
+ def time_to_ms time = current_time
33
+ (time.to_f * 1000).round
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+ # This file is distributed under New Relic's license terms.
4
+ # See https://github.com/newrelic/newrelic-telemetry-sdk-ruby/blob/main/LICENSE for complete details.
5
+
6
+ module NewRelic
7
+ module TelemetrySdk
8
+ VERSION = "0.1.0"
9
+
10
+ extend self
11
+
12
+ def gem_version
13
+ Gem::Version.create VERSION
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "newrelic/telemetry_sdk/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "newrelic-telemetry_sdk"
7
+ spec.version = NewRelic::TelemetrySdk::VERSION
8
+ spec.authors = ["Rachel Klein", "Tanna McClure", "Michael Lang"]
9
+ spec.email = ["support@newrelic.com"]
10
+
11
+ spec.summary = %q{New Relic Telemetry SDK}
12
+ spec.description = <<-EOS
13
+ Send your telemetry data to New Relic, no agent required.
14
+ EOS
15
+ spec.homepage = "https://newrelic.com/ruby"
16
+ spec.licenses = ['Apache-2.0']
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/newrelic/newrelic-telemetry-sdk-ruby/"
20
+ spec.metadata["changelog_uri"] = "https://github.com/newrelic/newrelic-telemetry-sdk-ruby/blob/main/CHANGELOG.md"
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('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.executables = []
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", ">= 12.3.3"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
33
+ spec.add_development_dependency "mocha", "~> 1.11"
34
+ spec.add_development_dependency "pry-nav", "~> 0.3.0"
35
+ spec.add_development_dependency "timecop", "~> 0.9"
36
+ spec.add_development_dependency "simplecov", "~> 0.19.0"
37
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newrelic-telemetry_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rachel Klein
8
+ - Tanna McClure
9
+ - Michael Lang
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2020-10-30 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 12.3.3
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 12.3.3
43
+ - !ruby/object:Gem::Dependency
44
+ name: minitest
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '5.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '5.0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: mocha
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.11'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '1.11'
71
+ - !ruby/object:Gem::Dependency
72
+ name: pry-nav
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: 0.3.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: 0.3.0
85
+ - !ruby/object:Gem::Dependency
86
+ name: timecop
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '0.9'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '0.9'
99
+ - !ruby/object:Gem::Dependency
100
+ name: simplecov
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: 0.19.0
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: 0.19.0
113
+ description: 'Send your telemetry data to New Relic, no agent required.
114
+
115
+ '
116
+ email:
117
+ - support@newrelic.com
118
+ executables: []
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
123
+ - ".github/ISSUE_TEMPLATE/enhancement.md"
124
+ - ".github/workflows/assignproj.yml"
125
+ - ".github/workflows/ci.yml"
126
+ - ".github/workflows/release.yml"
127
+ - ".github/workflows/scripts/rubygems-authenticate.py"
128
+ - ".github/workflows/scripts/rubygems-publish.rb"
129
+ - ".github/workflows/snyk.yml"
130
+ - ".gitignore"
131
+ - ".yardopts"
132
+ - CHANGELOG.md
133
+ - CODE_OF_CONDUCT.md
134
+ - CONTRIBUTING.md
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - cla.md
140
+ - examples/external_request/Gemfile
141
+ - examples/external_request/simple.rb
142
+ - examples/external_request/with_harvester.rb
143
+ - lib/newrelic/telemetry_sdk.rb
144
+ - lib/newrelic/telemetry_sdk/buffer.rb
145
+ - lib/newrelic/telemetry_sdk/clients/client.rb
146
+ - lib/newrelic/telemetry_sdk/clients/trace_client.rb
147
+ - lib/newrelic/telemetry_sdk/config.rb
148
+ - lib/newrelic/telemetry_sdk/configurator.rb
149
+ - lib/newrelic/telemetry_sdk/exception.rb
150
+ - lib/newrelic/telemetry_sdk/harvester.rb
151
+ - lib/newrelic/telemetry_sdk/logger.rb
152
+ - lib/newrelic/telemetry_sdk/span.rb
153
+ - lib/newrelic/telemetry_sdk/util.rb
154
+ - lib/newrelic/telemetry_sdk/version.rb
155
+ - newrelic-telemetry_sdk.gemspec
156
+ homepage: https://newrelic.com/ruby
157
+ licenses:
158
+ - Apache-2.0
159
+ metadata:
160
+ homepage_uri: https://newrelic.com/ruby
161
+ source_code_uri: https://github.com/newrelic/newrelic-telemetry-sdk-ruby/
162
+ changelog_uri: https://github.com/newrelic/newrelic-telemetry-sdk-ruby/blob/main/CHANGELOG.md
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubygems_version: 3.1.4
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: New Relic Telemetry SDK
182
+ test_files: []