Skip to content

Commit 74c251a

Browse files
vmihailencodvora-h
andauthored
Add OpenTelemetry example with Uptrace backend (#2452)
* chore: add opentelemetry example * chore: add opentelemetry API Jupyter notebook * chore: use a shorter title * chore: cleanup Co-authored-by: dvora-h <[email protected]>
1 parent 022a3e8 commit 74c251a

18 files changed

+1248
-2
lines changed

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
# Add any paths that contain custom static files (such as style sheets) here,
163163
# relative to this directory. They are copied after the builtin static files,
164164
# so a file named "default.css" will overwrite the builtin "default.css".
165-
html_static_path = ["_static"]
165+
html_static_path = ["_static", "images"]
166166

167167
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
168168
# using the given strftime format.

docs/examples.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ Examples
1313
examples/search_vector_similarity_examples
1414
examples/pipeline_examples
1515
examples/timeseries_examples
16-
examples/redis-stream-example.ipynb
16+
examples/redis-stream-example
17+
examples/opentelemetry_api_examples

docs/examples/opentelemetry/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Example for redis-py OpenTelemetry instrumentation
2+
3+
This example demonstrates how to monitor Redis using [OpenTelemetry](https://opentelemetry.io/) and
4+
[Uptrace](https://github.com/uptrace/uptrace). It requires Docker to start Redis Server and Uptrace.
5+
6+
See
7+
[Monitoring redis-py performance with OpenTelemetry](https://redis-py.readthedocs.io/en/latest/opentelemetry.html)
8+
for details.
9+
10+
**Step 1**. Download the example using Git:
11+
12+
```shell
13+
git clone https://github.com/redis/redis-py.git
14+
cd example/opentelemetry
15+
```
16+
17+
**Step 2**. Optionally, create a virtualenv:
18+
19+
```shell
20+
python3 -m venv .venv
21+
source .venv/bin/active
22+
```
23+
24+
**Step 3**. Install dependencies:
25+
26+
```shell
27+
pip install -r requirements.txt
28+
```
29+
30+
**Step 4**. Start the services using Docker and make sure Uptrace is running:
31+
32+
```shell
33+
docker-compose up -d
34+
docker-compose logs uptrace
35+
```
36+
37+
**Step 5**. Run the Redis client example and follow the link from the CLI to view the trace:
38+
39+
```shell
40+
python3 main.py
41+
trace: http://localhost:14318/traces/ee029d8782242c8ed38b16d961093b35
42+
```
43+
44+
![Redis trace](./image/redis-py-trace.png)
45+
46+
You can also open Uptrace UI at [http://localhost:14318](http://localhost:14318) to view available
47+
spans, logs, and metrics.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# See https://prometheus.io/docs/alerting/latest/configuration/ for details.
2+
3+
global:
4+
# The smarthost and SMTP sender used for mail notifications.
5+
smtp_smarthost: "mailhog:1025"
6+
smtp_from: "[email protected]"
7+
smtp_require_tls: false
8+
9+
receivers:
10+
- name: "team-X"
11+
email_configs:
12+
13+
send_resolved: true
14+
15+
# The root route on which each incoming alert enters.
16+
route:
17+
# The labels by which incoming alerts are grouped together. For example,
18+
# multiple alerts coming in for cluster=A and alertname=LatencyHigh would
19+
# be batched into a single group.
20+
group_by: ["alertname", "cluster", "service"]
21+
22+
# When a new group of alerts is created by an incoming alert, wait at
23+
# least 'group_wait' to send the initial notification.
24+
# This way ensures that you get multiple alerts for the same group that start
25+
# firing shortly after another are batched together on the first
26+
# notification.
27+
group_wait: 30s
28+
29+
# When the first notification was sent, wait 'group_interval' to send a batch
30+
# of new alerts that started firing for that group.
31+
group_interval: 5m
32+
33+
# If an alert has successfully been sent, wait 'repeat_interval' to
34+
# resend them.
35+
repeat_interval: 3h
36+
37+
# A default receiver
38+
receiver: team-X
39+
40+
# All the above attributes are inherited by all child routes and can
41+
# overwritten on each.
42+
43+
# The child route trees.
44+
routes:
45+
# This route matches error alerts created from spans or logs.
46+
- matchers:
47+
- alert_kind="error"
48+
group_interval: 24h
49+
receiver: team-X
50+
51+
# The directory from which notification templates are read.
52+
templates:
53+
- "/etc/alertmanager/template/*.tmpl"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
extensions:
2+
health_check:
3+
pprof:
4+
endpoint: 0.0.0.0:1777
5+
zpages:
6+
endpoint: 0.0.0.0:55679
7+
8+
receivers:
9+
otlp:
10+
protocols:
11+
grpc:
12+
http:
13+
hostmetrics:
14+
collection_interval: 10s
15+
scrapers:
16+
cpu:
17+
disk:
18+
load:
19+
filesystem:
20+
memory:
21+
network:
22+
paging:
23+
redis:
24+
endpoint: "redis-server:6379"
25+
collection_interval: 10s
26+
jaeger:
27+
protocols:
28+
grpc:
29+
30+
processors:
31+
resourcedetection:
32+
detectors: ["system"]
33+
batch:
34+
send_batch_size: 10000
35+
timeout: 10s
36+
37+
exporters:
38+
logging:
39+
logLevel: debug
40+
otlp:
41+
endpoint: uptrace:14317
42+
tls:
43+
insecure: true
44+
headers: { "uptrace-dsn": "http://project2_secret_token@localhost:14317/2" }
45+
46+
service:
47+
# telemetry:
48+
# logs:
49+
# level: DEBUG
50+
pipelines:
51+
traces:
52+
receivers: [otlp, jaeger]
53+
processors: [batch]
54+
exporters: [otlp, logging]
55+
metrics:
56+
receivers: [otlp]
57+
processors: [batch]
58+
exporters: [otlp]
59+
metrics/hostmetrics:
60+
receivers: [hostmetrics, redis]
61+
processors: [batch, resourcedetection]
62+
exporters: [otlp]
63+
logs:
64+
receivers: [otlp]
65+
processors: [batch]
66+
exporters: [otlp]
67+
68+
extensions: [health_check, pprof, zpages]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[sources.syslog_logs]
2+
type = "demo_logs"
3+
format = "syslog"
4+
interval = 0.1
5+
6+
[sources.apache_common_logs]
7+
type = "demo_logs"
8+
format = "apache_common"
9+
interval = 0.1
10+
11+
[sources.apache_error_logs]
12+
type = "demo_logs"
13+
format = "apache_error"
14+
interval = 0.1
15+
16+
[sources.json_logs]
17+
type = "demo_logs"
18+
format = "json"
19+
interval = 0.1
20+
21+
# Parse Syslog logs
22+
# See the Vector Remap Language reference for more info: https://vrl.dev
23+
[transforms.parse_logs]
24+
type = "remap"
25+
inputs = ["syslog_logs"]
26+
source = '''
27+
. = parse_syslog!(string!(.message))
28+
'''
29+
30+
# Export data to Uptrace.
31+
[sinks.uptrace]
32+
type = "http"
33+
inputs = ["parse_logs", "apache_common_logs", "apache_error_logs", "json_logs"]
34+
encoding.codec = "json"
35+
framing.method = "newline_delimited"
36+
compression = "gzip"
37+
uri = "http://uptrace:14318/api/v1/vector/logs"
38+
#uri = "https://api.uptrace.dev/api/v1/vector/logs"
39+
headers.uptrace-dsn = "http://project2_secret_token@localhost:14317/2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
version: "3"
2+
3+
services:
4+
clickhouse:
5+
image: clickhouse/clickhouse-server:22.7
6+
restart: on-failure
7+
environment:
8+
CLICKHOUSE_DB: uptrace
9+
healthcheck:
10+
test: ["CMD", "wget", "--spider", "-q", "localhost:8123/ping"]
11+
interval: 1s
12+
timeout: 1s
13+
retries: 30
14+
volumes:
15+
- ch_data:/var/lib/clickhouse
16+
ports:
17+
- "8123:8123"
18+
- "9000:9000"
19+
20+
uptrace:
21+
image: "uptrace/uptrace:1.2.0"
22+
#image: 'uptrace/uptrace-dev:latest'
23+
restart: on-failure
24+
volumes:
25+
- uptrace_data:/var/lib/uptrace
26+
- ./uptrace.yml:/etc/uptrace/uptrace.yml
27+
#environment:
28+
# - DEBUG=2
29+
ports:
30+
- "14317:14317"
31+
- "14318:14318"
32+
depends_on:
33+
clickhouse:
34+
condition: service_healthy
35+
36+
otel-collector:
37+
image: otel/opentelemetry-collector-contrib:0.58.0
38+
restart: on-failure
39+
volumes:
40+
- ./config/otel-collector.yaml:/etc/otelcol-contrib/config.yaml
41+
ports:
42+
- "4317:4317"
43+
- "4318:4318"
44+
45+
vector:
46+
image: timberio/vector:0.24.X-alpine
47+
volumes:
48+
- ./config/vector.toml:/etc/vector/vector.toml:ro
49+
50+
alertmanager:
51+
image: prom/alertmanager:v0.24.0
52+
restart: on-failure
53+
volumes:
54+
- ./config/alertmanager.yml:/etc/alertmanager/config.yml
55+
- alertmanager_data:/alertmanager
56+
ports:
57+
- 9093:9093
58+
command:
59+
- "--config.file=/etc/alertmanager/config.yml"
60+
- "--storage.path=/alertmanager"
61+
62+
mailhog:
63+
image: mailhog/mailhog:v1.0.1
64+
restart: on-failure
65+
ports:
66+
- "8025:8025"
67+
68+
redis-server:
69+
image: redis
70+
ports:
71+
- "6379:6379"
72+
redis-cli:
73+
image: redis
74+
75+
volumes:
76+
uptrace_data:
77+
driver: local
78+
ch_data:
79+
driver: local
80+
alertmanager_data:
81+
driver: local
Loading

docs/examples/opentelemetry/main.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
import time
4+
5+
import uptrace
6+
from opentelemetry import trace
7+
from opentelemetry.instrumentation.redis import RedisInstrumentor
8+
9+
import redis
10+
11+
tracer = trace.get_tracer("app_or_package_name", "1.0.0")
12+
13+
14+
def main():
15+
uptrace.configure_opentelemetry(
16+
dsn="http://project2_secret_token@localhost:14317/2",
17+
service_name="myservice",
18+
service_version="1.0.0",
19+
)
20+
RedisInstrumentor().instrument()
21+
22+
client = redis.StrictRedis(host="localhost", port=6379)
23+
24+
span = handle_request(client)
25+
print("trace:", uptrace.trace_url(span))
26+
27+
for i in range(10000):
28+
handle_request(client)
29+
time.sleep(1)
30+
31+
32+
def handle_request(client):
33+
with tracer.start_as_current_span(
34+
"handle-request", kind=trace.SpanKind.CLIENT
35+
) as span:
36+
client.get("my-key")
37+
client.set("hello", "world")
38+
client.mset(
39+
{
40+
"employee_name": "Adam Adams",
41+
"employee_age": 30,
42+
"position": "Software Engineer",
43+
}
44+
)
45+
46+
pipe = client.pipeline()
47+
pipe.set("foo", 5)
48+
pipe.set("bar", 18.5)
49+
pipe.set("blee", "hello world!")
50+
pipe.execute()
51+
52+
return span
53+
54+
55+
if __name__ == "__main__":
56+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
redis==4.3.4
2+
uptrace==1.14.0
3+
opentelemetry-instrumentation-redis==0.35b0

0 commit comments

Comments
 (0)