apollo-router 1.61.13

A configurable, high-performance routing runtime for Apollo Federation 🚀
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Configuration for apollo telemetry.
use std::collections::HashMap;
use std::fmt::Display;
use std::num::NonZeroUsize;
use std::ops::AddAssign;
use std::sync::OnceLock;
use std::time::SystemTime;

use http::header::HeaderName;
use itertools::Itertools;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use serde::ser::SerializeMap;
use url::Url;
use uuid::Uuid;

use super::config::ApolloMetricsReferenceMode;
use super::config::ApolloSignatureNormalizationAlgorithm;
use super::config::Sampler;
use super::metrics::apollo::studio::ContextualizedStats;
use super::metrics::apollo::studio::SingleStats;
use super::metrics::apollo::studio::SingleStatsReport;
use super::otlp::Protocol;
use super::tracing::apollo::TracesReport;
use crate::plugin::serde::deserialize_header_name;
use crate::plugin::serde::deserialize_vec_header_name;
use crate::plugins::telemetry::apollo_exporter::proto::reports::ReferencedFieldsForType;
use crate::plugins::telemetry::apollo_exporter::proto::reports::ReportHeader;
use crate::plugins::telemetry::apollo_exporter::proto::reports::StatsContext;
use crate::plugins::telemetry::apollo_exporter::proto::reports::Trace;
use crate::plugins::telemetry::config::SamplerOption;
use crate::plugins::telemetry::tracing::BatchProcessorConfig;
use crate::query_planner::OperationKind;
use crate::services::apollo_graph_reference;
use crate::services::apollo_key;

pub(crate) const ENDPOINT_DEFAULT: &str =
    "https://usage-reporting.api.apollographql.com/api/ingress/traces";

pub(crate) const OTLP_ENDPOINT_DEFAULT: &str = "https://usage-reporting.api.apollographql.com";

// Random unique UUID for the Router. This doesn't actually identify the router, it just allows disambiguation between multiple routers with the same metadata.
static ROUTER_ID: OnceLock<Uuid> = OnceLock::new();

/// Returns the current unique UUID for this instance of the Router.
pub(crate) fn router_id() -> String {
    ROUTER_ID.get_or_init(Uuid::new_v4).to_string()
}

#[derive(Clone, Deserialize, JsonSchema, Debug)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct Config {
    /// The Apollo Studio endpoint for exporting traces and metrics.
    #[schemars(with = "String", default = "endpoint_default")]
    pub(crate) endpoint: Url,

    /// The Apollo Studio endpoint for exporting traces and metrics.
    #[schemars(with = "String", default = "otlp_endpoint_default")]
    pub(crate) experimental_otlp_endpoint: Url,

    /// The Apollo Studio API key.
    #[schemars(skip)]
    pub(crate) apollo_key: Option<String>,

    /// The Apollo Studio graph reference.
    #[schemars(skip)]
    pub(crate) apollo_graph_ref: Option<String>,

    /// The name of the header to extract from requests when populating 'client name' for traces and metrics in Apollo Studio.
    #[schemars(with = "Option<String>", default = "client_name_header_default_str")]
    #[serde(deserialize_with = "deserialize_header_name")]
    pub(crate) client_name_header: HeaderName,

    /// The name of the header to extract from requests when populating 'client version' for traces and metrics in Apollo Studio.
    #[schemars(with = "Option<String>", default = "client_version_header_default_str")]
    #[serde(deserialize_with = "deserialize_header_name")]
    pub(crate) client_version_header: HeaderName,

    /// The buffer size for sending traces to Apollo. Increase this if you are experiencing lost traces.
    pub(crate) buffer_size: NonZeroUsize,

    /// Field level instrumentation for subgraphs via ftv1. ftv1 tracing can cause performance issues as it is transmitted in band with subgraph responses.
    pub(crate) field_level_instrumentation_sampler: SamplerOption,

    /// Percentage of traces to send via the OTel protocol when sending to Apollo Studio.
    pub(crate) otlp_tracing_sampler: SamplerOption,

    /// OTLP protocol used for OTel traces.
    /// Note this only applies if OTel traces are enabled and is only intended for use in tests.
    pub(crate) experimental_otlp_tracing_protocol: Protocol,

    /// To configure which request header names and values are included in trace data that's sent to Apollo Studio.
    pub(crate) send_headers: ForwardHeaders,
    /// To configure which GraphQL variable values are included in trace data that's sent to Apollo Studio
    pub(crate) send_variable_values: ForwardValues,

    // This'll get overridden if a user tries to set it.
    // The purpose is to allow is to pass this in to the plugin.
    #[schemars(skip)]
    pub(crate) schema_id: String,

    /// Configuration for batch processing.
    pub(crate) batch_processor: BatchProcessorConfig,

    /// Configure the way errors are transmitted to Apollo Studio
    pub(crate) errors: ErrorsConfiguration,

    /// Set the signature normalization algorithm to use when sending Apollo usage reports.
    pub(crate) signature_normalization_algorithm: ApolloSignatureNormalizationAlgorithm,

    /// Set the Apollo usage report reference reporting mode to use.
    pub(crate) metrics_reference_mode: ApolloMetricsReferenceMode,

    /// Enable field metrics that are generated without FTV1 to be sent to Apollo Studio.
    pub(crate) experimental_local_field_metrics: bool,
}

