Skip to content

Commit 1cde36a

Browse files
add test and update readme
1 parent 859efaa commit 1cde36a

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Prerequisites:
2727
* [encryption](encryption) - Demonstrates how to make a codec for end-to-end encryption.
2828
* [env_config](env_config) - Load client configuration from TOML files with programmatic overrides.
2929
* [message_passing_simple](message_passing_simple) - Simple workflow that accepts signals, queries, and updates.
30+
* [opentelemetry](opentelemetry) - Demonstrates how to use OpenTelemetry tracing and metrics with the Ruby SDK
3031
* [polling/infrequent](polling/infrequent) - Implement an infrequent polling mechanism using Temporal's automatic Activity Retry feature.
3132
* [rails_app](rails_app) - Basic Rails API application using Temporal workflows and activities.
3233
* [saga](saga) - Using undo/compensation using a very simplistic Saga pattern.
@@ -40,4 +41,3 @@ Prerequisites:
4041
To check format and test this repository, run:
4142

4243
bundle exec rake
43-

opentelemetry/README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
TODO: Doc that you have to use the below image, and how to see metrics and traces (traces are via
2-
Explore -> Tempo -> Query Type: Search).
1+
# OpenTelemetry Sample
32

4-
> docker run -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti grafana/otel-lgtm
3+
Demonstrates how to use OpenTelemetry tracing and metrics with the Ruby SDK
4+
5+
## How to Run
6+
7+
First, in another terminal start up a Grafana OpenTelemetry instance which will
8+
collect telemetry and provide the Grafana UI for viewing the data.
9+
10+
```bash
11+
docker compose up
12+
```
13+
14+
In another terminal, start the worker
15+
16+
```bash
17+
bundle exec ruby worker.rb
18+
```
19+
20+
Finally start the workflow
21+
22+
```bash
23+
bundle exec ruby starter.rb
24+
```
25+
26+
You should be able to see the result in the terminal.
27+
28+
To view the Grafana dashboard go to `http://localhost:3000`
29+
30+
You can find the trace by clicking on the "Explore" tab,
31+
selecting "Tempo" as the data source, and switching the query type to "Search".
32+
33+
There will be a trace for `my-service` containing the workflow trace.

opentelemetry/docker-compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
grafana-dashboard:
3+
image: grafana/otel-lgtm:latest
4+
tty: true
5+
ports:
6+
- 3000:3000
7+
- 4317:4317
8+
- 4318:4318

opentelemetry/util.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'opentelemetry/exporter/otlp'
24
require 'opentelemetry/sdk'
35
require 'temporalio/contrib/open_telemetry'
@@ -18,8 +20,8 @@ def self.configure_metrics_and_tracing
1820
)
1921
)
2022
)
21-
# Globally configure the Ruby OpenTelemetry library for tracing purposes. As of this writing, OpenTelemetry Ruby does
22-
# not support OTLP over gRPC, so we use the HTTP endpoint instead.
23+
# Globally configure the Ruby OpenTelemetry library for tracing purposes. As of this writing, OpenTelemetry Ruby
24+
# does not support OTLP over gRPC, so we use the HTTP endpoint instead.
2325
OpenTelemetry::SDK.configure do |c|
2426
c.service_name = 'my-service'
2527
c.use_all

opentelemetry/worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
.record(1.23)
1919

2020
# Create a client with the tracing interceptor set using the tracer
21-
tracer = OpenTelemetry.tracer_provider.tracer('temporal_ruby_sample', '0.1.0')
21+
tracer = OpenTelemetry.tracer_provider.tracer('opentelemetry_sample', '1.0.0')
2222
client = Temporalio::Client.connect(
2323
'localhost:7233',
2424
'default',
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'test'
4+
require 'opentelemetry/compose_greeting_activity'
5+
require 'opentelemetry/greeting_workflow'
6+
require 'opentelemetry/sdk'
7+
require 'securerandom'
8+
require 'temporalio/client'
9+
require 'temporalio/contrib/open_telemetry'
10+
require 'temporalio/testing'
11+
require 'temporalio/worker'
12+
13+
module OpenTelemetrySample
14+
class GreetingWorkflowTest < Test
15+
def test_workflow
16+
# Setup in memory buffer for telemetry events
17+
metrics_buffer = Temporalio::Runtime::MetricBuffer.new(1024)
18+
runtime = Temporalio::Runtime.new(
19+
telemetry: Temporalio::Runtime::TelemetryOptions.new(
20+
metrics: Temporalio::Runtime::MetricsOptions.new(
21+
buffer: metrics_buffer
22+
)
23+
)
24+
)
25+
tracer = OpenTelemetry.tracer_provider.tracer('opentelemetry_sample_test', '1.0.0')
26+
Temporalio::Testing::WorkflowEnvironment.start_local(
27+
runtime:,
28+
interceptors: [Temporalio::Contrib::OpenTelemetry::TracingInterceptor.new(tracer)]
29+
) do |env|
30+
# Run workflow in a worker
31+
env.client
32+
worker = Temporalio::Worker.new(
33+
client: env.client,
34+
task_queue: "tq-#{SecureRandom.uuid}",
35+
activities: [ComposeGreetingActivity.new(tracer)],
36+
workflows: [GreetingWorkflow]
37+
)
38+
result = worker.run do
39+
handle = env.client.start_workflow(
40+
GreetingWorkflow, 'Temporal',
41+
id: "wf-#{SecureRandom.uuid}",
42+
task_queue: worker.task_queue
43+
)
44+
handle.result
45+
end
46+
assert_equal 'Hello, Temporal!', result
47+
assert !metrics_buffer.retrieve_updates.empty?
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)