-
Notifications
You must be signed in to change notification settings - Fork 328
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
Changes from all commits
1c9370c
156e581
1675464
436f97a
44a6e3d
1f77164
5c76457
16b3636
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use opentelemetry::Key; | ||
| use opentelemetry::KeyValue; | ||
| use parking_lot::Mutex; | ||
|
|
@@ -6,25 +8,35 @@ use serde::Deserialize; | |
| use tower::BoxError; | ||
|
|
||
| use crate::Context; | ||
| use crate::plugins::telemetry::config_new::conditions::Condition; | ||
| use crate::plugins::telemetry::config_new::connector::ConnectorRequest; | ||
| use crate::plugins::telemetry::config_new::connector::ConnectorResponse; | ||
| use crate::plugins::telemetry::config_new::connector::attributes::ConnectorAttributes; | ||
| use crate::plugins::telemetry::config_new::connector::selectors::ConnectorSelector; | ||
| use crate::plugins::telemetry::config_new::events::CustomEvent; | ||
| use crate::plugins::telemetry::config_new::events::CustomEventInner; | ||
| use crate::plugins::telemetry::config_new::events::CustomEvents; | ||
| use crate::plugins::telemetry::config_new::events::Event; | ||
| use crate::plugins::telemetry::config_new::events::EventLevel; | ||
| use crate::plugins::telemetry::config_new::events::StandardEvent; | ||
| use crate::plugins::telemetry::config_new::events::StandardEventConfig; | ||
| use crate::plugins::telemetry::config_new::events::log_event; | ||
| use crate::plugins::telemetry::config_new::extendable::Extendable; | ||
| use crate::plugins::telemetry::config_new::instruments::Instrumented; | ||
|
|
||
| #[derive(Clone)] | ||
| pub(crate) struct ConnectorEventRequest(pub(crate) StandardEvent<ConnectorSelector>); | ||
| pub(crate) struct ConnectorEventRequest { | ||
| // XXX(@IvanGoncharov): As part of removing Mutex from StandardEvent I moved it here | ||
| // I think it's not nessary here but can't verify it right now, so in future can just wrap StandardEvent | ||
| pub(crate) level: EventLevel, | ||
| pub(crate) condition: Arc<Mutex<Condition<ConnectorSelector>>>, | ||
IvanGoncharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub(crate) struct ConnectorEventResponse(pub(crate) StandardEvent<ConnectorSelector>); | ||
| pub(crate) struct ConnectorEventResponse { | ||
| // XXX(@IvanGoncharov): As part of removing Arc from StandardEvent I moved it here | ||
| // I think it's not nessary here but can't verify it right now, so in future can just wrap StandardEvent | ||
| pub(crate) level: EventLevel, | ||
| pub(crate) condition: Arc<Condition<ConnectorSelector>>, | ||
| } | ||
|
|
||
| #[derive(Clone, Deserialize, JsonSchema, Debug, Default)] | ||
| #[serde(deny_unknown_fields, default)] | ||
|
|
@@ -46,92 +58,67 @@ pub(crate) fn new_connector_events( | |
| let custom_events = config | ||
| .custom | ||
| .iter() | ||
| .filter_map(|(event_name, event_cfg)| match &event_cfg.level { | ||
| EventLevel::Off => None, | ||
| _ => Some(CustomEvent { | ||
| inner: Mutex::new(CustomEventInner { | ||
| name: event_name.clone(), | ||
| level: event_cfg.level, | ||
| event_on: event_cfg.on, | ||
| message: event_cfg.message.clone(), | ||
| selectors: event_cfg.attributes.clone().into(), | ||
| condition: event_cfg.condition.clone(), | ||
| attributes: Vec::new(), | ||
| _phantom: Default::default(), | ||
| }), | ||
| }), | ||
| }) | ||
| .filter_map(|(name, config)| CustomEvent::from_config(name, config)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are very repetitive, so I just moved the code to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could still use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. basically we return None if event is |
||
| .collect(); | ||
|
|
||
| ConnectorEvents { | ||
| request: config.attributes.request.clone().into(), | ||
| response: config.attributes.response.clone().into(), | ||
| error: config.attributes.error.clone().into(), | ||
| request: StandardEvent::from_config(&config.attributes.request), | ||
| response: StandardEvent::from_config(&config.attributes.response), | ||
| error: StandardEvent::from_config(&config.attributes.error), | ||
| custom: custom_events, | ||
| } | ||
| } | ||
|
|
||
| impl Instrumented | ||
| for CustomEvents< | ||
| ConnectorRequest, | ||
| ConnectorResponse, | ||
| (), | ||
| ConnectorAttributes, | ||
| ConnectorSelector, | ||
| > | ||
| { | ||
| type Request = ConnectorRequest; | ||
| type Response = ConnectorResponse; | ||
| type EventResponse = (); | ||
|
|
||
| fn on_request(&self, request: &Self::Request) { | ||
| impl CustomEvents<ConnectorRequest, ConnectorResponse, (), ConnectorAttributes, ConnectorSelector> { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the |
||
| pub(crate) fn on_request(&mut self, request: &ConnectorRequest) { | ||
| // Any condition on the request is NOT evaluated here. It must be evaluated later when | ||
| // getting the ConnectorEventRequest from the context. The request context is shared | ||
| // between all connector requests, so any request could find this ConnectorEventRequest in | ||
| // the context. Its presence on the context cannot be conditional on an individual request. | ||
| if self.request.level() != EventLevel::Off { | ||
| request | ||
| .context | ||
| .extensions() | ||
| .with_lock(|lock| lock.insert(ConnectorEventRequest(self.request.clone()))); | ||
| if let Some(request_event) = self.request.take() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use |
||
| request.context.extensions().with_lock(|lock| { | ||
| lock.insert(ConnectorEventRequest { | ||
| level: request_event.level, | ||
| condition: Arc::new(Mutex::new(request_event.condition)), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| }) | ||
| }); | ||
| } | ||
|
|
||
| if self.response.level() != EventLevel::Off { | ||
| request | ||
| .context | ||
| .extensions() | ||
| .with_lock(|lock| lock.insert(ConnectorEventResponse(self.response.clone()))); | ||
| if let Some(response_event) = self.response.take() { | ||
| request.context.extensions().with_lock(|lock| { | ||
| lock.insert(ConnectorEventResponse { | ||
| level: response_event.level, | ||
| condition: Arc::new(response_event.condition), | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| for custom_event in &self.custom { | ||
| for custom_event in &mut self.custom { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to make it public since I removed trait |
||
| for custom_event in &mut self.custom { | ||
| custom_event.on_response(response); | ||
| } | ||
| } | ||
|
|
||
| fn on_error(&self, error: &BoxError, ctx: &Context) { | ||
| if self.error.level() != EventLevel::Off { | ||
| if let Some(condition) = self.error.condition() { | ||
| if !condition.lock().evaluate_error(error, ctx) { | ||
| return; | ||
| } | ||
| pub(crate) fn on_error(&mut self, error: &BoxError, ctx: &Context) { | ||
| if let Some(error_event) = &mut self.error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
| if error_event.condition.evaluate_error(error, ctx) { | ||
| log_event( | ||
| error_event.level, | ||
| "connector.http.error", | ||
| vec![KeyValue::new( | ||
| Key::from_static_str("error"), | ||
| opentelemetry::Value::String(error.to_string().into()), | ||
| )], | ||
| "", | ||
| ); | ||
| } | ||
| log_event( | ||
| self.error.level(), | ||
| "connector.http.error", | ||
| vec![KeyValue::new( | ||
| Key::from_static_str("error"), | ||
| opentelemetry::Value::String(error.to_string().into()), | ||
| )], | ||
| "", | ||
| ); | ||
| } | ||
| for custom_event in &self.custom { | ||
| for custom_event in &mut self.custom { | ||
| custom_event.on_error(error, ctx); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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