Skip to content

Commit

Permalink
Add manual instrumentation to emailservice (open-telemetry#158)
Browse files Browse the repository at this point in the history
* Add manual instrumentation to emailservice

* Apply name suggestion

* Apply suggestions

Co-authored-by: Carter Socha <43380952+cartersocha@users.noreply.github.com>
  • Loading branch information
julianocosta89 and cartersocha authored Jun 23, 2022
1 parent 32e31a2 commit c32bc31
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/emailservice/email_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,39 @@

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.total" => "#{data.order.shipping_cost.units}.#{data.order.shipping_cost.nanos}",
})

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('emailservice')
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

0 comments on commit c32bc31

Please sign in to comment.