From abf15cce8c85fcda6a37b67bee711a6fd85997e6 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Sun, 19 Jun 2022 18:02:02 +0200 Subject: [PATCH 1/3] Add manual instrumentation to emailservice --- src/emailservice/email_server.rb | 43 ++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/emailservice/email_server.rb b/src/emailservice/email_server.rb index f5c1b1de1f..bb150c30f7 100644 --- a/src/emailservice/email_server.rb +++ b/src/emailservice/email_server.rb @@ -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.email.order_id" => data.order.order_id, + "app.email.shipping_tracking_id" => data.order.shipping_tracking_id, + "app.email.shipping_cost.currency" => data.order.shipping_cost.currency_code, + "app.email.shipping_cost" => data.order.shipping_cost.units.to_s + "." + + data.order.shipping_cost.nanos.to_s + }) + + send_email(data) + + rescue Exception => e + # record exception in span (will create a span event) + current_span.record_exception(e) + raise e +end + +def send_email(data) + # create and start a manual span + tracer = OpenTelemetry.tracer_provider.tracer('') + 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 From 8dbe9a2b57a64c71f7b06d7c9dc592d80cc2d809 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Sun, 19 Jun 2022 18:51:22 +0200 Subject: [PATCH 2/3] Apply name suggestion --- src/emailservice/email_server.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/emailservice/email_server.rb b/src/emailservice/email_server.rb index bb150c30f7..3d0c2cdcb3 100644 --- a/src/emailservice/email_server.rb +++ b/src/emailservice/email_server.rb @@ -16,10 +16,10 @@ # get the current auto-instrumented span current_span = OpenTelemetry::Trace.current_span current_span.add_attributes({ - "app.email.order_id" => data.order.order_id, - "app.email.shipping_tracking_id" => data.order.shipping_tracking_id, - "app.email.shipping_cost.currency" => data.order.shipping_cost.currency_code, - "app.email.shipping_cost" => data.order.shipping_cost.units.to_s + "." + + "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 + "." + data.order.shipping_cost.nanos.to_s }) From 093f2cdffa4f673b5176aab30b8cca91bf541b54 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Wed, 22 Jun 2022 21:27:41 +0200 Subject: [PATCH 3/3] Apply suggestions --- src/emailservice/email_server.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/emailservice/email_server.rb b/src/emailservice/email_server.rb index 3d0c2cdcb3..268b790fd4 100644 --- a/src/emailservice/email_server.rb +++ b/src/emailservice/email_server.rb @@ -19,21 +19,20 @@ "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 + "." + - data.order.shipping_cost.nanos.to_s + "app.shipping.cost.total" => "#{data.order.shipping_cost.units}.#{data.order.shipping_cost.nanos}", }) send_email(data) - rescue Exception => e - # record exception in span (will create a span event) - current_span.record_exception(e) - raise e +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('') + tracer = OpenTelemetry.tracer_provider.tracer('emailservice') tracer.in_span("send_email") do |span| Pony.mail( to: data.email,