Skip to content

Commit 04c7da7

Browse files
committed
XXX more features, more example exercising things
1 parent 0864410 commit 04c7da7

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

dropshot/examples/otel.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ async fn main() -> Result<(), String> {
6868
api.register(example_api_put_counter).unwrap();
6969
api.register(example_api_get).unwrap();
7070
api.register(example_api_error).unwrap();
71+
api.register(example_api_panic).unwrap();
7172

7273
// The functions that implement our API endpoints will share this context.
7374
let api_context = ExampleContext::new();
@@ -201,6 +202,9 @@ async fn example_api_get(
201202
let req = traced_request("http://localhost:4000/error", &cx).await;
202203
let _res = client.request(req).await;
203204

205+
let req = traced_request("http://localhost:4000/panic", &cx).await;
206+
let _res = client.request(req).await;
207+
204208
let api_context = rqctx.context();
205209
Ok(HttpResponseOk(CounterValue {
206210
counter: api_context.counter.load(Ordering::SeqCst),
@@ -231,6 +235,26 @@ async fn example_api_error(
231235
_rqctx: RequestContext<ExampleContext>,
232236
) -> Result<HttpResponseOk<CounterValue>, HttpError> {
233237
//XXX Why does this create a 499 rather than a 500 error???
238+
// This feels like a bug in dropshot. As a downstream consumer
239+
// I just want anything blowing up in my handler to be a somewhat useful 500 error.
240+
// It does help that the compiler is strict about what can otherwise be returned...
241+
//panic!("This handler is totally broken!");
242+
243+
Err(HttpError::for_internal_error("This endpoint is broken".to_string()))
244+
}
245+
246+
/// This endpoint panics!
247+
#[endpoint {
248+
method = GET,
249+
path = "/panic",
250+
}]
251+
async fn example_api_panic(
252+
_rqctx: RequestContext<ExampleContext>,
253+
) -> Result<HttpResponseOk<CounterValue>, HttpError> {
254+
//XXX Why does this create a 499 rather than a 500 error???
255+
// This feels like a bug in dropshot. As a downstream consumer
256+
// I just want anything blowing up in my handler to be a somewhat useful 500 error.
257+
// It does help that the compiler is strict about what can otherwise be returned...
234258
panic!("This handler is totally broken!");
235259
}
236260

dropshot/src/otel.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ pub(crate) struct RequestInfo {
4343
// - error.message
4444
// - error.cause_chain
4545
// - otel.status_code
46-
#[derive(Debug, Clone, serde::Serialize)]
47-
pub(crate) struct ResponseInfo {
48-
pub id: String,
49-
pub local_addr: std::net::SocketAddr,
50-
pub remote_addr: std::net::SocketAddr,
46+
pub(crate) struct ResponseInfo<'a> {
5147
pub status_code: u16,
5248
pub message: String,
49+
pub error: Option<&'a crate::handler::HandlerError>,
5350
}
5451

5552
fn extract_context_from_request(
@@ -60,8 +57,6 @@ fn extract_context_from_request(
6057
})
6158
}
6259

63-
// - otel.kind
64-
// - otel.name
6560
pub fn create_request_span(
6661
request: &hyper::Request<hyper::body::Incoming>,
6762
) -> opentelemetry::global::BoxedSpan {
@@ -74,7 +69,7 @@ pub fn create_request_span(
7469
let tracer = tracer_provider.tracer_with_scope(scope);
7570
let parent_cx = extract_context_from_request(&request);
7671
tracer
77-
.span_builder("dropshot_request") //XXX Fixme
72+
.span_builder("dropshot_request") // TODO? Naming is hard.
7873
.with_kind(opentelemetry::trace::SpanKind::Server)
7974
.start_with_context(&tracer, &parent_cx)
8075
}
@@ -124,6 +119,10 @@ impl TraceDropshot for opentelemetry::global::BoxedSpan {
124119
response.message,
125120
),
126121
]);
127-
//XXX if this is a 5xx error, mark the span as an error
122+
if let Some(error) = response.error{
123+
self.set_status(
124+
opentelemetry::trace::Status::error(error.internal_message().clone())
125+
);
126+
}
128127
}
129128
}

dropshot/src/server.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ async fn http_request_handle_wrap<C: ServerContext>(
814814

815815
// Copy local address to report later during the finish probe, as the
816816
// server is passed by value to the request handler function.
817-
#[cfg(any(feature = "usdt-probes", feature = "otel-tracing"))]
817+
#[cfg(feature = "usdt-probes")]
818818
let local_addr = server.local_addr;
819819

820820
#[cfg(feature = "otel-tracing")]
@@ -831,14 +831,12 @@ async fn http_request_handle_wrap<C: ServerContext>(
831831

832832
#[cfg(feature = "otel-tracing")]
833833
span.trace_response(crate::otel::ResponseInfo {
834-
id: request_id.clone(),
835-
local_addr,
836-
remote_addr,
837834
// 499 is a non-standard code popularized by nginx to mean "client disconnected".
838835
status_code: 499,
839836
message: String::from(
840837
"client disconnected before response returned",
841838
),
839+
error: None,
842840
});
843841

844842
#[cfg(feature = "usdt-probes")]
@@ -851,6 +849,7 @@ async fn http_request_handle_wrap<C: ServerContext>(
851849
status_code: 499,
852850
message: String::from(
853851
"client disconnected before response returned",
852+
error: None,
854853
),
855854
}
856855
});
@@ -881,13 +880,11 @@ async fn http_request_handle_wrap<C: ServerContext>(
881880

882881
#[cfg(feature = "otel-tracing")]
883882
span.trace_response(crate::otel::ResponseInfo {
884-
id: request_id.clone(),
885-
local_addr,
886-
remote_addr,
887883
status_code: status.as_u16(),
888884
message: message_external
889885
.cloned()
890886
.unwrap_or_else(|| message_internal.clone()),
887+
error: Some(&error),
891888
});
892889

893890
#[cfg(feature = "usdt-probes")]
@@ -923,11 +920,9 @@ async fn http_request_handle_wrap<C: ServerContext>(
923920

924921
#[cfg(feature = "otel-tracing")]
925922
span.trace_response(crate::otel::ResponseInfo {
926-
id: request_id.parse().unwrap(),
927-
local_addr,
928-
remote_addr,
929923
status_code: response.status().as_u16(),
930924
message: "".to_string(),
925+
error: None,
931926
});
932927

933928
#[cfg(feature = "usdt-probes")]

0 commit comments

Comments
 (0)