Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual instrumentation to emailservice #158

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/emailservice/email_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,40 @@

post "/send_order_confirmation" do
data = JSON.parse(request.body.read, object_class: OpenStruct)
Pony.mail(
to: data.email,
from: "noreply@example.com",
subject: "Your confirmation email",
body: erb(:confirmation, locals: { order: data.order }),
via: :logger
)

# get the current auto-instrumented span
current_span = OpenTelemetry::Trace.current_span
current_span.add_attributes({
"app.order.id" => data.order.order_id,
"app.shipping.tracking.id" => data.order.shipping_tracking_id,
"app.shipping.cost.currency" => data.order.shipping_cost.currency_code,
"app.shipping.cost" => data.order.shipping_cost.units.to_s + "." +
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
data.order.shipping_cost.nanos.to_s
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
})

send_email(data)

rescue Exception => e
# record exception in span (will create a span event)
current_span.record_exception(e)
raise e
end
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved

def send_email(data)
# create and start a manual span
tracer = OpenTelemetry.tracer_provider.tracer('')
julianocosta89 marked this conversation as resolved.
Show resolved Hide resolved
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: :logger
)
span.set_attribute("app.email.sent", true)
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