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

editoast: setup tracing with opentelemetry for TestApp #10122

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

hamz2a
Copy link
Contributor

@hamz2a hamz2a commented Dec 18, 2024

closes #10123

I added the OpenTelemetry layer in TestApp to capture the trace_id set in the header, so we can test the endpoint we'll add in the ticket.

  • Refactored init_tracing to accept a custom SpanExporter for better flexibility, including support for testing scenarios.
  • Updated main.rs to configure and initialize tracing.
  • Modified setup tracing in test_app.rs to use opentelemetry layer.

@hamz2a hamz2a requested a review from a team as a code owner December 18, 2024 12:00
@github-actions github-actions bot added the area:editoast Work on Editoast Service label Dec 18, 2024
@hamz2a hamz2a requested a review from woshilapin December 18, 2024 12:01
@hamz2a hamz2a changed the title editoast: setup tracing with opentelemetry for TestApp editoast: setup tracing with opentelemetry for TestApp Dec 18, 2024
@codecov-commenter
Copy link

codecov-commenter commented Dec 18, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 86.66667% with 8 lines in your changes missing coverage. Please review.

Project coverage is 81.45%. Comparing base (6a105bf) to head (72ae856).

Files with missing lines Patch % Lines
editoast/src/client/telemetry_config.rs 0.00% 6 Missing ⚠️
editoast/editoast_common/src/tracing.rs 96.00% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #10122      +/-   ##
==========================================
+ Coverage   79.93%   81.45%   +1.51%     
==========================================
  Files        1057     1058       +1     
  Lines      106302   104318    -1984     
  Branches      724      724              
==========================================
- Hits        84977    84971       -6     
+ Misses      21283    19305    -1978     
  Partials       42       42              
Flag Coverage Δ
editoast 73.60% <86.66%> (-0.32%) ⬇️
front 89.19% <ø> (ø)
gateway 2.18% <ø> (ø)
osrdyne 3.28% <ø> (ø)
railjson_generator 87.50% <ø> (ø)
tests 87.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from c5b5f96 to 8a7a2ad Compare December 18, 2024 12:14
Copy link
Contributor

@woshilapin woshilapin left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. Let's see if we can improve it.

editoast/src/main.rs Outdated Show resolved Hide resolved
editoast/Cargo.toml Outdated Show resolved Hide resolved
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from 8a7a2ad to c361ea8 Compare December 18, 2024 14:32
@hamz2a hamz2a requested a review from woshilapin December 18, 2024 14:34
editoast/src/main.rs Outdated Show resolved Hide resolved
editoast/src/main.rs Outdated Show resolved Hide resolved
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from c361ea8 to b5ff13b Compare December 18, 2024 16:31
Copy link
Contributor

@leovalais leovalais left a comment

Choose a reason for hiding this comment

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

Thanks for this PR. A few comments about architecture and test isolation. Besides, with the context of what you're working on I suppose this is about collecting logs for unit testing, but it's not clear to me how that would work. Why does printing logs on stdout help? Do you plan to capture the stream? A bit more context in the PR description or in the related issue would help.

editoast/Cargo.toml Outdated Show resolved Hide resolved
Comment on lines 17 to 24
client::{TelemetryConfig, TelemetryKind},
core::{mocking::MockingClient, CoreClient},
generated_data::speed_limit_tags_config::SpeedLimitTagIds,
infra_cache::InfraCache,
map::MapLayers,
try_init_tracing,
valkey_utils::ValkeyConfig,
AppState, ValkeyClient,
AppState, EditoastMode, ValkeyClient,
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong dependency order. We want client and bin editoast to depend on views

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, but I need these structs for init_tracing in test_app:

let telemetry_config = TelemetryConfig {
    telemetry_kind: TelemetryKind::Opentelemetry,
    ..Default::default()
};
let sub = init_tracing(EditoastMode::Webservice, &telemetry_config, exporter);
let tracing_guard = tracing::subscriber::set_default(sub);