#[derive(Debug, Clone, Deserialize, JsonSchema, Default)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct ErrorsConfiguration {
    /// Handling of errors coming from subgraph
    pub(crate) subgraph: SubgraphErrorConfig,
}

#[derive(Debug, Clone, Deserialize, JsonSchema, Default)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct SubgraphErrorConfig {
    /// Handling of errors coming from all subgraphs
    pub(crate) all: ErrorConfiguration,
    /// Handling of errors coming from specified subgraphs
    pub(crate) subgraphs: HashMap<String, ErrorConfiguration>,
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct ErrorConfiguration {
    /// Send subgraph errors to Apollo Studio
    pub(crate) send: bool,
    /// Redact subgraph errors to Apollo Studio
    pub(crate) redact: bool,
}

impl Default for ErrorConfiguration {
    fn default() -> Self {
        Self {
            send: true,
            redact: true,
        }
    }
}

impl SubgraphErrorConfig {
    pub(crate) fn get_error_config(&self, subgraph: &str) -> &ErrorConfiguration {
        if let Some(subgraph_conf) = self.subgraphs.get(subgraph) {
            subgraph_conf
        } else {
            &self.all
        }
    }
}

const fn default_field_level_instrumentation_sampler() -> SamplerOption {
    SamplerOption::TraceIdRatioBased(0.01)
}

const fn default_otlp_tracing_sampler() -> SamplerOption {
    SamplerOption::Always(Sampler::AlwaysOff)
}

fn endpoint_default() -> Url {
    Url::parse(ENDPOINT_DEFAULT).expect("must be valid url")
}

fn otlp_endpoint_default() -> Url {
    Url::parse(OTLP_ENDPOINT_DEFAULT).expect("must be valid url")
}

const fn client_name_header_default_str() -> &'static str {
    "apollographql-client-name"
}

const fn client_name_header_default() -> HeaderName {
    HeaderName::from_static(client_name_header_default_str())
}

const fn client_version_header_default_str() -> &'static str {
    "apollographql-client-version"
}

const fn client_version_header_default() -> HeaderName {
    HeaderName::from_static(client_version_header_default_str())
}

pub(crate) const fn default_buffer_size() -> NonZeroUsize {
    unsafe { NonZeroUsize::new_unchecked(10000) }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            endpoint: endpoint_default(),
            experimental_otlp_endpoint: otlp_endpoint_default(),
            experimental_otlp_tracing_protocol: Protocol::default(),
            apollo_key: apollo_key(),
            apollo_graph_ref: apollo_graph_reference(),
            client_name_header: client_name_header_default(),
            client_version_header: client_version_header_default(),
            schema_id: "<no_schema_id>".to_string(),
            buffer_size: default_buffer_size(),
            field_level_instrumentation_sampler: default_field_level_instrumentation_sampler(),
            otlp_tracing_sampler: default_otlp_tracing_sampler(),
            send_headers: ForwardHeaders::None,
            send_variable_values: ForwardValues::None,
            batch_processor: BatchProcessorConfig::default(),
            errors: ErrorsConfiguration::default(),
            signature_normalization_algorithm: ApolloSignatureNormalizationAlgorithm::default(),
            experimental_local_field_metrics: false,
            metrics_reference_mode: ApolloMetricsReferenceMode::default(),
        }
    }
}

