diff --git a/CHANGELOG.md b/CHANGELOG.md index 413a642..38c8121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 0.3.0 + +- `service.name` must be sent vs `service_name` now for consistency +- a `PATCH` to update the `ttl` will extend from the current span duration. e.g. patching 10000 to a span that has been running for 34125ms will set the ttl to 44125 +- added `test.sh` to run through the operations and expiration behaviour + +# 0.2.1 + +- API changed to use HTTP verbs for crud + # 0.1.0 - First release diff --git a/Cargo.lock b/Cargo.lock index 91dbf97..71599f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1317,7 +1317,7 @@ dependencies = [ [[package]] name = "testevents" -version = "0.2.1" +version = "0.3.0" dependencies = [ "anyhow", "axum", diff --git a/Cargo.toml b/Cargo.toml index b5d4180..9672655 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "testevents" -version = "0.2.1" +version = "0.3.0" edition = "2021" authors = ["Jeremy Blythe "] repository = "https://github.com/jerbly/testevents" diff --git a/README.md b/README.md index 09b6fdf..196e408 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A service to make it easier to create Honeycomb spans in some restricted environ Spans are "closed" with a `DELETE` to `/{trace_id}/{span_id}/` - this creates the Honeycomb event with a calculated `duration_ms` from the "open" call. -`PATCH` to `/{trace_id}/{span_id}/` can be used to merge a set of key-values into the "open" span. Existing keys will have their values overwritten. You can change the `ttl` to extend or shorten the time to expiry. +`PATCH` to `/{trace_id}/{span_id}/` can be used to merge a set of key-values into the "open" span. Existing keys will have their values overwritten. You can change the `ttl` to extend or shorten the time to expiry. e.g. patching 10000 to a span that has been running for 34125ms will set the ttl to 44125. ## Installing @@ -41,7 +41,7 @@ Make a root span: curl -i -X POST \ 'http://127.0.0.1:3003/' \ -H 'Content-Type: application/json' \ - -d '{"service_name":"jerbly-test", + -d '{"service.name":"jerbly-test", "name":"test", "hello":"world", "ttl":600000}' @@ -60,7 +60,7 @@ Make a child span: curl -i -X POST \ 'http://127.0.0.1:3003/4c278c122e123f87036b44772861b9f4/5370f70191c4b03c/' \ -H 'Content-Type: application/json' \ - -d '{"service_name":"jerbly-test", + -d '{"service.name":"jerbly-test", "name":"test_child", "hello":"child span", "ttl":600000}' diff --git a/src/main.rs b/src/main.rs index 3a3cc9f..c956fb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,7 @@ async fn main() -> anyhow::Result<()> { // run it let bind_port = std::env::var("TESTEVENTS_PORT").unwrap_or("3003".to_string()); let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{bind_port}")).await?; + println!("testevents version: {}", env!("CARGO_PKG_VERSION")); println!("listening on {}", listener.local_addr()?); axum::serve(listener, app).await?; diff --git a/src/span_store.rs b/src/span_store.rs index 80a025b..4e1fcfb 100644 --- a/src/span_store.rs +++ b/src/span_store.rs @@ -113,13 +113,20 @@ impl SpanAttributes { .unwrap_or_default() .clone_into(&mut self.service_name), "status_code" => self.status_code = value.as_i64().unwrap_or_default(), - "ttl" => self.ttl = value.as_i64().unwrap_or_default(), + "ttl" => self.set_ttl(value.as_i64().unwrap_or_default()), _ => { self.extra.insert(key, value); } } } } + + fn set_ttl(&mut self, ttl: i64) { + // set a new ttl which is millis from timestamp + incoming ttl + let now = Utc::now().timestamp_millis(); + let current_used_millis = now - self.timestamp.timestamp_millis(); + self.ttl = current_used_millis + ttl; + } } #[derive(Debug, Serialize, Clone)] @@ -159,6 +166,7 @@ impl SpanAttributes { #[derive(Deserialize, Debug)] pub struct QueryAttributes { + #[serde(rename = "service.name")] service_name: String, name: String, ttl: Option, diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..17aef66 --- /dev/null +++ b/test.sh @@ -0,0 +1,25 @@ +#!/bin/bash +url="http://localhost:3003" + +echo "Make a root span" +response=$(curl -s -X POST $url/ -H 'Content-Type: application/json' -d '{"service.name":"jerbly-test","name":"test","hello":"world","ttl":3600000}') +root_span_id=$(echo $response | jq -r '.span_id') +root_trace_id=$(echo $response | jq -r '.trace_id') +sleep 1 + +echo "Make a child span" +response=$(curl -s -X POST $url/$root_trace_id/$root_span_id/ -H 'Content-Type: application/json' -d '{"service.name":"jerbly-test","name":"child","ttl":3600000}') +span_id=$(echo $response | jq -r '.span_id') +sleep 1 + +echo "Patch the child span with a new value and a shorter ttl - and let it timeout" +curl -s -X PATCH $url/$root_trace_id/$span_id/ -H 'Content-Type: application/json' -d '{"new":true,"ttl":1000}' +sleep 2 + +echo "" +echo "Check for a 404 on an attempt to DELETE the expired child span" +response=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE $url/$root_trace_id/$span_id/) +echo "Status code: $response" + +echo "Close the root span" +curl -s -X DELETE $url/$root_trace_id/$root_span_id/