Skip to content

Simplify implementation of telementry's events #7280

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

Merged
merged 8 commits into from
Apr 17, 2025
Merged

Conversation

IvanGoncharov
Copy link
Contributor

@IvanGoncharov IvanGoncharov commented Apr 16, 2025

I made a few attempts to make minimal fixes based on #7231. However, it was extremely hard to cover all edge cases, so I decided to do a separate refactoring PR first.

The core issue is that currently, some on_request/on_response functions exit earlier:

if self.request.level() != EventLevel::Off {
if let Some(condition) = self.request.condition() {
if condition.lock().evaluate_request(request) != Some(true) {
return;

And because of it, bypass response and custom events:

if self.response.level() != EventLevel::Off {
request
.context
.extensions()
.with_lock(|ext| ext.insert(DisplayRouterResponse(true)));
}
for custom_event in &self.custom {
custom_event.on_request(request);
}

Simply removing returns and implementing logic using ifs also doesn't work:
https://github.com/apollographql/router/pull/7231/files#diff-fab80b4af0ee70850af9ac42bf7990d09912b65f75b39456c6e47ca817d4a0d2R262-R269

It results in the standard event without the condition being ignored.
Trying to address it with a more complicated condition would work, but it would require adding it to more than a dozen places and also doesn't guarantee that we don't break some other edge cases.


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Tests added and passing3
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

Note any exceptions here

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Apr 16, 2025

✅ Docs preview ready

The preview is ready to be viewed. View the preview

File Changes

0 new, 6 changed, 0 removed
* graphos/routing/(latest)/observability/telemetry/instrumentation/events.mdx
* graphos/routing/(latest)/observability/telemetry/log-exporters/overview.mdx
* graphos/routing/(latest)/observability/telemetry/metrics-exporters/overview.mdx
* graphos/routing/(latest)/observability/telemetry/trace-exporters/overview.mdx
* graphos/routing/(latest)/security/jwt.mdx
* graphos/routing/(latest)/security/authorization.mdx

Build ID: a75f02e6fe7a2205f586a04d

URL: https://www.apollographql.com/docs/deploy-preview/a75f02e6fe7a2205f586a04d

Copy link
Contributor

@IvanGoncharov, please consider creating a changeset entry in /.changesets/. These instructions describe the process and tooling.

@IvanGoncharov IvanGoncharov changed the title Check telemetry events on all stages of pipeline Simplify implementation of telementry's events Apr 16, 2025
@@ -2800,7 +2801,7 @@ expression: "&schema"
},
"type": "object"
},
"EventLevel": {
"EventLevelConfig": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whole change is caused by my crating separate enum type just for config

}),
}),
})
.filter_map(|(name, config)| CustomEvent::from_config(name, config))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are very repetitive, so I just moved the code to from_config methods. We can't use From or TryFrom trait here since this method returns options.
I would appreciate suggestions on how to name it better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could still use TryFrom and convert it into an option using .ok()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryFrom returns Result: https://doc.rust-lang.org/std/convert/trait.TryFrom.html
So it will require us to define some dummy error type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically we return None if event is Off
So it's not an error

type EventResponse = ();

