-
Notifications
You must be signed in to change notification settings - Fork 598
docs: Fix tracing grpc example #2710
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
cijothomas
merged 5 commits into
open-telemetry:main
from
cijothomas:cijothomas/fix-grpc
Feb 26, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8cd1c2e
Fix tracing grpc example
cijothomas ad19d88
Merge branch 'main' into cijothomas/fix-grpc
cijothomas d690894
Merge branch 'main' into cijothomas/fix-grpc
cijothomas 111c14e
Merge branch 'main' into cijothomas/fix-grpc
cijothomas 45051ce
Merge branch 'main' into cijothomas/fix-grpc
cijothomas 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
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 |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ fn init_tracer() -> sdktrace::SdkTracerProvider { | |
| global::set_text_map_propagator(TraceContextPropagator::new()); | ||
| // Install stdout exporter pipeline to be able to retrieve the collected spans. | ||
| let provider = sdktrace::SdkTracerProvider::builder() | ||
| .with_batch_exporter(SpanExporter::default()) | ||
| .with_simple_exporter(SpanExporter::default()) | ||
| .build(); | ||
|
|
||
| global::set_tracer_provider(provider.clone()); | ||
|
|
@@ -43,10 +43,14 @@ async fn greet() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static | |
| let span = tracer | ||
| .span_builder("Greeter/client") | ||
| .with_kind(SpanKind::Client) | ||
| .with_attributes([KeyValue::new("component", "grpc")]) | ||
| .with_attributes([ | ||
| KeyValue::new("rpc.system", "grpc"), | ||
| KeyValue::new("server.port", 50052), | ||
| KeyValue::new("rpc.method", "say_hello"), | ||
| ]) | ||
| .start(&tracer); | ||
| let cx = Context::current_with_span(span); | ||
| let mut client = GreeterClient::connect("http://[::1]:50051").await?; | ||
| let mut client = GreeterClient::connect("http://[::1]:50052").await?; | ||
|
|
||
| let mut request = tonic::Request::new(HelloRequest { | ||
| name: "Tonic".into(), | ||
|
|
@@ -58,16 +62,23 @@ async fn greet() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static | |
|
|
||
| let response = client.say_hello(request).await; | ||
|
|
||
| let span = cx.span(); | ||
|
Member
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. trying to get the span from context once, and re-use. Seeing the benchmarks for Context, I suspect asking for current span from Context repeatedly is unnecessarily burning CPU cycles. |
||
| let status = match response { | ||
| Ok(_res) => "OK".to_string(), | ||
| Ok(_res) => { | ||
| span.set_attribute(KeyValue::new("response", "OK")); | ||
| "OK".to_string() | ||
| } | ||
| Err(status) => { | ||
| // Access the status code | ||
| let status_code = status.code(); | ||
| span.set_attribute(KeyValue::new( | ||
| "response_code_desc", | ||
| status_code.description(), | ||
| )); | ||
| status_code.to_string() | ||
| } | ||
| }; | ||
| cx.span() | ||
| .add_event("Got response!", vec![KeyValue::new("status", status)]); | ||
| span.add_event("Got response!", vec![KeyValue::new("status", status)]); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
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.