Ship traces to Honeycomb.io.

This commit is contained in:
Lyle Mantooth 2023-05-14 21:50:23 -04:00
parent c813356eaa
commit bb32083d2c
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
3 changed files with 796 additions and 41 deletions

756
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,8 @@ artem = { version = "=1.1.5", default-features = false }
axum = "0.6"
color-eyre = "0.6"
image = "0.24"
opentelemetry = { version = "0.18", features = ["rt-tokio"] }
opentelemetry-honeycomb = { git = "https://github.com/fasterthanlime/opentelemetry-honeycomb-rs", branch = "simplified", version = "0.1.0" }
pretty-hex = "0.3"
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1", features = ["derive"] }

View file

@ -8,6 +8,11 @@ use axum::{
routing::get,
Router,
};
use opentelemetry::{
global,
trace::{get_active_span, FutureExt, Span, Status, TraceContextExt, Tracer},
Context, KeyValue,
};
use reqwest::StatusCode;
use serde::Deserialize;
use tracing::{info, Level};
@ -20,6 +25,12 @@ struct ServerState {
#[tokio::main]
async fn main() {
let (_honeyguard, _tracer) = opentelemetry_honeycomb::new_pipeline(
std::env::var("HONEYCOMB_API_KEY").expect("$HONEYCOMB_API_KEY should be set"),
"catscii".into(),
)
.install()
.unwrap();
let filter = Targets::from_str(std::env::var("RUST_LOG").as_deref().unwrap_or("info"))
.expect("RUST_LOG should be a valid tracing filter");
tracing_subscriber::fmt()
@ -43,8 +54,31 @@ async fn main() {
.unwrap();
}
async fn root_get(_headers: HeaderMap, State(state): State<ServerState>) -> Response<BoxBody> {
match get_cat_ascii_art(&state.client).await {
async fn root_get(headers: HeaderMap, State(state): State<ServerState>) -> Response<BoxBody> {
let tracer = global::tracer("");
let mut span = tracer.start("root_get");
span.set_attribute(KeyValue::new(
"user_agent",
headers
.get(header::USER_AGENT)
.map(|h| h.to_str().unwrap_or_default().to_owned())
.unwrap_or_default(),
));
root_get_inner(state)
.with_context(Context::current_with_span(span))
.await
}
async fn root_get_inner(state: ServerState) -> Response<BoxBody> {
let tracer = global::tracer("");
match get_cat_ascii_art(&state.client)
.with_context(Context::current_with_span(
tracer.start("get_cat_arscii_art"),
))
.await
{
Ok(art) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
@ -52,25 +86,46 @@ async fn root_get(_headers: HeaderMap, State(state): State<ServerState>) -> Resp
)
.into_response(),
Err(e) => {
println!("Something went wrong: {e}");
get_active_span(|span| {
span.set_status(Status::Error {
description: format!("{e}").into(),
})
});
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong").into_response()
}
}
}
async fn get_cat_ascii_art(client: &reqwest::Client) -> color_eyre::Result<String> {
let image_url = get_cat_image_url(client).await?;
let tracer = global::tracer("");
let image_bytes = download_file(client, &image_url).await?;
let image_url = get_cat_image_url(client)
.with_context(Context::current_with_span(
tracer.start("get_cat_image_url"),
))
.await?;
let image = image::load_from_memory(&image_bytes)?;
let image_bytes = download_file(client, &image_url)
.with_context(Context::current_with_span(tracer.start("download_file")))
.await?;
let ascii_art = artem::convert(
image,
artem::options::OptionBuilder::new()
.target(artem::options::TargetType::HtmlFile(true, true))
.build(),
);
let image = tracer.in_span("image::load_from_memory", |cx| {
let img = image::load_from_memory(&image_bytes)?;
cx.span()
.set_attribute(KeyValue::new("width", img.width() as i64));
cx.span()
.set_attribute(KeyValue::new("height", img.height() as i64));
Ok::<_, color_eyre::eyre::Report>(img)
})?;
let ascii_art = tracer.in_span("artem::convert", |_cx| {
artem::convert(
image,
artem::options::OptionBuilder::new()
.target(artem::options::TargetType::HtmlFile(true, true))
.build(),
)
});
Ok(ascii_art)
}