|
| 1 | +// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Mock implementations of trace agent components for testing |
| 5 | +
|
| 6 | +use datadog_trace_agent::{ |
| 7 | + config::Config, |
| 8 | + env_verifier::EnvVerifier, |
| 9 | + stats_flusher::StatsFlusher, |
| 10 | + stats_processor::StatsProcessor, |
| 11 | + trace_flusher::TraceFlusher, |
| 12 | + trace_processor::TraceProcessor, |
| 13 | +}; |
| 14 | +use libdd_common::hyper_migration; |
| 15 | +use libdd_trace_protobuf::pb; |
| 16 | +use libdd_trace_utils::trace_utils::{self, MiniAgentMetadata, SendData}; |
| 17 | +use std::sync::Arc; |
| 18 | +use tokio::sync::mpsc::{Receiver, Sender}; |
| 19 | + |
| 20 | +/// Mock trace processor that returns 200 OK for all requests |
| 21 | +#[allow(dead_code)] |
| 22 | +pub struct MockTraceProcessor; |
| 23 | + |
| 24 | +#[async_trait::async_trait] |
| 25 | +impl TraceProcessor for MockTraceProcessor { |
| 26 | + async fn process_traces( |
| 27 | + &self, |
| 28 | + _config: Arc<Config>, |
| 29 | + _req: hyper_migration::HttpRequest, |
| 30 | + _trace_tx: Sender<SendData>, |
| 31 | + _mini_agent_metadata: Arc<MiniAgentMetadata>, |
| 32 | + ) -> Result<hyper_migration::HttpResponse, hyper::http::Error> { |
| 33 | + hyper::Response::builder() |
| 34 | + .status(200) |
| 35 | + .body(hyper_migration::Body::from("{}")) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Mock trace flusher that consumes messages without processing |
| 40 | +pub struct MockTraceFlusher; |
| 41 | + |
| 42 | +#[async_trait::async_trait] |
| 43 | +impl TraceFlusher for MockTraceFlusher { |
| 44 | + fn new( |
| 45 | + _aggregator: Arc<tokio::sync::Mutex<datadog_trace_agent::aggregator::TraceAggregator>>, |
| 46 | + _config: Arc<Config>, |
| 47 | + ) -> Self { |
| 48 | + MockTraceFlusher |
| 49 | + } |
| 50 | + |
| 51 | + async fn start_trace_flusher(&self, mut trace_rx: Receiver<SendData>) { |
| 52 | + // Consume messages from the channel without processing them |
| 53 | + while let Some(_trace) = trace_rx.recv().await { |
| 54 | + // Just discard the trace - we're not testing the flusher |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async fn send(&self, _traces: Vec<SendData>) -> Option<Vec<SendData>> { |
| 59 | + None |
| 60 | + } |
| 61 | + |
| 62 | + async fn flush(&self, _failed_traces: Option<Vec<SendData>>) -> Option<Vec<SendData>> { |
| 63 | + None |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Mock stats processor that returns 200 OK for all requests |
| 68 | +pub struct MockStatsProcessor; |
| 69 | + |
| 70 | +#[async_trait::async_trait] |
| 71 | +impl StatsProcessor for MockStatsProcessor { |
| 72 | + async fn process_stats( |
| 73 | + &self, |
| 74 | + _config: Arc<Config>, |
| 75 | + _req: hyper_migration::HttpRequest, |
| 76 | + _stats_tx: Sender<pb::ClientStatsPayload>, |
| 77 | + ) -> Result<hyper_migration::HttpResponse, hyper::http::Error> { |
| 78 | + hyper::Response::builder() |
| 79 | + .status(200) |
| 80 | + .body(hyper_migration::Body::from("{}")) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Mock stats flusher that consumes messages without processing |
| 85 | +pub struct MockStatsFlusher; |
| 86 | + |
| 87 | +#[async_trait::async_trait] |
| 88 | +impl StatsFlusher for MockStatsFlusher { |
| 89 | + async fn start_stats_flusher( |
| 90 | + &self, |
| 91 | + _config: Arc<Config>, |
| 92 | + mut stats_rx: Receiver<pb::ClientStatsPayload>, |
| 93 | + ) { |
| 94 | + // Consume messages from the channel without processing them |
| 95 | + while let Some(_stats) = stats_rx.recv().await { |
| 96 | + // Just discard the stats - we're not testing the flusher |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + async fn flush_stats(&self, _config: Arc<Config>, _traces: Vec<pb::ClientStatsPayload>) { |
| 101 | + // Do nothing |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Mock environment verifier that returns default metadata |
| 106 | +pub struct MockEnvVerifier; |
| 107 | + |
| 108 | +#[async_trait::async_trait] |
| 109 | +impl EnvVerifier for MockEnvVerifier { |
| 110 | + async fn verify_environment( |
| 111 | + &self, |
| 112 | + _timeout_ms: u64, |
| 113 | + _env_type: &trace_utils::EnvironmentType, |
| 114 | + _os: &str, |
| 115 | + ) -> MiniAgentMetadata { |
| 116 | + MiniAgentMetadata::default() |
| 117 | + } |
| 118 | +} |
0 commit comments