# frozen_string_literal: true module Minato module Trace module Middleware class IntegrateWithCloudLogging def call(log) return log unless ::Rails.env.production? && Minato::Trace.enabled? add_trace_to_logs(log) end private def add_trace_to_logs(log) return log if current_cloud_trace.nil? trace_log = "projects/#{current_cloud_trace.trace.project_id}/traces/#{current_cloud_trace.trace.trace_id}" log.merge({ "logging.googleapis.com/trace": trace_log, "logging.googleapis.com/spanId": current_cloud_trace.span_id.to_s }) end def current_cloud_trace @current_cloud_trace ||= current_cloud_trace_from_google_cloud end def current_cloud_trace_from_google_cloud _trace_client = Google::Cloud::Trace.new Google::Cloud::Trace.get end end end end end