-
Notifications
You must be signed in to change notification settings - Fork 2
test: add integration tests and test dependencies #14
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use axum::{ | ||
| Router, | ||
| body::Body, | ||
| http::{Method, Request, StatusCode}, | ||
| routing::get, | ||
| }; | ||
| use axum_otel::{AxumOtelOnFailure, AxumOtelOnResponse, AxumOtelSpanCreator, Level}; | ||
| use http_body_util::BodyExt; | ||
| use opentelemetry::{global, trace::TracerProvider}; | ||
| use opentelemetry_sdk::{ | ||
| Resource, | ||
| trace::{InMemorySpanExporter, RandomIdGenerator, Sampler, SdkTracerProvider}, | ||
| }; | ||
| use tower::ServiceExt; | ||
| use tower_http::trace::TraceLayer; | ||
| use tracing::instrument; | ||
| use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
|
||
| #[instrument] | ||
| async fn hello() -> &'static str { | ||
| "Hello, world!" | ||
| } | ||
|
|
||
| fn app() -> Router<()> { | ||
| Router::new().route("/", get(hello)).layer( | ||
| TraceLayer::new_for_http() | ||
| .make_span_with(AxumOtelSpanCreator::new().level(Level::INFO)) | ||
| .on_response(AxumOtelOnResponse::new().level(Level::INFO)) | ||
| .on_failure(AxumOtelOnFailure::new()), | ||
| ) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
iamnivekx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async fn test_axum_otel_middleware() { | ||
| // Set up in-memory exporter for testing | ||
| let exporter = InMemorySpanExporter::default(); | ||
| let provider: SdkTracerProvider = SdkTracerProvider::builder() | ||
| .with_sampler(Sampler::AlwaysOn) | ||
| .with_id_generator(RandomIdGenerator::default()) | ||
| .with_simple_exporter(exporter.clone()) | ||
| .with_resource(Resource::builder().build()) | ||
| .build(); | ||
|
|
||
| global::set_tracer_provider(provider.clone()); | ||
|
|
||
| // Set up tracing subscriber with OpenTelemetry layer | ||
| let tracer = provider.tracer("axum-otel-test".to_string()); | ||
| let otel_layer = tracing_opentelemetry::OpenTelemetryLayer::new(tracer); | ||
| Registry::default() | ||
| .with(otel_layer) | ||
| .try_init() | ||
| .expect("Failed to initialize tracing subscriber"); | ||
iamnivekx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let app = app(); | ||
|
|
||
| // Send request using oneshot | ||
| let response = app | ||
| .oneshot( | ||
| Request::builder() | ||
| .uri("/") | ||
| .method(Method::GET) | ||
| .body(Body::empty()) | ||
| .expect("Failed to build request"), | ||
| ) | ||
| .await | ||
| .expect("Failed to send request"); | ||
|
|
||
| assert_eq!(response.status(), StatusCode::OK); | ||
|
|
||
| let body = response | ||
| .into_body() | ||
| .collect() | ||
| .await | ||
| .expect("Failed to read body"); | ||
|
|
||
| assert_eq!(body.to_bytes(), "Hello, world!".as_bytes()); | ||
|
|
||
| // Force flush to ensure spans are exported | ||
| let _ = provider.force_flush(); | ||
|
|
||
| // Verify spans were created | ||
| let spans = exporter | ||
| .get_finished_spans() | ||
| .expect("Failed to get finished spans"); | ||
|
|
||
| assert!( | ||
| !spans.is_empty(), | ||
| "Expected at least one span to be created" | ||
| ); | ||
|
|
||
| // With oneshot(), there's no MatchedPath, so span name is just "GET" | ||
| // When using a real server, it would be "GET /" | ||
| let request_span = spans | ||
| .iter() | ||
| .find(|s| s.name == "GET" || s.name == "GET /") | ||
| .expect("Request span not found"); | ||
|
|
||
| let hello_span = spans | ||
| .iter() | ||
| .find(|s| s.name == "hello") | ||
| .expect("Handler span not found"); | ||
|
|
||
| assert_eq!( | ||
| hello_span.parent_span_id, | ||
| request_span.span_context.span_id(), | ||
| "Handler span should be a child of the request span" | ||
| ); | ||
|
|
||
| // Check http.status_code attribute | ||
| let status_code_attr = request_span | ||
| .attributes | ||
| .iter() | ||
| .find(|kv| kv.key.as_str() == "http.status_code") | ||
| .map(|kv| kv.value.to_string()); | ||
|
|
||
| assert_eq!( | ||
| status_code_attr, | ||
| Some("200".to_string()), | ||
| "Expected http.status_code to be 200" | ||
| ); | ||
|
|
||
| provider | ||
| .shutdown() | ||
| .expect("Failed to shutdown tracer provider"); | ||
| } | ||
iamnivekx marked this conversation as resolved.
Show resolved
Hide resolved
iamnivekx marked this conversation as resolved.
Show resolved
Hide resolved
iamnivekx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.