Module: Paid::Tracing
- Defined in:
- lib/paid_ruby/tracing/logging.rb,
lib/paid_ruby/tracing/tracing.rb,
lib/paid_ruby/tracing/wrappers/open_ai_wrapper.rb
Defined Under Namespace
Constant Summary collapse
- PAID_EXTERNAL_CUSTOMER_ID_KEY =
Context keys to propagate external_customer_id and token to child spans. These are just keys, not the values themselves.
OpenTelemetry::Context.create_key("paid.external_customer_id")
- PAID_TOKEN_KEY =
OpenTelemetry::Context.create_key("paid.token")
Class Method Summary collapse
- .capture(*args, external_customer_id:, &block) ⇒ Object
-
.get_external_customer_id_from_context ⇒ Object
Getter for the OpenAI wrapper to retrieve the external_customer_id from the context.
-
.get_token_from_context ⇒ Object
Getter for the OpenAI wrapper to retrieve the token from the context.
- .initialize_tracing(api_key:) ⇒ Object
- .token ⇒ Object
Class Method Details
.capture(*args, external_customer_id:, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/paid_ruby/tracing/tracing.rb', line 61 def self.capture(*args, external_customer_id:, &block) token = self.token unless token Logging.logger.warn("No token found - tracing is not initialized and will not be captured") return yield(*args) if block_given? end new_context_values = { PAID_EXTERNAL_CUSTOMER_ID_KEY => external_customer_id, PAID_TOKEN_KEY => token } # Execute the block within a new context containing our values. OpenTelemetry::Context.with_values(new_context_values) do tracer = OpenTelemetry.tracer_provider.tracer("paid.ruby") tracer.in_span("paid.ruby:#{external_customer_id}") do |span| span.set_attribute("external_customer_id", external_customer_id) span.set_attribute("token", token) begin result = yield(*args) if block_given? span.status = OpenTelemetry::Trace::Status.ok("Success") result rescue StandardError => e span.status = OpenTelemetry::Trace::Status.error("Error: #{e.}") raise e end end end end |
.get_external_customer_id_from_context ⇒ Object
Getter for the OpenAI wrapper to retrieve the external_customer_id from the context.
49 50 51 |
# File 'lib/paid_ruby/tracing/tracing.rb', line 49 def self.get_external_customer_id_from_context OpenTelemetry::Context.current.value(PAID_EXTERNAL_CUSTOMER_ID_KEY) end |
.get_token_from_context ⇒ Object
Getter for the OpenAI wrapper to retrieve the token from the context.
54 55 56 |
# File 'lib/paid_ruby/tracing/tracing.rb', line 54 def self.get_token_from_context OpenTelemetry::Context.current.value(PAID_TOKEN_KEY) end |
.initialize_tracing(api_key:) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/paid_ruby/tracing/tracing.rb', line 17 def self.initialize_tracing(api_key:) endpoint = "https://collector.agentpaid.io:4318/v1/traces" # endpoint = "http://localhost:4318/v1/traces" @token = api_key exporter = OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: endpoint, headers: {} ) span_processor = OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter) OpenTelemetry::SDK.configure do |c| c.add_span_processor(span_processor) end # Add an at_exit hook to ensure spans are flushed before the script exits. at_exit do OpenTelemetry.tracer_provider.shutdown end Logging.logger.info("Paid tracing initialized successfully") rescue StandardError => e Logging.logger.error("Failed to initialize Paid tracing: #{e.}") raise end |
.token ⇒ Object
44 45 46 |
# File 'lib/paid_ruby/tracing/tracing.rb', line 44 def self.token @token end |