-
Notifications
You must be signed in to change notification settings - Fork 77
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
[DRAFT - DO NOT MERGE] Basic OpenTelemetry integration #1201
Draft
nshalman
wants to merge
8
commits into
oxidecomputer:main
Choose a base branch
from
nshalman:otel-tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,354
−637
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5b2d67
XXX add basic otel tracing
nshalman d946139
XXX otel: add an example
nshalman 90c5de0
XXX cargo update DO NOT MERGE
nshalman 399577f
XXX more features
nshalman 0864410
XXX More features in example code
nshalman 1a5061c
XXX more features, more example exercising things
nshalman 4092c21
XXX more proof why that hack is gross
nshalman 1db15e7
XXX Better code documentation
nshalman 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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,282 @@ | ||
// Copyright 2024 Oxide Computer Company | ||
//! Example use of Dropshot with OpenTelemetry integration. | ||
//! | ||
//! equinix-otel-tools will parse the standard otel exporter | ||
//! environment variables, e.g. | ||
//! If you launch an otel-collector or otel-enabled jaeger-all-in-one | ||
//! listening for otlp over grpc, then you can do: | ||
//! | ||
//! ```bash | ||
//! export OTEL_EXPORTER_OTLP_ENDPOINT=grpc://localhost:4317 | ||
//! export OTEL_EXPORTER_OTLP_INSECURE=true | ||
//! export OTEL_EXPORTER_OTLP_PROTOCOL=grpc | ||
//! cargo run --features=otel-tracing --example otel& | ||
//! curl http://localhost:4000/get | ||
//! ``` | ||
//! | ||
//! And you should see an example trace. | ||
|
||
use dropshot::endpoint; | ||
use dropshot::ApiDescription; | ||
use dropshot::ConfigDropshot; | ||
use dropshot::ConfigLogging; | ||
use dropshot::ConfigLoggingLevel; | ||
use dropshot::HttpError; | ||
use dropshot::HttpResponseOk; | ||
use dropshot::HttpResponseUpdatedNoContent; | ||
use dropshot::HttpServerStarter; | ||
use dropshot::RequestContext; | ||
use dropshot::TypedBody; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use std::sync::atomic::AtomicU64; | ||
use std::sync::atomic::Ordering; | ||
|
||
use http_body_util::Full; | ||
use hyper_util::{client::legacy::Client, rt::TokioExecutor}; | ||
use opentelemetry::{ | ||
global, | ||
trace::{SpanKind, TraceContextExt, Tracer}, | ||
Context, | ||
}; | ||
use opentelemetry_http::{Bytes, HeaderInjector}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), String> { | ||
let _otel_guard = equinix_otel_tools::init("otel-demo"); | ||
|
||
let config_dropshot = ConfigDropshot { | ||
bind_address: "127.0.0.1:4000".parse().unwrap(), | ||
..Default::default() | ||
}; | ||
|
||
// For simplicity, we'll configure an "info"-level logger that writes to | ||
// stderr assuming that it's a terminal. | ||
let config_logging = | ||
ConfigLogging::StderrTerminal { level: ConfigLoggingLevel::Info }; | ||
let log = config_logging | ||
.to_logger("example-basic") | ||
.map_err(|error| format!("failed to create logger: {}", error))?; | ||
|
||
// Build a description of the API. | ||
let mut api = ApiDescription::new(); | ||
api.register(example_api_get_counter).unwrap(); | ||
api.register(example_api_put_counter).unwrap(); | ||
api.register(example_api_get).unwrap(); | ||
api.register(example_api_error).unwrap(); | ||
api.register(example_api_panic).unwrap(); | ||
|
||
// The functions that implement our API endpoints will share this context. | ||
let api_context = ExampleContext::new(); | ||
|
||
// Set up the server. | ||
let server = | ||
HttpServerStarter::new(&config_dropshot, api, api_context, &log) | ||
.map_err(|error| format!("failed to create server: {}", error))? | ||
.start(); | ||
|
||
// Wait for the server to stop. Note that there's not any code to shut down | ||
// this server, so we should never get past this point. | ||
let _ = server.await; | ||
Ok(()) | ||
} | ||
|
||
/// Application-specific example context (state shared by handler functions) | ||
#[derive(Debug)] | ||
struct ExampleContext { | ||
/// counter that can be manipulated by requests to the HTTP API | ||
counter: AtomicU64, | ||
} | ||
|
||
impl ExampleContext { | ||
/// Return a new ExampleContext. | ||
pub fn new() -> ExampleContext { | ||
ExampleContext { counter: AtomicU64::new(0) } | ||
} | ||
} | ||
|
||
// HTTP API interface | ||
|
||
/// `CounterValue` represents the value of the API's counter, either as the | ||
/// response to a GET request to fetch the counter or as the body of a PUT | ||
/// request to update the counter. | ||
#[derive(Debug, Deserialize, Serialize, JsonSchema)] | ||
struct CounterValue { | ||
counter: u64, | ||
} | ||
|
||
/// Helper function for propagating a traceparent using hyper | ||
async fn traced_request( | ||
uri: &str, | ||
cx: &Context, | ||
) -> hyper::Request<Full<Bytes>> { | ||
let mut req = hyper::Request::builder() | ||
.uri(uri) | ||
.method(hyper::Method::GET) | ||
.header("accept", "application/json") | ||
.header("content-type", "application/json"); | ||
global::get_text_map_propagator(|propagator| { | ||
propagator.inject_context( | ||
&cx, | ||
&mut HeaderInjector(req.headers_mut().unwrap()), | ||
) | ||
}); | ||
req.body(Full::new(Bytes::from("".to_string()))).unwrap() | ||
} | ||
|
||
/// Do a bunch of work to show off otel tracing | ||
#[endpoint { | ||
method = GET, | ||
path = "/get", | ||
}] | ||
#[cfg_attr(feature = "tokio-tracing", tracing::instrument(skip_all))] | ||
async fn example_api_get( | ||
rqctx: RequestContext<ExampleContext>, | ||
) -> Result<HttpResponseOk<CounterValue>, HttpError> { | ||
let trace_context = opentelemetry::Context::new(); | ||
let parent_context = | ||
opentelemetry::trace::TraceContextExt::with_remote_span_context( | ||
&trace_context, | ||
rqctx.span_context.clone(), | ||
); | ||
|
||
let client = Client::builder(TokioExecutor::new()).build_http(); | ||
let tracer = global::tracer(""); | ||
let span = tracer | ||
.span_builder(String::from("example_api_get")) | ||
.with_kind(SpanKind::Internal) | ||
.start_with_context(&tracer, &parent_context); | ||
let cx = Context::current_with_span(span); | ||
//assert!(cx.has_active_span()); | ||
|
||
let mut req = hyper::Request::builder() | ||
.uri("http://localhost:4000/counter") | ||
.method(hyper::Method::GET) | ||
.header("accept", "application/json") | ||
.header("content-type", "application/json"); | ||
global::get_text_map_propagator(|propagator| { | ||
propagator.inject_context( | ||
&cx, | ||
&mut HeaderInjector(req.headers_mut().unwrap()), | ||
) | ||
}); | ||
let _res = client | ||
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap()) | ||
.await; | ||
|
||
let mut req = hyper::Request::builder() | ||
.uri("http://localhost:4000/counter") | ||
.method(hyper::Method::GET) | ||
.header("accept", "application/json") | ||
.header("content-type", "application/json"); | ||
global::get_text_map_propagator(|propagator| { | ||
propagator.inject_context( | ||
&cx, | ||
&mut HeaderInjector(req.headers_mut().unwrap()), | ||
) | ||
}); | ||
let _res = client | ||
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap()) | ||
.await; | ||
|
||
let mut req = hyper::Request::builder() | ||
.uri("http://localhost:4000/does-not-exist") | ||
.method(hyper::Method::GET) | ||
.header("accept", "application/json") | ||
.header("content-type", "application/json") | ||
.header("user-agent", "dropshot-otel-example"); | ||
global::get_text_map_propagator(|propagator| { | ||
propagator.inject_context( | ||
&cx, | ||
&mut HeaderInjector(req.headers_mut().unwrap()), | ||
) | ||
}); | ||
let _res = client | ||
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap()) | ||
.await; | ||
|
||
let req = traced_request("http://localhost:4000/error", &cx).await; | ||
let _res = client.request(req).await; | ||
|
||
let req = traced_request("http://localhost:4000/panic", &cx).await; | ||
let _res = client.request(req).await; | ||
|
||
let api_context = rqctx.context(); | ||
Ok(HttpResponseOk(CounterValue { | ||
counter: api_context.counter.load(Ordering::SeqCst), | ||
})) | ||
} | ||
|
||
/// Fetch the current value of the counter. | ||
#[endpoint { | ||
method = GET, | ||
path = "/counter", | ||
}] | ||
async fn example_api_get_counter( | ||
rqctx: RequestContext<ExampleContext>, | ||
) -> Result<HttpResponseOk<CounterValue>, HttpError> { | ||
let api_context = rqctx.context(); | ||
|
||
Ok(HttpResponseOk(CounterValue { | ||
counter: api_context.counter.load(Ordering::SeqCst), | ||
})) | ||
} | ||
|
||
/// Cause an error! | ||
#[endpoint { | ||
method = GET, | ||
path = "/error", | ||
}] | ||
async fn example_api_error( | ||
_rqctx: RequestContext<ExampleContext>, | ||
) -> Result<HttpResponseOk<CounterValue>, HttpError> { | ||
//XXX Why does this create a 499 rather than a 500 error??? | ||
// This feels like a bug in dropshot. As a downstream consumer | ||
// I just want anything blowing up in my handler to be a somewhat useful 500 error. | ||
// It does help that the compiler is strict about what can otherwise be returned... | ||
//panic!("This handler is totally broken!"); | ||
|
||
Err(HttpError::for_internal_error("This endpoint is broken".to_string())) | ||
} | ||
|
||
/// This endpoint panics! | ||
#[endpoint { | ||
method = GET, | ||
path = "/panic", | ||
}] | ||
#[cfg_attr(feature = "tokio-tracing", tracing::instrument(skip_all, err))] | ||
async fn example_api_panic( | ||
_rqctx: RequestContext<ExampleContext>, | ||
) -> Result<HttpResponseOk<CounterValue>, HttpError> { | ||
//XXX Why does this create a 499 rather than a 500 error??? | ||
// This feels like a bug in dropshot. As a downstream consumer | ||
// I just want anything blowing up in my handler to be a somewhat useful 500 error. | ||
// It does help that the compiler is strict about what can otherwise be returned... | ||
panic!("This handler is totally broken!"); | ||
} | ||
|
||
/// Update the current value of the counter. Note that the special value of 10 | ||
/// is not allowed (just to demonstrate how to generate an error). | ||
#[endpoint { | ||
method = PUT, | ||
path = "/counter", | ||
}] | ||
#[cfg_attr(feature = "tokio-tracing", tracing::instrument(skip_all, err))] | ||
async fn example_api_put_counter( | ||
rqctx: RequestContext<ExampleContext>, | ||
update: TypedBody<CounterValue>, | ||
) -> Result<HttpResponseUpdatedNoContent, HttpError> { | ||
let api_context = rqctx.context(); | ||
let updated_value = update.into_inner(); | ||
|
||
if updated_value.counter == 10 { | ||
Err(HttpError::for_bad_request( | ||
Some(String::from("BadInput")), | ||
format!("do not like the number {}", updated_value.counter), | ||
)) | ||
} else { | ||
api_context.counter.store(updated_value.counter, Ordering::SeqCst); | ||
Ok(HttpResponseUpdatedNoContent()) | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -86,6 +86,8 @@ pub struct RequestContext<Context: ServerContext> { | |
pub log: Logger, | ||
/// basic request information (method, URI, etc.) | ||
pub request: RequestInfo, | ||
#[cfg(feature = "otel-tracing")] | ||
pub span_context: opentelemetry::trace::SpanContext, | ||
} | ||
|
||
// This is deliberately as close to compatible with `hyper::Request` as | ||
|
@@ -377,6 +379,19 @@ where | |
} | ||
} | ||
|
||
impl std::fmt::Display for HandlerError { | ||
fn fmt(&self, f: &mut Formatter) -> FmtResult { | ||
write!( | ||
f, | ||
"{}", | ||
match self { | ||
Self::Handler { ref message, .. } => message, | ||
Self::Dropshot(ref e) => &e.external_message, | ||
} | ||
) | ||
} | ||
} | ||
|
||
Comment on lines
+382
to
+394
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 think this is for tokio-tracing and could potentially be dropped or separated into a dedicated commit. Really all the tokio-tracing bits should be a separate commit now that I think about it. |
||
/// An error type which can be converted into an HTTP response. | ||
/// | ||
/// The error types returned by handlers must implement this trait, so that a | ||
|
This file contains 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.
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.
This is gross and I need something better.