Skip to content

Commit b48363c

Browse files
[demo] Add OTLP metrics and logs to email service (#8416)
1 parent 3f994f0 commit b48363c

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

content/en/docs/demo/services/email.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,75 @@ end
7474

7575
## Metrics
7676

77-
TBD
77+
### Initializing Metrics
78+
79+
The OpenTelemetry Metrics SDK and OTLP metrics exporter are initialized at root
80+
level in the `email_server.rb` file. You first need the `require` statements to
81+
access them.
82+
83+
```ruby
84+
require "opentelemetry-metrics-sdk"
85+
require "opentelemetry-exporter-otlp-metrics"
86+
```
87+
88+
The Ruby SDK uses OpenTelemetry standard environment variables to configure OTLP
89+
export, resource attributes, and service name automatically. When initializing
90+
the OpenTelemetry Metrics SDK, you also need to configure a meter provider and a
91+
metric reader.
92+
93+
```ruby
94+
otlp_metric_exporter = OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new
95+
OpenTelemetry.meter_provider.add_metric_reader(otlp_metric_exporter)
96+
meter = OpenTelemetry.meter_provider.meter("email")
97+
```
98+
99+
With the meter provider you now have access to the meter, which can be used to
100+
create a global metric (ie: `counter`).
101+
102+
```ruby
103+
$confirmation_counter = meter.create_counter("app.confirmation.counter", unit: "1", description: "Counts the number of order confirmation emails sent")
104+
```
105+
106+
### Custom metrics
107+
108+
The following custom metric is currently available:
109+
110+
- `app.confirmation.counter`: Cumulative count of number of order confirmation
111+
emails sent
78112

79113
## Logs
80114

81-
TBD
115+
### Initializing logs
116+
117+
The OpenTelemetry Logs SDK and OTLP logs exporter are initialized at root level
118+
in the `email_server.rb` file. You first need the `require` statements to access
119+
them.
120+
121+
```ruby
122+
require "opentelemetry-logs-sdk"
123+
require "opentelemetry-exporter-otlp-logs"
124+
```
125+
126+
The Ruby SDK uses OpenTelemetry standard environment variables to configure OTLP
127+
export, resource attributes, and service name automatically. When initializing
128+
the OpenTelemetry Logs SDK, you need a logger provider to create a global
129+
logger.
130+
131+
```ruby
132+
$logger = OpenTelemetry.logger_provider.logger(name: "email")
133+
```
134+
135+
### Emitting structured logs
136+
137+
You can use the logger’s `on_emit` method to write structured logs. Include
138+
`severity_text` (e.g., `INFO`, `ERROR`), a human-readable `body`, and
139+
`app.email.recipient` attribute that may help querying the logs later.
140+
141+
```ruby
142+
$logger.on_emit(
143+
timestamp: Time.now,
144+
severity_text: "INFO",
145+
body: "Order confirmation email sent",
146+
attributes: { "app.email.recipient" => data.email }
147+
)
148+
```

0 commit comments

Comments
 (0)