Any suggestions on how to better isolate this?

Copy link
Contributor

Choose a reason for hiding this comment

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

You could move init_tracing in editoast_common, but EditoastMode and the telemetry_config do not make sense there. You'll need some kind of mapping.

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’ve updated the function signature for create_tracing_subscriber to the following:

pub fn create_tracing_subscriber<T: SpanExporter + 'static>(
    mode: Option<EditoastMode>,
    telemetry_config: Option<TelemetryConfig>,
    exporter: T,
) -> impl tracing::Subscriber

I believe it adds more flexibility. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not entirely convinced 😕

  1. This does not solves the dependency problem: the function is still imported in views from the crate level.
  2. We have two calls to that function and we know each time which mode and config we want. So there's no need for flexibility when 100% of our usage do not require that.
  3. I'd argue that hiding a parameter behind a None degrades readability: now we have to look inside the function to find the actual values of the parameters, both of them not having an obvious default value.

Wdyt?

Copy link
Contributor Author

@hamz2a hamz2a Dec 24, 2024

Choose a reason for hiding this comment

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

I’ve updated the code based on your feedback. Please verify if the changes align with your suggestions. @leovalais

editoast/src/views/test_app.rs Outdated Show resolved Hide resolved
editoast/src/views/test_app.rs Outdated Show resolved Hide resolved
@hamz2a
Copy link
Contributor Author

hamz2a commented Dec 19, 2024

Thanks for this PR. A few comments about architecture and test isolation. Besides, with the context of what you're working on I suppose this is about collecting logs for unit testing, but it's not clear to me how that would work. Why does printing logs on stdout help? Do you plan to capture the stream? A bit more context in the PR description or in the related issue would help.

Apologies for that. I've added more details in the description, and I hope it helps clarify things.

@leovalais
Copy link
Contributor

I added the OpenTelemetry layer in TestApp to capture the trace_id set in the header, so we can test the endpoint we'll add in the #9724.

Thanks for adding some context. So if I get it right, the point is not to necessarily print on stdout, but rather to setup OpenTelemetry so that the request header can be read. The fact that it prints on stdout is irrelevant, or am I missing smth?

I'm surely lacking OpenTelemetry and tracing knowledge, but I still don't understand what setting up a span exporter has to do with reading the HTTP request headers? Especially since the middleware OtelAxumLayer::default() is set in the TestApp.

@hamz2a
Copy link
Contributor Author

hamz2a commented Dec 19, 2024

Thanks for adding some context. So if I get it right, the point is not to necessarily print on stdout, but rather to setup OpenTelemetry so that the request header can be read. The fact that it prints on stdout is irrelevant, or am I missing smth?

I'm surely lacking OpenTelemetry and tracing knowledge, but I still don't understand what setting up a span exporter has to do with reading the HTTP request headers? Especially since the middleware OtelAxumLayer::default() is set in the TestApp.

You're right, printing to stdout is irrelevant.
The goal is to have the same init_tracing setup for both editoast webservice and TestApp. That's why I kept the same configuration. @woshilapin, do you confirm?

@woshilapin
Copy link
Contributor

woshilapin commented Dec 20, 2024

I'm surely lacking OpenTelemetry and tracing knowledge, but I still don't understand what setting up a span exporter has to do with reading the HTTP request headers? Especially since the middleware OtelAxumLayer::default() is set in the TestApp.

@leovalais From what I understand, OtelAxumLayer emits span events, the SpanExporter is used in the subscriber to these events and will consume these span events to export them somewhere (to Datadog, to stdout, etc.). But you're right, we shouldn't need opentelemetry_stdout since we already have the tracing::layer::fmt() configured, which prints to the terminal.

But as said by @hamz2a, one of the main idea of this PR is to have a similar setup of the subscriber for tests and for production code. We had a hard time debugging what was not working and part of it was that it was working well with a cargo run -- runserver but not with cargo test, and some of the problems came from this difference between production code and tests.

