Skip to content

Commit

Permalink
misc(tracing) - Add custom spans to Services & GraphQLController (get…
Browse files Browse the repository at this point in the history
…lago#2357)

## Description

Improve tracing by adding spans to high-level calls. Currently this will
add tracing to all `.call` calls on Service and Query objects.
  • Loading branch information
nudded authored and abdussamadbello committed Aug 8, 2024
1 parent afc1abf commit 7035b63
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ def execute
current_user&.memberships&.find_by(organization: current_organization)&.permissions_hash ||
Permission::EMPTY_PERMISSIONS_HASH
}
result = LagoApiSchema.execute(query, variables:, context:, operation_name:)

OpenTelemetry::Trace.current_span.add_attributes({"query" => query, "operation_name" => operation_name})
result = LagoTracer.in_span("LagoApiSchema.execute") do
LagoApiSchema.execute(query, variables:, context:, operation_name:)
end

render(json: result)
rescue JWT::ExpiredSignature
render_graphql_error(code: 'expired_jwt_token', status: 401)
Expand Down
4 changes: 0 additions & 4 deletions app/services/applied_coupons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module AppliedCoupons
class CreateService < BaseService
def self.call(...)
new(...).call
end

def initialize(customer:, coupon:, params:)
@customer = customer
@coupon = coupon
Expand Down
8 changes: 6 additions & 2 deletions app/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,15 @@ def raise_if_error!
end

def self.call(*, **, &)
new(*, **).call(&)
LagoTracer.in_span("#{name}#call") do
new(*, **).call(&)
end
end

def self.call_async(*, **, &)
new(*, **).call_async(&)
LagoTracer.in_span("#{name}#call_async") do
new(*, **).call_async(&)
end
end

def initialize(current_user = nil)
Expand Down
4 changes: 3 additions & 1 deletion app/services/events/validate_creation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def initialize(organization:, params:, result:, customer:, subscriptions: [])
end

def call
validate_create
LagoTracer.in_span("Events::ValidateCreationService#call") do
validate_create
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
config.consider_all_requests_local = true
config.server_timing = true

config.cache_store = :redis_cache_store, {url: ENV['LAGO_REDIS_CACHE_URL'], db: '3'}
config.cache_store = :redis_cache_store, {url: ENV['LAGO_REDIS_CACHE_URL'], db: 3}

if Rails.root.join("tmp/caching-dev.txt").exist?
config.public_file_server.headers = {
Expand Down
2 changes: 0 additions & 2 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,4 @@
enable_starttls_auto: true
}
end

OpenTelemetry::SDK.configure(&:use_all) if ENV['OTEL_EXPORTER'].present?
end
8 changes: 8 additions & 0 deletions config/initializers/open_telemetry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/all'

OpenTelemetry::SDK.configure(&:use_all) if ENV['OTEL_EXPORTER'].present?

LagoTracer = OpenTelemetry.tracer_provider.tracer('lago')

0 comments on commit 7035b63

Please sign in to comment.