From 8ae095721590efe6968910c5508d88c51ce968d8 Mon Sep 17 00:00:00 2001 From: Davi Arnaut Date: Mon, 22 Feb 2021 12:03:32 -0800 Subject: [PATCH] Support for Datadog's tracing client --- Gemfile | 4 ++++ config.ru | 30 +++++++++++++++++++++++++++++- optica.rb | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 25eb01b..274adaf 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,7 @@ group :newrelic do gem 'newrelic_rpm', '~> 3.18.1' gem 'newrelic-zookeeper', '~> 1.0.0' end + +group :ddtrace do + gem 'ddtrace', '~> 0.45.0' +end diff --git a/config.ru b/config.ru index 08e72f5..26597b1 100644 --- a/config.ru +++ b/config.ru @@ -110,11 +110,39 @@ when false, nil then false else raise 'unknown value for ip_check option' end -# start the app +# load the app require './optica.rb' + +# configure tracing client +def datadog_config(log) + Datadog.configure do |c| + service = ENV.fetch('DD_SERVICE', 'optica') + c.use :sinatra, service_name: service + # Statsd instance used for sending runtime metrics + c.runtime_metrics.statsd = STATSD + end + + # register tracer extension + Optica.register Datadog::Contrib::Sinatra::Tracer + + # add correlation IDs to logger + log.formatter = proc do |severity, datetime, progname, msg| + "[#{datetime}][#{progname}][#{severity}][#{Datadog.tracer.active_correlation}] #{msg}\n" + end +end + +begin + require 'ddtrace/auto_instrument' + datadog_config(log) +rescue LoadError + log.info "Datadog's tracing client not found, skipping..." +end + +Optica.set :logger, log Optica.set :store, store Optica.set :events, events Optica.set :ip_check, ip_check +# start the app log.info "Starting sinatra server..." run Optica diff --git a/optica.rb b/optica.rb index 2b119d1..4f5be44 100644 --- a/optica.rb +++ b/optica.rb @@ -3,6 +3,10 @@ require 'oj' class Optica < Sinatra::Base + before do + env['rack.logger'] = settings.logger + end + configure :production, :development do enable :logging end