1- # Uptrace Ruby exporter for OpenTelemetry
1+ # OpenTelemetry Ruby distro for Uptrace
22
33![ build workflow] ( https://github.com/uptrace/uptrace-ruby/actions/workflows/build.yml/badge.svg )
44[ ![ Documentation] ( https://img.shields.io/badge/uptrace-documentation-informational )] ( https://uptrace.dev/get/opentelemetry-ruby )
1010
1111## Introduction
1212
13- uptrace-ruby is an OpenTelemery distribution configured to export
14- [ traces] ( https://uptrace.dev/opentelemetry/distributed-tracing ) to Uptrace.
13+ ` uptrace-ruby ` is a preconfigured [ OpenTelemetry] ( https://opentelemetry.io )
14+ distribution for Ruby that exports ** traces, logs, and metrics** to
15+ [ Uptrace] ( https://uptrace.dev ) .
16+ It builds on top of
17+ [ opentelemetry-ruby] ( https://github.com/open-telemetry/opentelemetry-ruby ) and
18+ makes connecting your application to Uptrace fast and easy.
1519
1620## Quickstart
1721
@@ -21,7 +25,8 @@ Install uptrace-ruby:
2125gem install uptrace
2226```
2327
24- Run the [ basic example] ( example/basic ) below using the DSN from the Uptrace project settings page.
28+ Run the [ traces example] ( example/traces ) below using the DSN from the Uptrace
29+ project settings page.
2530
2631``` ruby
2732# !/usr/bin/env ruby
@@ -32,42 +37,52 @@ require 'bundler/setup'
3237require ' uptrace'
3338
3439# Configure OpenTelemetry with sensible defaults.
35- # Copy your project DSN here or use UPTRACE_DSN env var.
40+ # DSN can be set via UPTRACE_DSN environment variable.
41+ # Example: export UPTRACE_DSN="https://<project_secret>@uptrace.dev?grpc=4317"
3642Uptrace .configure_opentelemetry(dsn: ' ' ) do |c |
37- # c is OpenTelemetry::SDK::Configurator
43+ # c is an instance of OpenTelemetry::SDK::Configurator
44+
45+ # Configure service metadata (helps identify this service in Uptrace).
3846 c.service_name = ' myservice'
3947 c.service_version = ' 1.0.0'
4048
49+ # Add environment information
4150 c.resource = OpenTelemetry ::SDK ::Resources ::Resource .create(
42- ' deployment.environment' => ' production '
51+ ' deployment.environment.name ' => ENV .fetch( ' RACK_ENV ' , ' development ' )
4352 )
4453end
4554
46- # Create a tracer. Usually, tracer is a global variable.
47- tracer = OpenTelemetry .tracer_provider.tracer(' my_app_or_gem' , ' 0.1.0' )
48-
49- # Create a root span (a trace) to measure some operation.
50- tracer.in_span(' main-operation' , kind: :client ) do |main |
51- tracer.in_span(' GET /posts/:id' ) do |child1 |
52- child1.set_attribute(' http.method' , ' GET' )
53- child1.set_attribute(' http.route' , ' /posts/:id' )
54- child1.set_attribute(' http.url' , ' http://localhost:8080/posts/123' )
55- child1.set_attribute(' http.status_code' , 200 )
56- child1.record_exception(ArgumentError .new (' error1' ))
55+ # Ensure spans are flushed even if the program exits unexpectedly.
56+ at_exit { OpenTelemetry .tracer_provider.shutdown }
57+
58+ # Register a tracer (usually stored globally).
59+ TRACER = OpenTelemetry .tracer_provider.tracer(' my_app' , ' 0.1.0' )
60+
61+ # Example trace with nested spans.
62+ TRACER .in_span(' main-operation' , kind: :server ) do |main_span |
63+ # Simulate an HTTP request span.
64+ TRACER .in_span(' GET /posts/:id' , kind: :client ) do |http_span |
65+ http_span.set_attribute(' http.method' , ' GET' )
66+ http_span.set_attribute(' http.route' , ' /posts/:id' )
67+ http_span.set_attribute(' http.url' , ' http://localhost:8080/posts/123' )
68+ http_span.set_attribute(' http.status_code' , 200 )
69+ http_span.record_exception(ArgumentError .new (' Invalid parameter' ))
5770 end
5871
59- tracer.in_span(' SELECT' ) do |child2 |
60- child2.set_attribute(' db.system' , ' mysql' )
61- child2.set_attribute(' db.statement' , ' SELECT * FROM posts LIMIT 100' )
72+ # Simulate a database query span.
73+ TRACER .in_span(' SELECT posts' , kind: :client ) do |db_span |
74+ db_span.set_attribute(' db.system' , ' mysql' )
75+ db_span.set_attribute(' db.statement' , ' SELECT * FROM posts LIMIT 100' )
6276 end
6377
64- puts (" trace URL: #{ Uptrace .trace_url(main)} " )
78+ # Print the trace URL (clickable in console).
79+ puts " Trace URL: #{ Uptrace .trace_url(main_span)} "
6580end
66-
67- # Send buffered spans and free resources.
68- OpenTelemetry .tracer_provider.shutdown
6981```
7082
83+ Additional examples are available for [ logs] ( example/logs ) and
84+ [ metrics] ( example/metrics ) .
85+
7186## Links
7287
7388- [ Examples] ( example )
0 commit comments