diff options
Diffstat (limited to 'src/email/email_server.rb')
| -rw-r--r-- | src/email/email_server.rb | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/email/email_server.rb b/src/email/email_server.rb new file mode 100644 index 0000000..98a5259 --- /dev/null +++ b/src/email/email_server.rb @@ -0,0 +1,83 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +require "ostruct" +require "pony" +require "sinatra" +require "open_feature/sdk" +require "openfeature/flagd/provider" +# require "open_feature/flagd_provider" + +require "opentelemetry/sdk" +require "opentelemetry/exporter/otlp" +require "opentelemetry/instrumentation/sinatra" + +set :port, ENV["EMAIL_PORT"] + +# Initialize OpenFeature SDK with flagd provider +flagd_client = OpenFeature::Flagd::Provider.build_client +flagd_client.configure do |config| + config.host = ENV.fetch("FLAGD_HOST", "localhost") + config.port = ENV.fetch("FLAGD_PORT", 8013).to_i + config.tls = ENV.fetch("FLAGD_TLS", "false") == "true" +end + +OpenFeature::SDK.configure do |config| + config.set_provider(flagd_client) +end + +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| + # Check if memory leak flag is enabled + client = OpenFeature::SDK.build_client + memory_leak_multiplier = client.fetch_number_value(flag_key: "emailMemoryLeak", default_value: 0) + + # To speed up the memory leak we create a long email body + confirmation_content = erb(:confirmation, locals: { order: data.order }) + whitespace_length = [0, confirmation_content.length * (memory_leak_multiplier-1)].max + + Pony.mail( + to: data.email, + from: "noreply@example.com", + subject: "Your confirmation email", + body: confirmation_content + " " * whitespace_length, + via: :test + ) + + # If not clearing the deliveries, the emails will accumulate in the test mailer + # We use this to create a memory leak. + if memory_leak_multiplier < 1 + Mail::TestMailer.deliveries.clear + end + + 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 |
