forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_server.rb
53 lines (43 loc) · 1.45 KB
/
email_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
require "ostruct"
require "pony"
require "sinatra"
require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/sinatra"
set :port, ENV["EMAIL_PORT"]
OpenTelemetry::SDK.configure do |c|
c.use "OpenTelemetry::Instrumentation::Sinatra"
end
post "/send_order_confirmation" do
data = JSON.parse(request.body.read, object_class: OpenStruct)
# get the current auto-instrumented span
current_span = OpenTelemetry::Trace.current_span
current_span.add_attributes({
"app.order.id" => data.order.order_id,
})
send_email(data)
end
error do
OpenTelemetry::Trace.current_span.record_exception(env['sinatra.error'])
end
def send_email(data)
# create and start a manual span
tracer = OpenTelemetry.tracer_provider.tracer('email')
tracer.in_span("send_email") do |span|
Pony.mail(
to: data.email,
from: "noreply@example.com",
subject: "Your confirmation email",
body: erb(:confirmation, locals: { order: data.order }),
via: :test
)
span.set_attribute("app.email.recipient", data.email)
puts "Order confirmation email sent to: #{data.email}"
end
# manually created spans need to be ended
# in Ruby, the method `in_span` ends it automatically
# check out the OpenTelemetry Ruby docs at:
# https://opentelemetry.io/docs/instrumentation/ruby/manual/#creating-new-spans
end