Skip to content

Commit 0864410

Browse files
committed
XXX More features in example code
1 parent 399577f commit 0864410

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

dropshot/examples/otel.rs

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ async fn main() -> Result<(), String> {
5656

5757
// For simplicity, we'll configure an "info"-level logger that writes to
5858
// stderr assuming that it's a terminal.
59-
let config_logging = ConfigLogging::StderrTerminal {
60-
level: ConfigLoggingLevel::Info,
61-
};
59+
let config_logging =
60+
ConfigLogging::StderrTerminal { level: ConfigLoggingLevel::Info };
6261
let log = config_logging
6362
.to_logger("example-basic")
6463
.map_err(|error| format!("failed to create logger: {}", error))?;
@@ -68,14 +67,16 @@ async fn main() -> Result<(), String> {
6867
api.register(example_api_get_counter).unwrap();
6968
api.register(example_api_put_counter).unwrap();
7069
api.register(example_api_get).unwrap();
70+
api.register(example_api_error).unwrap();
7171

7272
// The functions that implement our API endpoints will share this context.
7373
let api_context = ExampleContext::new();
7474

7575
// Set up the server.
76-
let server = HttpServerStarter::new(&config_dropshot, api, api_context, &log)
77-
.map_err(|error| format!("failed to create server: {}", error))?
78-
.start();
76+
let server =
77+
HttpServerStarter::new(&config_dropshot, api, api_context, &log)
78+
.map_err(|error| format!("failed to create server: {}", error))?
79+
.start();
7980

8081
// Wait for the server to stop. Note that there's not any code to shut down
8182
// this server, so we should never get past this point.
@@ -93,9 +94,7 @@ struct ExampleContext {
9394
impl ExampleContext {
9495
/// Return a new ExampleContext.
9596
pub fn new() -> ExampleContext {
96-
ExampleContext {
97-
counter: AtomicU64::new(0),
98-
}
97+
ExampleContext { counter: AtomicU64::new(0) }
9998
}
10099
}
101100

@@ -109,7 +108,26 @@ struct CounterValue {
109108
counter: u64,
110109
}
111110

112-
/// Fetch the current value of the counter.
111+
/// Helper function for propagating a traceparent using hyper
112+
async fn traced_request(
113+
uri: &str,
114+
cx: &Context,
115+
) -> hyper::Request<Full<Bytes>> {
116+
let mut req = hyper::Request::builder()
117+
.uri(uri)
118+
.method(hyper::Method::GET)
119+
.header("accept", "application/json")
120+
.header("content-type", "application/json");
121+
global::get_text_map_propagator(|propagator| {
122+
propagator.inject_context(
123+
&cx,
124+
&mut HeaderInjector(req.headers_mut().unwrap()),
125+
)
126+
});
127+
req.body(Full::new(Bytes::from("".to_string()))).unwrap()
128+
}
129+
130+
/// Do a bunch of work to show off otel tracing
113131
#[endpoint {
114132
method = GET,
115133
path = "/get",
@@ -119,10 +137,11 @@ async fn example_api_get(
119137
rqctx: RequestContext<ExampleContext>,
120138
) -> Result<HttpResponseOk<CounterValue>, HttpError> {
121139
let trace_context = opentelemetry::Context::new();
122-
let parent_context = opentelemetry::trace::TraceContextExt::with_remote_span_context(
123-
&trace_context,
124-
rqctx.span_context.clone(),
125-
);
140+
let parent_context =
141+
opentelemetry::trace::TraceContextExt::with_remote_span_context(
142+
&trace_context,
143+
rqctx.span_context.clone(),
144+
);
126145

127146
let client = Client::builder(TokioExecutor::new()).build_http();
128147
let tracer = global::tracer("");
@@ -139,25 +158,48 @@ async fn example_api_get(
139158
.header("accept", "application/json")
140159
.header("content-type", "application/json");
141160
global::get_text_map_propagator(|propagator| {
142-
propagator.inject_context(&cx, &mut HeaderInjector(req.headers_mut().unwrap()))
161+
propagator.inject_context(
162+
&cx,
163+
&mut HeaderInjector(req.headers_mut().unwrap()),
164+
)
143165
});
144166
let _res = client
145167
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap())
146-
.await
147-
.unwrap();
168+
.await;
148169

149170
let mut req = hyper::Request::builder()
150171
.uri("http://localhost:4000/counter")
151172
.method(hyper::Method::GET)
152173
.header("accept", "application/json")
153174
.header("content-type", "application/json");
154175
global::get_text_map_propagator(|propagator| {
155-
propagator.inject_context(&cx, &mut HeaderInjector(req.headers_mut().unwrap()))
176+
propagator.inject_context(
177+
&cx,
178+
&mut HeaderInjector(req.headers_mut().unwrap()),
179+
)
156180
});
157181
let _res = client
158182
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap())
159-
.await
160-
.unwrap();
183+
.await;
184+
185+
let mut req = hyper::Request::builder()
186+
.uri("http://localhost:4000/does-not-exist")
187+
.method(hyper::Method::GET)
188+
.header("accept", "application/json")
189+
.header("content-type", "application/json")
190+
.header("user-agent", "dropshot-otel-example");
191+
global::get_text_map_propagator(|propagator| {
192+
propagator.inject_context(
193+
&cx,
194+
&mut HeaderInjector(req.headers_mut().unwrap()),
195+
)
196+
});
197+
let _res = client
198+
.request(req.body(Full::new(Bytes::from("".to_string()))).unwrap())
199+
.await;
200+
201+
let req = traced_request("http://localhost:4000/error", &cx).await;
202+
let _res = client.request(req).await;
161203

162204
let api_context = rqctx.context();
163205
Ok(HttpResponseOk(CounterValue {
@@ -180,6 +222,18 @@ async fn example_api_get_counter(
180222
}))
181223
}
182224

225+
/// Cause an error!
226+
#[endpoint {
227+
method = GET,
228+
path = "/error",
229+
}]
230+
async fn example_api_error(
231+
_rqctx: RequestContext<ExampleContext>,
232+
) -> Result<HttpResponseOk<CounterValue>, HttpError> {
233+
//XXX Why does this create a 499 rather than a 500 error???
234+
panic!("This handler is totally broken!");
235+
}
236+
183237
/// Update the current value of the counter. Note that the special value of 10
184238
/// is not allowed (just to demonstrate how to generate an error).
185239
#[endpoint {
@@ -200,9 +254,7 @@ async fn example_api_put_counter(
200254
format!("do not like the number {}", updated_value.counter),
201255
))
202256
} else {
203-
api_context
204-
.counter
205-
.store(updated_value.counter, Ordering::SeqCst);
257+
api_context.counter.store(updated_value.counter, Ordering::SeqCst);
206258
Ok(HttpResponseUpdatedNoContent())
207259
}
208260
}

0 commit comments

Comments
 (0)