@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from b5ff13b to 4492eb8 Compare December 20, 2024 14:56
@hamz2a hamz2a requested a review from leovalais December 20, 2024 15:02
editoast/src/views/test_app.rs Outdated Show resolved Hide resolved
editoast/src/views/test_app.rs Outdated Show resolved Hide resolved
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from 4492eb8 to 112a5c5 Compare December 20, 2024 16:22
@hamz2a hamz2a requested a review from woshilapin December 20, 2024 16:23
editoast/Cargo.toml Outdated Show resolved Hide resolved
Copy link
Contributor

@woshilapin woshilapin left a comment

Choose a reason for hiding this comment

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

I believe it's in a much better state now 🚀. Thank you 🎉

@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch 2 times, most recently from 9d9e553 to 373cf12 Compare December 23, 2024 10:36
editoast/Cargo.toml Outdated Show resolved Hide resolved
editoast/src/main.rs Outdated Show resolved Hide resolved
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from 373cf12 to 45217d4 Compare December 23, 2024 14:42
@hamz2a hamz2a requested a review from leovalais December 23, 2024 14:45
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch 2 times, most recently from 033cbc3 to 5c49d18 Compare December 24, 2024 10:24
Copy link
Contributor

@leovalais leovalais left a comment

Choose a reason for hiding this comment

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

Almost there, thanks for the changes! There's still a dependency order issue, but apart from that, lgtm.

use serde::de::DeserializeOwned;
use tower_http::trace::TraceLayer;
use url::Url;

use crate::{
core::{mocking::MockingClient, CoreClient},
create_tracing_subscriber,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the import that breaks the dependency order. views cannot use something from the crate or client level. I'd suggest moving create_tracing_subscriber to editoast_common.

@leovalais
Copy link
Contributor

si je bouge create_tracing_subscriber je dois bouger aussi

   mode: EditoastMode,
   telemetry_config: TelemetryConfig,

Yes, but TelemetryConfig is a clap definition and EditoastMode doesn't make sense outside of the CLI. I'd suggest making a new struct for configuration:

struct TracingConfig {
   stream: enum { Stderr, Stdout },
   telemetry: Option<struct {
		service_name: String,
		telemetry_endpoint: Url
	}>
}

Wdyt?

@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from 5c49d18 to 4418d21 Compare December 26, 2024 17:42
@hamz2a hamz2a requested a review from leovalais December 26, 2024 17:42
@hamz2a hamz2a force-pushed the hai/editoast-setup-tracing-with-opentelemetry-for-test-app branch from 4418d21 to 72ae856 Compare December 26, 2024 17:52
Copy link
Contributor

@leovalais leovalais left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for all the modifications!

@@ -66,61 +58,18 @@ pub use valkey_utils::ValkeyConnection;
/// - we *expect* a webserver to output logging information, so since it's an expected
/// output (and not extra information), it should be on stdout
#[derive(Debug, PartialEq)]
enum EditoastMode {
pub enum EditoastMode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub enum EditoastMode {
enum EditoastMode {

@@ -22,7 +22,7 @@ pub mod train_schedule;
pub mod work_schedules;

#[cfg(test)]
mod test_app;
pub mod test_app;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub mod test_app;
mod test_app;

)
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
.finish();
let sub = create_tracing_subscriber(TracingConfig::default(), NoopSpanExporter);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let sub = create_tracing_subscriber(TracingConfig::default(), NoopSpanExporter);
let sub = create_tracing_subscriber(TracingConfig {
...
}, NoopSpanExporter);

Could the default value be built here? This way we gather all default test constants here instead of having them scattered across different crates. The impl Default and derive(Derivative) from common::tracing can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:editoast Work on Editoast Service
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Editoast: setup tracing with opentelemetry for TestApp
4 participants