|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'data_streams/processor' |
| 4 | +require_relative 'data_streams/pathway_context' |
| 5 | +require_relative 'data_streams/configuration/settings' |
| 6 | +require_relative 'data_streams/extensions' |
| 7 | +require_relative 'core/utils/time' |
| 8 | + |
| 9 | +module Datadog |
| 10 | + # Datadog Data Streams Monitoring public API. |
| 11 | + # |
| 12 | + # The Datadog team ensures that public methods in this module |
| 13 | + # only receive backwards compatible changes, and breaking changes |
| 14 | + # will only occur in new major versions releases. |
| 15 | + # @public_api |
| 16 | + module DataStreams |
| 17 | + class << self |
| 18 | + # Set a produce checkpoint for Data Streams Monitoring |
| 19 | + # |
| 20 | + # @param type [String] The type of the checkpoint (e.g., 'kafka', 'kinesis', 'sns') |
| 21 | + # @param destination [String] The destination (e.g., topic, exchange, stream name) |
| 22 | + # @param auto_instrumentation [Boolean] Whether this checkpoint was set by auto-instrumentation (default: false) |
| 23 | + # @param tags [Hash] Additional tags to include |
| 24 | + # @yield [key, value] Block to inject context into carrier |
| 25 | + # @return [String, nil] Base64 encoded pathway context or nil if disabled |
| 26 | + # @public_api |
| 27 | + def set_produce_checkpoint(type:, destination:, auto_instrumentation: false, tags: {}, &block) |
| 28 | + processor&.set_produce_checkpoint( |
| 29 | + type: type, |
| 30 | + destination: destination, |
| 31 | + manual_checkpoint: !auto_instrumentation, |
| 32 | + tags: tags, |
| 33 | + &block |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + # Set a consume checkpoint for Data Streams Monitoring |
| 38 | + # |
| 39 | + # @param type [String] The type of the checkpoint (e.g., 'kafka', 'kinesis', 'sns') |
| 40 | + # @param source [String] The source (e.g., topic, exchange, stream name) |
| 41 | + # @param auto_instrumentation [Boolean] Whether this checkpoint was set by auto-instrumentation (default: false) |
| 42 | + # @param tags [Hash] Additional tags to include |
| 43 | + # @yield [key] Block to extract context from carrier |
| 44 | + # @return [String, nil] Base64 encoded pathway context or nil if disabled |
| 45 | + # @public_api |
| 46 | + def set_consume_checkpoint(type:, source:, auto_instrumentation: false, tags: {}, &block) |
| 47 | + processor&.set_consume_checkpoint( |
| 48 | + type: type, |
| 49 | + source: source, |
| 50 | + manual_checkpoint: !auto_instrumentation, |
| 51 | + tags: tags, |
| 52 | + &block |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + # Track Kafka produce offset for lag monitoring |
| 57 | + # |
| 58 | + # @param topic [String] The Kafka topic name |
| 59 | + # @param partition [Integer] The partition number |
| 60 | + # @param offset [Integer] The offset of the produced message |
| 61 | + # @return [Boolean, nil] true if tracking succeeded, nil if disabled |
| 62 | + # @!visibility private |
| 63 | + def track_kafka_produce(topic, partition, offset) |
| 64 | + processor&.track_kafka_produce(topic, partition, offset, Core::Utils::Time.now) |
| 65 | + end |
| 66 | + |
| 67 | + # Track Kafka message consumption for consumer lag monitoring |
| 68 | + # |
| 69 | + # @param topic [String] The Kafka topic name |
| 70 | + # @param partition [Integer] The partition number |
| 71 | + # @param offset [Integer] The offset of the consumed message |
| 72 | + # @return [Boolean, nil] true if tracking succeeded, nil if disabled |
| 73 | + # @!visibility private |
| 74 | + def track_kafka_consume(topic, partition, offset) |
| 75 | + processor&.track_kafka_consume(topic, partition, offset, Core::Utils::Time.now) |
| 76 | + end |
| 77 | + |
| 78 | + # Check if Data Streams Monitoring is enabled and available |
| 79 | + # |
| 80 | + # @return [Boolean] true if the processor is available |
| 81 | + # @public_api |
| 82 | + def enabled? |
| 83 | + !processor.nil? |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def processor |
| 89 | + components.data_streams |
| 90 | + end |
| 91 | + |
| 92 | + def components |
| 93 | + Datadog.send(:components) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + # Expose Data Streams to global shared objects |
| 98 | + Extensions.activate! |
| 99 | + end |
| 100 | +end |
0 commit comments