uptrace 0.2.3 → 0.16.1

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: 3cc81f964b6c014c38abbdfcf4250fa467ba0c8ef2dfd8077967d851bdd9f567
4
- data.tar.gz: 5fb6975a0916e18d1603aa03ec78d7639b4e6a37de079e2ed72326a7db8c6ee0
3
+ metadata.gz: 3e04833f5ff0015d78af4774cc49272db2c0c10e3cd93ce5acbe6cfc3e9e3772
4
+ data.tar.gz: f027f68acd8f95b550bdbde26322307420f8e833447aff1a268d06ac57b00910
5
5
  SHA512:
6
- metadata.gz: ba5ef3bc321a50fd4203a033a023bbfc491071d29a05edb9c9a93cd6a2c53a8a59ec6af2e97cc919d57695193993ecedbf42914b53cbda72a6ab80d9085b763c
7
- data.tar.gz: e5a44d04828a788321193d00a6c7196fa38c92ce7aaa274636448f50609c3759410d11444360cd42f7d1ee066af020660e706732b2209686dc224da707d6ec33
6
+ metadata.gz: 1ef5879252c95a7ca008f042061ecc1d674f36822739e8f17bb19ae2385ef1f6cdc02bc2885edf3538da13d3f9d5f9feca849c3f553381de4b393f55aedf570d
7
+ data.tar.gz: ef03b0f10c6d992d81f5d64c0df4d9eb8c9590399c61fe9f3b9b8cc166492309940c2e2d6479f2366a9137d111caae6ce16b6c9f53c4f89f176b532345d94e2f
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/lib/uptrace.rb CHANGED
@@ -1,14 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
+ require 'opentelemetry'
4
5
 
5
6
  # Uptrace provides Uptrace exporters for OpenTelemetry.
6
7
  module Uptrace
7
8
  extend self
8
9
 
9
10
  attr_accessor :logger
11
+ attr_writer :client
10
12
 
11
13
  self.logger = Logger.new($stdout)
14
+
15
+ # @return [Object, Client] registered client or a default no-op implementation of the client.
16
+ def client
17
+ @client ||= Client.new
18
+ end
19
+
20
+ # @param [optional OpenTelemetry::Trace::Span] span
21
+ # @return [String]
22
+ def trace_url(span = nil)
23
+ client.trace_url(span)
24
+ end
25
+
26
+ # ConfigureOpentelemetry configures OpenTelemetry to export data to Uptrace.
27
+ # By default it:
28
+ # - creates tracer provider;
29
+ # - registers Uptrace span exporter;
30
+ # - sets tracecontext + baggage composite context propagator.
31
+ #
32
+ # @param [OpenTelemetry::SDK::Configurator] c
33
+ def configure_opentelemetry(c, dsn: '')
34
+ @client = Client.new(dsn: dsn) unless dsn.empty?
35
+
36
+ c.add_span_processor(client.span_processor) unless client.disabled?
37
+
38
+ if OpenTelemetry.propagation.nil?
39
+ c.injectors = [
40
+ OpenTelemetry::Trace::Propagation::TraceContext.text_map_injector,
41
+ OpenTelemetry::Baggage::Propagation.text_map_injector
42
+ ]
43
+ c.extractors = [
44
+ OpenTelemetry::Trace::Propagation::TraceContext.text_map_extractor,
45
+ OpenTelemetry::Baggage::Propagation.text_map_extractor
46
+ ]
47
+ end
48
+ end
12
49
  end
13
50
 
14
51
  require 'uptrace/version'
@@ -5,43 +5,37 @@ 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?
15
-
16
- @cfg.dsn = ENV.fetch('UPTRACE_DSN', '') if @cfg.dsn.nil? || @cfg.dsn.empty?
8
+ # @param [string] dsn
9
+ def initialize(dsn: '')
10
+ dsn = ENV.fetch('UPTRACE_DSN', '') if dsn.empty?
17
11
 
18
12
  begin
19
- @cfg.dsno
13
+ @dsn = DSN.new(dsn)
20
14
  rescue ArgumentError => e
21
15
  Uptrace.logger.error("Uptrace is disabled: #{e.message}")
22
- @cfg.disabled = true
16
+ @disabled = true
23
17
 
24
- @cfg.dsn = 'https://TOKEN@api.uptrace.dev/PROJECT_ID'
18
+ @dsn = DSN.new('https://TOKEN@api.uptrace.dev/PROJECT_ID')
25
19
  end
26
20
  end
27
21
 
28
- # @param [optional Numeric] timeout An optional timeout in seconds.
29
- def close(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
 
36
+ # @return [OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor]
43
37
  def span_processor
44
- exp = Uptrace::Trace::Exporter.new(@cfg)
38
+ exp = Uptrace::Trace::Exporter.new(@dsn)
45
39
  OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
46
40
  exp,
47
41
  max_queue_size: 1000,
data/lib/uptrace/dsn.rb CHANGED
@@ -25,7 +25,7 @@ module Uptrace
25
25
 
26
26
  KEYS.each do |k|
27
27
  v = public_send(k)
28
- raise ArgumentError, %(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
@@ -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.3'
4
+ VERSION = '0.16.1'
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.3
4
+ version: 0.16.1
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-03-18 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,26 +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 dsno
22
- @dsno ||= DSN.new(@dsn)
23
- end
24
- end
25
- end
26
- end