uptrace 0.2.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e98c516863a2d2d3cf5befff7333a5347b9624ac4b4287177dffa93327fca89
4
- data.tar.gz: b1081b536729d1de1a50717b21053816faa4801d7f886db81c23f8a2ca98c2a5
3
+ metadata.gz: d437c19b7fc3feb724d4fa895fb7ef5e9cfc8501931a16f53d598fb9ef5afddc
4
+ data.tar.gz: 6340b2ba9b3991f922777310e43d7e256384f621b18c6598e422332b1940efce
5
5
  SHA512:
6
- metadata.gz: 7eb40d3d6563d1eec0f822a758ac4cbbccbb73dbcbe13e0ddfc992e9b9a39ed8746035fec3e85ab6610278584c3db873a37d9cd87ffbfbb0dd9152f7126c1da4
7
- data.tar.gz: 6eef8ebe76bc77bae7efe13a78a43329ceeb8ae3e1aa44d5e444ebc7d09290f17fa9d31c0b9a6fe12599dda1f33230bea23e718b0ae919ebf955d5e0ff9aaac0
6
+ metadata.gz: cdb698a1ee148f598fbf2ecf93ddc47867d797fd0f72a33bdc44547dfe5cec846984eaaba71e69c198ce3423c64bfe8ff830313b770980624a8cff164b0b431d
7
+ data.tar.gz: 95c417612dd655203b05d3323188536f453fa03ba4f412e84491dc23bdca90397f9ae3d99cdfd1899f06e23632282efa6f74251a9e93da8436d1255aa74431df
data/README.md CHANGED
@@ -1,21 +1,62 @@
1
1
  # Uptrace Ruby exporter for OpenTelemetry
2
2
 
3
- [![Build Status](https://travis-ci.org/uptrace/uptrace-ruby.svg?branch=master)](https://travis-ci.org/uptrace/uptrace-ruby)
3
+ ![build workflow](https://github.com/uptrace/uptrace-ruby/actions/workflows/build.yml/badge.svg)
4
4
  [![Documentation](https://img.shields.io/badge/uptrace-documentation-informational)](https://docs.uptrace.dev/ruby/)
5
5
 
6
6
  <a href="https://docs.uptrace.dev/ruby/">
7
7
  <img src="https://docs.uptrace.dev/devicons/ruby-original.svg" height="200px" />
8
8
  </a>
9
9
 
10
- ## Installation
10
+ ## Introduction
11
+
12
+ uptrace-ruby is an OpenTelemery distribution configured to export
13
+ [traces](https://docs.uptrace.dev/tracing/#spans) to Uptrace.
14
+
15
+ ## Quickstart
16
+
17
+ Install uptrace-ruby:
11
18
 
12
19
  ```bash
13
20
  gem install uptrace
14
21
  ```
15
22
 
16
- ## Introduction
23
+ Run the [basic example](example/basic) below using the DSN from the Uptrace project settings page.
24
+
25
+ ```ruby
26
+ #!/usr/bin/env ruby
27
+ # frozen_string_literal: true
28
+
29
+ require 'rubygems'
30
+ require 'bundler/setup'
31
+ require 'uptrace'
17
32
 
18
- uptrace-ruby is the official Uptrace client for Ruby that sends your traces/spans and metrics to
19
- [Uptrace.dev](https://uptrace.dev).
33
+ OpenTelemetry::SDK.configure do |c|
34
+ c.service_name = 'myservice'
35
+ c.service_version = '1.0.0'
36
+
37
+ # Configure OpenTelemetry to export data to Uptrace.
38
+ # Copy your project DSN here or use UPTRACE_DSN env var.
39
+ Uptrace.configure_opentelemetry(c, dsn: '')
40
+ end
41
+
42
+ tracer = OpenTelemetry.tracer_provider.tracer('my_app_or_gem', '0.1.0')
43
+
44
+ tracer.in_span('main') do |span|
45
+ tracer.in_span('child1') do |child1|
46
+ child1.set_attribute('key1', 'value1')
47
+ child1.record_exception(ArgumentError.new('error1'))
48
+ end
49
+
50
+ tracer.in_span('child2') do |child2|
51
+ child2.set_attribute('key2', '24')
52
+ child2.set_attribute('key3', 123.456)
53
+ end
54
+
55
+ puts("trace URL: #{Uptrace.trace_url(span)}")
56
+ end
57
+
58
+ # Send buffered spans.
59
+ OpenTelemetry.tracer_provider.shutdown
60
+ ```
20
61
 
21
- See [uptrace-ruby documentation](https://docs.uptrace.dev/ruby/) for details.
62
+ Please see [uptrace-ruby documentation](https://docs.uptrace.dev/ruby/) for more details.
data/RELEASE.md CHANGED
@@ -1,4 +1,6 @@
1
- ```
1
+ Bump version and then run:
2
+
3
+ ```shell
2
4
  gem build uptrace.gemspec
3
5
  bundle install
4
6
  gem push uptrace-0.2.0.gem
data/lib/uptrace.rb CHANGED
@@ -7,8 +7,26 @@ module Uptrace
7
7
  extend self
8
8
 
9
9
  attr_accessor :logger
10
+ attr_writer :client
10
11
 
11
12
  self.logger = Logger.new($stdout)
13
+
14
+ # @return [Object, Client] registered client or a default no-op implementation of the client.
15
+ def client
16
+ @client ||= Client.new
17
+ end
18
+
19
+ # @param [optional OpenTelemetry::Trace::Span] span
20
+ # @return [String]
21
+ def trace_url(span = nil)
22
+ client.trace_url(span)
23
+ end
24
+
25
+ def configure_opentelemetry(c, dsn: '')
26
+ @client = Client.new(dsn: dsn) unless dsn.empty?
27
+
28
+ c.add_span_processor(client.span_processor) unless client.disabled?
29
+ end
12
30
  end
13
31
 
14
32
  require 'uptrace/version'
@@ -5,58 +5,43 @@ require 'opentelemetry/sdk'
5
5
  module Uptrace
6
6
  # Uptrace client that configures OpenTelemetry SDK to use Uptrace exporter.
7
7
  class Client
8
- ##
9
- # @yieldparam config [Uptrace::Config]
10
- # @return [void]
11
- #
12
- def initialize
13
- @cfg = Uptrace::Trace::Config.new
14
- yield @cfg if block_given?
8
+ # @param [string] dsn
9
+ def initialize(dsn: '')
10
+ dsn = ENV.fetch('UPTRACE_DSN', '') if dsn.empty?
15
11
 
16
12
  begin
17
- @cfg.dsno
13
+ @dsn = DSN.new(dsn)
18
14
  rescue ArgumentError => e
19
- Uptrace.logger.error(e.message)
20
- @cfg.disabled = true
15
+ Uptrace.logger.error("Uptrace is disabled: #{e.message}")
16
+ @disabled = true
21
17
 
22
- @cfg.dsn = 'https://TOKEN@api.uptrace.dev/PROJECT_ID'
18
+ @dsn = DSN.new('https://TOKEN@api.uptrace.dev/PROJECT_ID')
23
19
  end
24
-
25
- setup_tracing unless @cfg.disabled
26
20
  end
27
21
 
28
- # @param [optional Numeric] timeout An optional timeout in seconds.
29
- def shutdown(timeout: nil)
30
- return if @cfg.disabled
31
-
32
- OpenTelemetry.tracer_provider.shutdown(timeout: timeout)
22
+ def disabled?
23
+ @disabled
33
24
  end
34
25
 
35
- # @return [OpenTelemetry::Trace::Span]
36
- def trace_url(span)
37
- dsn = @cfg.dsno
38
- host = dsn.host.delete_prefix('api.')
26
+ # @param [optional OpenTelemetry::Trace::Span] span
27
+ # @return [String]
28
+ def trace_url(span = nil)
29
+ span = OpenTelemetry::Trace.current_span if span.nil?
30
+
31
+ host = @dsn.host.delete_prefix('api.')
39
32
  trace_id = span.context.hex_trace_id
40
- "#{dsn.scheme}://#{host}/search/#{dsn.project_id}?q=#{trace_id}"
33
+ "#{@dsn.scheme}://#{host}/search/#{@dsn.project_id}?q=#{trace_id}"
41
34
  end
42
35
 
43
- private
44
-
45
- def setup_tracing
46
- exp = Uptrace::Trace::Exporter.new(@cfg)
47
-
48
- OpenTelemetry::SDK.configure do |c|
49
- bsp = OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
50
- exp,
51
- max_queue_size: 1000,
52
- max_export_batch_size: 1000,
53
- schedule_delay: 5_000
54
- )
55
- c.add_span_processor(bsp)
56
-
57
- c.service_name = @cfg.service_name
58
- c.service_version = @cfg.service_version
59
- end
36
+ # @return [OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor]
37
+ def span_processor
38
+ exp = Uptrace::Trace::Exporter.new(@dsn)
39
+ OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
40
+ exp,
41
+ max_queue_size: 1000,
42
+ max_export_batch_size: 1000,
43
+ schedule_delay: 5_000
44
+ )
60
45
  end
61
46
  end
62
47
  end
data/lib/uptrace/dsn.rb CHANGED
@@ -8,12 +8,12 @@ module Uptrace
8
8
  attr_reader :dsn, :port, *KEYS
9
9
 
10
10
  def initialize(dsn)
11
- raise ArgumentError, "uptrace: DSN can't be empty" if dsn.empty?
11
+ raise ArgumentError, "DSN can't be empty" if dsn.empty?
12
12
 
13
13
  begin
14
14
  uri = URI.parse(dsn)
15
15
  rescue URI::InvalidURIError => e
16
- raise ArgumentError, %(uptrace: can't parse DSN=#{dsn.inspect}: #{e})
16
+ raise ArgumentError, %(can't parse DSN=#{dsn.inspect}: #{e})
17
17
  end
18
18
 
19
19
  @dsn = dsn
@@ -25,7 +25,7 @@ module Uptrace
25
25
 
26
26
  KEYS.each do |k|
27
27
  v = public_send(k)
28
- raise ArgumentError, %(uptrace: DSN does not have #{k} (DSN=#{dsn.inspect})) if v.nil? || v.empty?
28
+ raise ArgumentError, %(DSN=#{dsn.inspect} does not have a #{k}) if v.nil? || v.empty?
29
29
  end
30
30
  end
31
31
 
data/lib/uptrace/trace.rb CHANGED
@@ -6,5 +6,4 @@ module Uptrace
6
6
  end
7
7
  end
8
8
 
9
- require 'uptrace/trace/config'
10
9
  require 'uptrace/trace/exporter'
@@ -20,13 +20,11 @@ module Uptrace
20
20
  ##
21
21
  # @param [Config] cfg
22
22
  #
23
- def initialize(cfg)
24
- @cfg = cfg
23
+ def initialize(dsn)
24
+ @dsn = dsn
25
+ @endpoint = "/api/v1/tracing/#{@dsn.project_id}/spans"
25
26
 
26
- dsn = @cfg.dsno
27
- @endpoint = "/api/v1/tracing/#{dsn.project_id}/spans"
28
-
29
- @http = Net::HTTP.new(dsn.host, 443)
27
+ @http = Net::HTTP.new(@dsn.host, 443)
30
28
  @http.use_ssl = true
31
29
  @http.open_timeout = 5
32
30
  @http.read_timeout = 5
@@ -53,6 +51,15 @@ module Uptrace
53
51
  send({ spans: out }, timeout: timeout)
54
52
  end
55
53
 
54
+ # Called when {OpenTelemetry::SDK::Trace::TracerProvider#force_flush} is called, if
55
+ # this exporter is registered to a {OpenTelemetry::SDK::Trace::TracerProvider}
56
+ # object.
57
+ #
58
+ # @param [optional Numeric] timeout An optional timeout in seconds.
59
+ def force_flush(timeout: nil) # rubocop:disable Lint/UnusedMethodArgument
60
+ SUCCESS
61
+ end
62
+
56
63
  # Called when {OpenTelemetry::SDK::Trace::Tracer#shutdown} is called, if
57
64
  # this exporter is registered to a {OpenTelemetry::SDK::Trace::Tracer}
58
65
  # object.
@@ -61,6 +68,7 @@ module Uptrace
61
68
  def shutdown(timeout: nil) # rubocop:disable Lint/UnusedMethodArgument
62
69
  @shutdown = true
63
70
  @http.finish if @http.started?
71
+ SUCCESS
64
72
  end
65
73
 
66
74
  private
@@ -82,18 +90,18 @@ module Uptrace
82
90
  attrs: span.attributes
83
91
  }
84
92
 
85
- out[parentId] = span.parent_span_id.unpack1('Q') if span.parent_span_id
93
+ out[:parentId] = span.parent_span_id.unpack1('Q') if span.parent_span_id
86
94
 
87
- out[events] = uptrace_events(span.events) unless span.events.nil?
88
- out[links] = uptrace_links(span.links) unless span.links.nil?
95
+ out[:events] = uptrace_events(span.events) unless span.events.nil?
96
+ out[:links] = uptrace_links(span.links) unless span.links.nil?
89
97
 
90
98
  status = span.status
91
- out[statusCode] = status_code_as_str(status.code)
92
- out[statusMessage] = status.description unless status.description.empty?
99
+ out[:statusCode] = status_code_as_str(status.code)
100
+ out[:statusMessage] = status.description unless status.description.empty?
93
101
 
94
102
  il = span.instrumentation_library
95
- out[tracerName] = il.name
96
- out[tracerVersion] = il.name unless il.version.empty?
103
+ out[:tracerName] = il.name
104
+ out[:tracerVersion] = il.name unless il.version.empty?
97
105
 
98
106
  out
99
107
  end
@@ -133,7 +141,7 @@ module Uptrace
133
141
  data = Zstd.compress(data, 3)
134
142
 
135
143
  req = Net::HTTP::Post.new(@endpoint)
136
- req.add_field('Authorization', "Bearer #{@cfg.dsno.token}")
144
+ req.add_field('Authorization', "Bearer #{@dsn.token}")
137
145
  req.add_field('Content-Type', 'application/msgpack')
138
146
  req.add_field('Content-Encoding', 'zstd')
139
147
  req.add_field('Connection', 'keep-alive')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uptrace
4
- VERSION = '0.2.2'
4
+ VERSION = '0.16.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uptrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uptrace Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.15.0
33
+ version: 0.16.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.15.0
40
+ version: 0.16.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: zstd-ruby
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -180,7 +180,6 @@ files:
180
180
  - lib/uptrace/dsn.rb
181
181
  - lib/uptrace/metric.rb
182
182
  - lib/uptrace/trace.rb
183
- - lib/uptrace/trace/config.rb
184
183
  - lib/uptrace/trace/exporter.rb
185
184
  - lib/uptrace/version.rb
186
185
  homepage: https://github.com/uptrace/uptrace-ruby
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'uptrace/dsn'
4
-
5
- module Uptrace
6
- module Trace
7
- # Config is a configuration for Uptrace span exporter.
8
- class Config
9
- # @return [string] a data source name to connect to uptrace.dev.
10
- attr_accessor :dsn
11
-
12
- # @return [string] `service.name` resource attribute.
13
- attr_accessor :service_name
14
-
15
- # @return [string] `service.name` resource attribute.
16
- attr_accessor :service_version
17
-
18
- # @return [boolean] disables the exporter.
19
- attr_accessor :disabled
20
-
21
- def initialize
22
- @dsn = ENV.fetch('UPTRACE_DSN', '')
23
- end
24
-
25
- def dsno
26
- @dsno ||= DSN.new(@dsn)
27
- end
28
- end
29
- end
30
- end