Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to alias standard attributes for telemetry #5957

Merged
merged 8 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

510 changes: 328 additions & 182 deletions apollo-router/src/plugins/telemetry/config_new/attributes.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use schemars::JsonSchema;
use serde::Deserialize;
use tower::BoxError;

use crate::plugins::telemetry::config_new::attributes::StandardAttribute;
use crate::plugins::telemetry::config_new::DefaultAttributeRequirementLevel;
use crate::plugins::telemetry::config_new::DefaultForLevel;
use crate::plugins::telemetry::config_new::Selectors;
Expand All @@ -15,7 +16,7 @@ use crate::Context;
pub(crate) struct CacheAttributes {
/// Entity type
#[serde(rename = "entity.type")]
pub(crate) entity_type: Option<bool>,
pub(crate) entity_type: Option<StandardAttribute>,
}

impl DefaultForLevel for CacheAttributes {
Expand All @@ -26,7 +27,8 @@ impl DefaultForLevel for CacheAttributes {
) {
if let TelemetryDataKind::Metrics = kind {
if let DefaultAttributeRequirementLevel::Required = requirement_level {
self.entity_type.get_or_insert(false);
self.entity_type
.get_or_insert(StandardAttribute::Bool(false));
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions apollo-router/src/plugins/telemetry/config_new/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ impl Instrumented for CacheInstruments {
inner_cache_hit.selector = Some(Arc::new(SubgraphSelector::StaticField {
r#static: AttributeValue::I64(*hit as i64),
}));
if inner_cache_hit
if let Some(key) = inner_cache_hit
.selectors
.as_ref()
.map(|s| s.attributes.entity_type == Some(true))
.unwrap_or_default()
.and_then(|s| s.attributes.entity_type.as_ref())
.and_then(|a| a.key(ENTITY_TYPE))
{
inner_cache_hit.attributes.push(KeyValue::new(
ENTITY_TYPE,
key,
opentelemetry::Value::String(entity_type.to_string().into()),
));
}
Expand All @@ -118,14 +118,14 @@ impl Instrumented for CacheInstruments {
inner_cache_miss.selector = Some(Arc::new(SubgraphSelector::StaticField {
r#static: AttributeValue::I64(*miss as i64),
}));
if inner_cache_miss
if let Some(key) = inner_cache_miss
.selectors
.as_ref()
.map(|s| s.attributes.entity_type == Some(true))
.unwrap_or_default()
.and_then(|s| s.attributes.entity_type.as_ref())
.and_then(|a| a.key(ENTITY_TYPE))
{
inner_cache_miss.attributes.push(KeyValue::new(
ENTITY_TYPE,
key,
opentelemetry::Value::String(entity_type.to_string().into()),
));
}
Expand Down
41 changes: 29 additions & 12 deletions apollo-router/src/plugins/telemetry/config_new/cost/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use schemars::JsonSchema;
use serde::Deserialize;
use tower::BoxError;

use super::attributes::StandardAttribute;
use super::instruments::Increment;
use super::instruments::StaticInstrument;
use crate::metrics;
Expand Down Expand Up @@ -47,16 +48,16 @@ static COST_DELTA: &str = "cost.delta";
pub(crate) struct SupergraphCostAttributes {
/// The estimated cost of the operation using the currently configured cost model
#[serde(rename = "cost.estimated")]
cost_estimated: Option<bool>,
cost_estimated: Option<StandardAttribute>,
/// The actual cost of the operation using the currently configured cost model
#[serde(rename = "cost.actual")]
cost_actual: Option<bool>,
cost_actual: Option<StandardAttribute>,
/// The delta (estimated - actual) cost of the operation using the currently configured cost model
#[serde(rename = "cost.delta")]
cost_delta: Option<bool>,
cost_delta: Option<StandardAttribute>,
/// The cost result, this is an error code returned by the cost calculation or COST_OK
#[serde(rename = "cost.result")]
cost_result: Option<bool>,
cost_result: Option<StandardAttribute>,
}

impl Selectors for SupergraphCostAttributes {
Expand All @@ -82,17 +83,33 @@ impl Selectors for SupergraphCostAttributes {
.extensions()
.with_lock(|lock| lock.get::<CostContext>().cloned());
if let Some(cost_result) = cost_result {
if let Some(true) = self.cost_estimated {
attrs.push(KeyValue::new("cost.estimated", cost_result.estimated));
if let Some(key) = self
.cost_estimated
.as_ref()
.and_then(|a| a.key(Key::from_static_str("cost.estimated")))
{
attrs.push(KeyValue::new(key, cost_result.estimated));
}
if let Some(true) = self.cost_actual {
attrs.push(KeyValue::new("cost.actual", cost_result.actual));
if let Some(key) = self
.cost_actual
.as_ref()
.and_then(|a| a.key(Key::from_static_str("cost.actual")))
{
attrs.push(KeyValue::new(key, cost_result.actual));
}
if let Some(true) = self.cost_delta {
attrs.push(KeyValue::new("cost.delta", cost_result.delta()));
if let Some(key) = self
.cost_delta
.as_ref()
.and_then(|a| a.key(Key::from_static_str("cost.delta")))
{
attrs.push(KeyValue::new(key, cost_result.delta()));
}
if let Some(true) = self.cost_result {
attrs.push(KeyValue::new("cost.result", cost_result.result));
if let Some(key) = self
.cost_result
.as_ref()
.and_then(|a| a.key(Key::from_static_str("cost.result")))
{
attrs.push(KeyValue::new(key, cost_result.result));
}
}
attrs
Expand Down
11 changes: 6 additions & 5 deletions apollo-router/src/plugins/telemetry/config_new/extendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ mod test {
use crate::plugins::telemetry::config_new::attributes::HttpCommonAttributes;
use crate::plugins::telemetry::config_new::attributes::HttpServerAttributes;
use crate::plugins::telemetry::config_new::attributes::RouterAttributes;
use crate::plugins::telemetry::config_new::attributes::StandardAttribute;
use crate::plugins::telemetry::config_new::attributes::SupergraphAttributes;
use crate::plugins::telemetry::config_new::conditional::Conditional;
use crate::plugins::telemetry::config_new::conditions::Condition;
Expand Down Expand Up @@ -312,8 +313,8 @@ mod test {
extendable_conf.attributes,
SupergraphAttributes {
graphql_document: None,
graphql_operation_name: Some(true),
graphql_operation_type: Some(true),
graphql_operation_name: Some(StandardAttribute::Bool(true)),
graphql_operation_type: Some(StandardAttribute::Bool(true)),
cost: Default::default()
}
);
Expand Down Expand Up @@ -384,12 +385,12 @@ mod test {
trace_id: None,
baggage: None,
common: HttpCommonAttributes {
http_request_method: Some(true),
http_response_status_code: Some(true),
http_request_method: Some(StandardAttribute::Bool(true)),
http_response_status_code: Some(StandardAttribute::Bool(true)),
..Default::default()
},
server: HttpServerAttributes {
url_path: Some(true),
url_path: Some(StandardAttribute::Bool(true)),
..Default::default()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ info:
http.server.active_requests: false
http.server.request.duration:
attributes:
http.request.method:
alias: http_method
my_attribute:
request_method: true
graphql.operation.name:
Expand All @@ -23,6 +25,6 @@ info:
- sum: 0.1
attributes:
graphql.operation.name: TestQuery
http.request.method: GET
http.response.status_code: 200
http_method: GET
my_attribute: GET
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ telemetry:
http.server.active_requests: false
http.server.request.duration:
attributes:
http.request.method:
alias: http_method
my_attribute:
request_method: true
graphql.operation.name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ info:
cache:
apollo.router.operations.entity.cache:
attributes:
entity.type: true
entity.type:
alias: entity_type
subgraph.name:
subgraph_name: true
supergraph.operation.name:
Expand Down Expand Up @@ -63,25 +64,25 @@ info:
- value: 0
attributes:
cache.hit: false
entity.type: Product
entity_type: Product
subgraph.name: products
supergraph.operation.name: Test
- value: 0
attributes:
cache.hit: false
entity.type: Review
entity_type: Review
subgraph.name: products
supergraph.operation.name: Test
- value: 3
attributes:
cache.hit: true
entity.type: Product
entity_type: Product
subgraph.name: products
supergraph.operation.name: Test
- value: 5
attributes:
cache.hit: true
entity.type: Review
entity_type: Review
subgraph.name: products
supergraph.operation.name: Test
- name: only_cache_hit_on_subgraph_products
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ telemetry:
cache:
apollo.router.operations.entity.cache:
attributes:
entity.type: true
entity.type:
alias: entity_type
subgraph.name:
subgraph_name: true
supergraph.operation.name:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: apollo-router/src/plugins/telemetry/config_new/instruments.rs
description: standard instrument http.client.request.duration
expression: "&metrics.all()"
info:
telemetry:
instrumentation:
instruments:
default_requirement_level: none
subgraph:
http.client.request.duration:
attributes:
subgraph.name:
alias: apollo_subgraph_name
---
- name: http.client.request.duration
description: Duration of HTTP client requests.
unit: s
data:
datapoints:
- sum: 0.1
attributes:
apollo_subgraph_name: products
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
telemetry:
instrumentation:
instruments:
default_requirement_level: none
subgraph:
http.client.request.duration:
attributes:
subgraph.name:
alias: apollo_subgraph_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
description: standard instrument http.client.request.duration
events:
- - router_request:
uri: "/hello"
method: GET
body: |
hello
- supergraph_request:
uri: "/hello"
method: GET
headers:
custom_header: custom_value
query: "query { hello }"
- subgraph_request:
query: "query { hello }"
operation_name: "Products"
operation_kind: query
subgraph_name: "products"
- subgraph_response:
status: 200
data:
hello: "world"
- supergraph_response:
status: 200
data:
hello: "world"
- router_response:
body: |
hello
status: 200
Loading
Loading