summaryrefslogtreecommitdiff
path: root/src/email/email_server.rb
diff options
context:
space:
mode:
authorSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
committerSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
commit82e03978b89938219958032efb1448cc76baa181 (patch)
tree626f3e54d52ecd49be0ed3bee30abacc0453d081 /src/email/email_server.rb
Initial snapshot - OpenTelemetry demo 2.1.3 -f
Diffstat (limited to 'src/email/email_server.rb')
-rw-r--r--src/email/email_server.rb83
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