|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +require_relative '../../command_serializer' |
| 8 | + |
| 9 | +module OpenTelemetry |
| 10 | + module Instrumentation |
| 11 | + module Mongo |
| 12 | + module Subscribers |
| 13 | + module Dup |
| 14 | + # Event handler class for Mongo Ruby driver (dup mode - emits both old and new semantic conventions) |
| 15 | + class Subscriber |
| 16 | + THREAD_KEY = :__opentelemetry_mongo_spans__ |
| 17 | + |
| 18 | + def started(event) |
| 19 | + # start a trace and store it in the current thread; using the `operation_id` |
| 20 | + # is safe since it's a unique id used to link events together. Also only one |
| 21 | + # thread is involved in this execution so thread-local storage should be safe. Reference: |
| 22 | + # https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring.rb#L70 |
| 23 | + # https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring/publishable.rb#L38-L56 |
| 24 | + |
| 25 | + collection = get_collection(event.command) |
| 26 | + |
| 27 | + # Old conventions |
| 28 | + attributes = { |
| 29 | + 'db.system' => 'mongodb', |
| 30 | + 'db.name' => event.database_name, |
| 31 | + 'db.operation' => event.command_name, |
| 32 | + 'net.peer.name' => event.address.host, |
| 33 | + 'net.peer.port' => event.address.port |
| 34 | + } |
| 35 | + |
| 36 | + # New stable conventions |
| 37 | + attributes['db.system.name'] = 'mongodb' |
| 38 | + attributes['db.namespace'] = event.database_name |
| 39 | + attributes['db.operation.name'] = event.command_name |
| 40 | + attributes['server.address'] = event.address.host |
| 41 | + attributes['server.port'] = event.address.port |
| 42 | + |
| 43 | + config = Mongo::Instrumentation.instance.config |
| 44 | + attributes['peer.service'] = config[:peer_service] if config[:peer_service] |
| 45 | + omit = config[:db_statement] == :omit |
| 46 | + obfuscate = config[:db_statement] == :obfuscate |
| 47 | + unless omit |
| 48 | + serialized = CommandSerializer.new(event.command, obfuscate).serialize |
| 49 | + # Both old and new attributes |
| 50 | + attributes['db.statement'] = serialized |
| 51 | + attributes['db.query.text'] = serialized |
| 52 | + end |
| 53 | + if collection |
| 54 | + # Both old and new attributes |
| 55 | + attributes['db.mongodb.collection'] = collection |
| 56 | + attributes['db.collection.name'] = collection |
| 57 | + end |
| 58 | + attributes.compact! |
| 59 | + |
| 60 | + span = tracer.start_span(span_name(collection, event.command_name), attributes: attributes, kind: :client) |
| 61 | + set_span(event, span) |
| 62 | + end |
| 63 | + |
| 64 | + def failed(event) |
| 65 | + finish_event('failed', event) do |span| |
| 66 | + if event.is_a?(::Mongo::Monitoring::Event::CommandFailed) |
| 67 | + error_type = extract_error_type(event) |
| 68 | + # New stable convention attributes |
| 69 | + span.set_attribute('error.type', error_type) |
| 70 | + status_code = event.failure['code'] |
| 71 | + span.set_attribute('db.response.status_code', status_code.to_s) if status_code |
| 72 | + span.add_event('exception', |
| 73 | + attributes: { |
| 74 | + 'exception.type' => 'CommandFailed', |
| 75 | + 'exception.message' => event.message |
| 76 | + }) |
| 77 | + span.status = OpenTelemetry::Trace::Status.error(event.message) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + def succeeded(event) |
| 83 | + finish_event('succeeded', event) |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def finish_event(name, event) |
| 89 | + span = get_span(event) |
| 90 | + return unless span |
| 91 | + |
| 92 | + yield span if block_given? |
| 93 | + rescue StandardError => e |
| 94 | + OpenTelemetry.logger.debug("error when handling MongoDB '#{name}' event: #{e}") |
| 95 | + ensure |
| 96 | + # finish span to prevent leak and remove it from thread storage |
| 97 | + span&.finish |
| 98 | + clear_span(event) |
| 99 | + end |
| 100 | + |
| 101 | + def span_name(collection, command_name) |
| 102 | + return command_name unless collection |
| 103 | + |
| 104 | + "#{command_name} #{collection}" |
| 105 | + end |
| 106 | + |
| 107 | + def get_collection(command) |
| 108 | + collection = command.values.first |
| 109 | + collection if collection.is_a?(String) |
| 110 | + end |
| 111 | + |
| 112 | + def get_span(event) |
| 113 | + Thread.current[THREAD_KEY]&.[](event.request_id) |
| 114 | + end |
| 115 | + |
| 116 | + def set_span(event, span) |
| 117 | + Thread.current[THREAD_KEY] ||= {} |
| 118 | + Thread.current[THREAD_KEY][event.request_id] = span |
| 119 | + end |
| 120 | + |
| 121 | + def clear_span(event) |
| 122 | + Thread.current[THREAD_KEY]&.delete(event.request_id) |
| 123 | + end |
| 124 | + |
| 125 | + def extract_error_type(event) |
| 126 | + event.failure['codeName'] || 'CommandFailed' |
| 127 | + end |
| 128 | + |
| 129 | + def tracer |
| 130 | + Mongo::Instrumentation.instance.tracer |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments