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 all 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
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: "[email protected]",
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: "[email protected]",
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