Simplify implementation of telementry's events#7280
Conversation
✅ Docs preview readyThe preview is ready to be viewed. View the preview File Changes 0 new, 6 changed, 0 removedBuild ID: a75f02e6fe7a2205f586a04d URL: https://www.apollographql.com/docs/deploy-preview/a75f02e6fe7a2205f586a04d |
|
@IvanGoncharov, please consider creating a changeset entry in |
d5785d3 to
1675464
Compare
| "type": "object" | ||
| }, | ||
| "EventLevel": { | ||
| "EventLevelConfig": { |
There was a problem hiding this comment.
Whole change is caused by my crating separate enum type just for config
| }), | ||
| }), | ||
| }) | ||
| .filter_map(|(name, config)| CustomEvent::from_config(name, config)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Maybe we could still use TryFrom and convert it into an option using .ok()
There was a problem hiding this comment.
TryFrom returns Result: https://doc.rust-lang.org/std/convert/trait.TryFrom.html
So it will require us to define some dummy error type.
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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)), |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Everything is mutable now because I removed the internal wrapping of Arc + Mutex around the condition.
|
|
||
| fn on_response(&self, response: &Self::Response) { | ||
| for custom_event in &self.custom { | ||
| pub(crate) fn on_response(&mut self, response: &ConnectorResponse) { |
There was a problem hiding this comment.
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>>, |
There was a problem hiding this comment.
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) 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>>, |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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, | ||
| } |
There was a problem hiding this comment.
Main purpose of adding it is to remove Off
| 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>>, |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
I removed "inner" since we no longer need Mutex, we maintain a single copy and mutate it directly.
| error!(http.method = "GET", "Hello from test"); | ||
|
|
||
| let router_events = event_config.new_router_events(); | ||
| let mut router_events = event_config.new_router_events(); |
There was a problem hiding this comment.
Everything became explicitly mutable.
Previously it also was mutable but hidden under internal Arc + Mutex
| @@ -1,9 +1,6 @@ | |||
| telemetry: | |||
|
|
|||
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
we send default query here, just attach custom header to it
| }), | ||
| }), | ||
| }) | ||
| .filter_map(|(name, config)| CustomEvent::from_config(name, config)) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Why don't we call .take() here like in request ? Just a question I'm not sure it's needed.
There was a problem hiding this comment.
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.
BrynCooke
left a comment
There was a problem hiding this comment.
Nice! It's great to have events standardized
|
@mergify backport 1.x |
✅ Backports have been createdDetails
|
(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
|
CI performance tests
|
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_responsefunctions exit earlier:router/apollo-router/src/plugins/telemetry/config_new/events.rs
Lines 261 to 264 in 3ae5f3f
And because of it, bypass
responseand custom events:router/apollo-router/src/plugins/telemetry/config_new/events.rs
Lines 273 to 281 in 3ae5f3f
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.
Exceptions
Note any exceptions here
Notes
Footnotes
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. ↩
Configuration is an important part of many changes. Where applicable please try to document configuration examples. ↩
Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions. ↩