schemar_fn!(
    forward_headers_only,
    Vec<String>,
    "Send only the headers specified"
);
schemar_fn!(
    forward_headers_except,
    Vec<String>,
    "Send all headers except those specified"
);

/// Forward headers
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub(crate) enum ForwardHeaders {
    /// Don't send any headers
    None,

    /// Send all headers
    All,

    /// Send only the headers specified
    #[schemars(schema_with = "forward_headers_only")]
    #[serde(deserialize_with = "deserialize_vec_header_name")]
    Only(Vec<HeaderName>),

    /// Send all headers except those specified
    #[schemars(schema_with = "forward_headers_except")]
    #[serde(deserialize_with = "deserialize_vec_header_name")]
    Except(Vec<HeaderName>),
}

impl Default for ForwardHeaders {
    fn default() -> Self {
        Self::None
    }
}

schemar_fn!(
    forward_variables_except,
    Vec<String>,
    "Send all variables except those specified"
);

schemar_fn!(
    forward_variables_only,
    Vec<String>,
    "Send only the variables specified"
);

/// Forward GraphQL variables
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub(crate) enum ForwardValues {
    /// Dont send any variables
    None,
    /// Send all variables
    All,
    /// Send only the variables specified
    #[schemars(schema_with = "forward_variables_only")]
    Only(Vec<String>),
    /// Send all variables except those specified
    #[schemars(schema_with = "forward_variables_except")]
    Except(Vec<String>),
}

impl Default for ForwardValues {
    fn default() -> Self {
        Self::None
    }
}

#[derive(Debug, Serialize)]
pub(crate) enum SingleReport {
    Stats(SingleStatsReport),
    Traces(TracesReport),
}

#[derive(Default, Debug, Serialize)]
pub(crate) struct Report {
    pub(crate) traces_per_query: HashMap<String, TracesAndStats>,
    #[serde(serialize_with = "serialize_licensed_operation_count_by_type")]
    pub(crate) licensed_operation_count_by_type:
        HashMap<(OperationKind, Option<OperationSubType>), LicensedOperationCountByType>,
}

#[derive(Clone, Default, Debug, Serialize, PartialEq, Eq, Hash)]
pub(crate) struct LicensedOperationCountByType {
    pub(crate) r#type: OperationKind,
    pub(crate) subtype: Option<OperationSubType>,
    pub(crate) licensed_operation_count: u64,
}

#[derive(Debug, Serialize, PartialEq, Eq, Hash, Clone, Copy)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum OperationSubType {
    SubscriptionEvent,
    SubscriptionRequest,
}

impl OperationSubType {
    pub(crate) const fn as_str(&self) -> &'static str {
        match self {
            OperationSubType::SubscriptionEvent => "subscription-event",
            OperationSubType::SubscriptionRequest => "subscription-request",
        }
    }
}

impl Display for OperationSubType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OperationSubType::SubscriptionEvent => write!(f, "subscription-event"),
            OperationSubType::SubscriptionRequest => write!(f, "subscription-request"),
        }
    }
}

impl From<LicensedOperationCountByType>
    for crate::plugins::telemetry::apollo_exporter::proto::reports::report::OperationCountByType
{
    fn from(value: LicensedOperationCountByType) -> Self {
        Self {
            r#type: value.r#type.as_apollo_operation_type().to_string(),
            subtype: value.subtype.map(|s| s.to_string()).unwrap_or_default(),
            operation_count: value.licensed_operation_count,
        }
    }
}

fn serialize_licensed_operation_count_by_type<S>(
    elt: &HashMap<(OperationKind, Option<OperationSubType>), LicensedOperationCountByType>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let mut map_ser = serializer.serialize_map(Some(elt.len()))?;
    for ((op_type, op_subtype), v) in elt {
        map_ser.serialize_entry(
            &format!(
                "{}{}",
                op_type.as_apollo_operation_type(),
                op_subtype
                    .map(|o| "/".to_owned() + o.as_str())
                    .unwrap_or_default()
            ),
            v,
        )?;
    }
    map_ser.end()
}

impl Report {
    #[cfg(test)]
    pub(crate) fn new(reports: Vec<SingleStatsReport>) -> Report {
        let mut aggregated_report = Report::default();
        for report in reports {
            aggregated_report += report;
        }
        aggregated_report
    }

    pub(crate) fn build_proto_report(
        &self,
        header: ReportHeader,
        extended_references_enabled: bool,
    ) -> crate::plugins::telemetry::apollo_exporter::proto::reports::Report {
        let mut report = crate::plugins::telemetry::apollo_exporter::proto::reports::Report {
            header: Some(header),
            end_time: Some(SystemTime::now().into()),
            operation_count_by_type: self
                .licensed_operation_count_by_type
                .values()
                .cloned()
                .map(|op| op.into())
                .collect(),
            traces_pre_aggregated: true,
            extended_references_enabled,
            ..Default::default()
        };

        for (key, traces_and_stats) in &self.traces_per_query {
            report
                .traces_per_query
                .insert(key.clone(), traces_and_stats.clone().into());
        }

        report
    }
}

impl AddAssign<SingleReport> for Report {
    fn add_assign(&mut self, report: SingleReport) {
        match report {
            SingleReport::Stats(stats) => self.add_assign(stats),
            SingleReport::Traces(traces) => self.add_assign(traces),
        }
    }
}

impl AddAssign<TracesReport> for Report {
    fn add_assign(&mut self, report: TracesReport) {
        // Note that operation count is dealt with in metrics so we don't increment this.
        for (operation_signature, trace) in report.traces {
            self.traces_per_query
                .entry(operation_signature)
                .or_default()
                .traces
                .push(trace);
        }
    }
}

impl AddAssign<SingleStatsReport> for Report {
    fn add_assign(&mut self, report: SingleStatsReport) {
        for (k, v) in report.stats {
            *self.traces_per_query.entry(k).or_default() += v;
        }

        if let Some(licensed_operation_count_by_type) = report.licensed_operation_count_by_type {
            let key = (
                licensed_operation_count_by_type.r#type,
                licensed_operation_count_by_type.subtype,
            );
            self.licensed_operation_count_by_type
                .entry(key)
                .and_modify(|e| {
                    e.licensed_operation_count += 1;
                })
                .or_insert(licensed_operation_count_by_type);
        }
    }
}

#[derive(Clone, Default, Debug, Serialize)]
pub(crate) struct TracesAndStats {
    pub(crate) traces: Vec<Trace>,
    #[serde(with = "vectorize")]
    pub(crate) stats_with_context: HashMap<StatsContext, ContextualizedStats>,
    pub(crate) referenced_fields_by_type: HashMap<String, ReferencedFieldsForType>,
}

impl From<TracesAndStats>
    for crate::plugins::telemetry::apollo_exporter::proto::reports::TracesAndStats
{
    fn from(stats: TracesAndStats) -> Self {
        Self {
            stats_with_context: stats.stats_with_context.into_values().map_into().collect(),
            referenced_fields_by_type: stats.referenced_fields_by_type,
            trace: stats.traces,
            ..Default::default()
        }
    }
}

impl AddAssign<SingleStats> for TracesAndStats {
    fn add_assign(&mut self, stats: SingleStats) {
        *self
            .stats_with_context
            .entry(stats.stats_with_context.context.clone())
            .or_default() += stats.stats_with_context;

        // No merging required here because references fields by type will always be the same for each stats report key.
        self.referenced_fields_by_type = stats.referenced_fields_by_type;
    }
}

pub(crate) mod vectorize {
    use serde::Serialize;
    use serde::Serializer;

    pub(crate) fn serialize<'a, T, K, V, S>(target: T, ser: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        T: IntoIterator<Item = (&'a K, &'a V)>,
        K: Serialize + 'a,
        V: Serialize + 'a,
    {
        let container: Vec<_> = target.into_iter().collect();
        serde::Serialize::serialize(&container, ser)
    }
}