fn on_request(&self, request: &Self::Request) {
impl CustomEvents<ConnectorRequest, ConnectorResponse, (), ConnectorAttributes, ConnectorSelector> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the Instrumented trait from here, and it worked without any other code modifications.
Doing this allowed me to change on_request so it now gets a mutable reference.

.context
.extensions()
.with_lock(|lock| lock.insert(ConnectorEventRequest(self.request.clone())));
if let Some(request_event) = self.request.take() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use Option::take so event is basically moved into context and we don't need to call clone here.

request.context.extensions().with_lock(|lock| {
lock.insert(ConnectorEventRequest {
level: request_event.level,
condition: Arc::new(Mutex::new(request_event.condition)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted earlier I don't think we really need this wrapping, but I don't have time to check so I move it here just to be 100% sure I'm not introducing regression here.

}

for custom_event in &self.custom {
for custom_event in &mut self.custom {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is mutable now because I removed the internal wrapping of Arc + Mutex around the condition.

custom_event.on_request(request);
}
}

fn on_response(&self, response: &Self::Response) {
for custom_event in &self.custom {
pub(crate) fn on_response(&mut self, response: &ConnectorResponse) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to make it public since I removed trait

pub(super) error: StandardEvent<Sel>,
pub(super) request: Option<StandardEvent<Sel>>,
pub(super) response: Option<StandardEvent<Sel>>,
pub(super) error: Option<StandardEvent<Sel>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I normalize data here, previously we always had these values but internally they could have EventLevel::Off and this check was noisy.
So I removed Off from EventLevel and just set None instead.

pub(super) error: StandardEvent<Sel>,
pub(super) request: Option<StandardEvent<Sel>>,
pub(super) response: Option<StandardEvent<Sel>>,
pub(super) error: Option<StandardEvent<Sel>>,
pub(super) custom: Vec<CustomEvent<Request, Response, EventResponse, Attributes, Sel>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it removes useless code with custom events, because custom is filtered to not have events that are Off but because EventLevel contained this value it resulted in useless checks

if condition.lock().evaluate_request(request) != Some(true) {
return;
}
impl CustomEvents<router::Request, router::Response, (), RouterAttributes, RouterSelector> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also removed Instrumented here and it just required me to add pub(crate) to methods

pub(crate) fn on_response(&mut self, response: &router::Response) {
if let Some(response_event) = &mut self.response {
if !response_event.condition.evaluate_response(response) {
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this PR doesn't fix early exit issues I talked about in PR description.
I will fix them in separate PR

Self::Conditional { level, .. } => *level,
}
}
#[derive(Debug)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: It doesn't contain Clone, previously it was done by wrapping condition into Arc + Mutex, but I changed the code to maintain a single copy inside telemetry plugin.
I still wrap the condition in Arc + Mutex when the condition is passed outside of the telemetry plugin.

Info,
Warn,
Error,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main purpose of adding it is to remove Off

@@ -706,110 +649,94 @@ where
pub(super) level: EventLevel,
pub(super) event_on: EventOn,
pub(super) message: Arc<String>,
pub(super) selectors: Option<Arc<Extendable<A, T>>>,
pub(super) selectors: Arc<Extendable<A, T>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why it was wrapped in Option, but it caused issues with my changes so I just removed it.

pub(super) inner: Mutex<CustomEventInner<Request, Response, EventResponse, A, T>>,
}

pub(super) struct CustomEventInner<Request, Response, EventResponse, A, T>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed "inner" since we no longer need Mutex, we maintain a single copy and mutate it directly.

@@ -727,7 +726,7 @@ connector:

error!(http.method = "GET", "Hello from test");

let router_events = event_config.new_router_events();
let mut router_events = event_config.new_router_events();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything became explicitly mutable.
Previously it also was mutable but hidden under internal Arc + Mutex

@@ -1,9 +1,6 @@
telemetry:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was using this test as a basis for my new test and cleaned up some stuff in a process

let _ = router.execute_query(Query::builder().body(json!({"query":"query {topProducts{name, name, name, name, name, name, name, name, name, name}}","variables":{}})).header("id_from_header".to_string(), trace_id.to_string()).build()).await;
let query = Query::builder()
.header("id_from_header".to_string(), trace_id.to_string())
.build();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we send default query here, just attach custom header to it

@IvanGoncharov IvanGoncharov marked this pull request as ready for review April 16, 2025 14:13
@IvanGoncharov IvanGoncharov requested a review from a team April 16, 2025 14:13
@IvanGoncharov IvanGoncharov requested a review from a team as a code owner April 16, 2025 14:13
}),
}),
})
.filter_map(|(name, config)| CustomEvent::from_config(name, config))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could still use TryFrom and convert it into an option using .ok()

return;
}
pub(crate) fn on_error(&mut self, error: &BoxError, ctx: &Context) {
if let Some(error_event) = &mut self.error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we call .take() here like in request ? Just a question I'm not sure it's needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we can, if we sure it's the last call (which it probably is).
But I use take more as a hack to move event outside of this plugin.
In this case error event is handled inside so &mut is enough.

Copy link
Contributor

@BrynCooke BrynCooke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It's great to have events standardized

@IvanGoncharov IvanGoncharov enabled auto-merge (squash) April 17, 2025 13:35
@IvanGoncharov IvanGoncharov merged commit e7d8e7b into dev Apr 17, 2025
15 checks passed
@IvanGoncharov IvanGoncharov deleted the i1g/fix_events branch April 17, 2025 13:48
@IvanGoncharov
Copy link
Contributor Author

@mergify backport 1.x

Copy link
Contributor

mergify bot commented Apr 18, 2025

backport 1.x

✅ Backports have been created

IvanGoncharov added a commit that referenced this pull request Apr 18, 2025
(cherry picked from commit e7d8e7b)

# Conflicts:
#	apollo-router/src/configuration/snapshots/apollo_router__configuration__tests__schema_generation.snap
#	apollo-router/src/plugins/connectors/handle_responses.rs
#	apollo-router/src/plugins/telemetry/config_new/connector/events.rs
#	apollo-router/src/plugins/telemetry/config_new/events.rs
#	apollo-router/src/plugins/telemetry/fmt_layer.rs
#	apollo-router/src/plugins/telemetry/mod.rs
#	apollo-router/src/services/connector/request_service.rs
@router-perf
Copy link

router-perf bot commented Apr 20, 2025

CI performance tests

  • connectors-const - Connectors stress test that runs with a constant number of users
  • const - Basic stress test that runs with a constant number of users
  • demand-control-instrumented - A copy of the step test, but with demand control monitoring and metrics enabled
  • demand-control-uninstrumented - A copy of the step test, but with demand control monitoring enabled
  • enhanced-signature - Enhanced signature enabled
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • events_big_cap_high_rate_callback - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity using callback mode
  • events_callback - Stress test for events with a lot of users and deduplication ENABLED in callback mode
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • events_without_dedup_callback - Stress test for events with a lot of users and deduplication DISABLED using callback mode
  • extended-reference-mode - Extended reference mode enabled
  • large-request - Stress test with a 1 MB request payload
  • no-tracing - Basic stress test, no tracing
  • reload - Reload test over a long period of time at a constant rate of users
  • step-jemalloc-tuning - Clone of the basic stress test for jemalloc tuning
  • step-local-metrics - Field stats that are generated from the router rather than FTV1
  • step-with-prometheus - A copy of the step test with the Prometheus metrics exporter enabled
  • step - Basic stress test that steps up the number of users over time
  • xlarge-request - Stress test with 10 MB request payload
  • xxlarge-request - Stress test with 100 MB request payload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants