generated from Patagia/template-nix
Compare commits
2 commits
f1eaa440fc
...
32d0d2f02b
Author | SHA1 | Date | |
---|---|---|---|
32d0d2f02b | |||
3ccfb881b5 |
11 changed files with 119 additions and 71 deletions
46
Cargo.lock
generated
46
Cargo.lock
generated
|
@ -1319,13 +1319,15 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "instrumentation"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"http",
|
||||
"once_cell",
|
||||
"opentelemetry",
|
||||
"opentelemetry-appender-tracing",
|
||||
"opentelemetry-otlp",
|
||||
"opentelemetry-resource-detectors",
|
||||
"opentelemetry-semantic-conventions",
|
||||
"opentelemetry_sdk",
|
||||
"tonic",
|
||||
|
@ -1712,6 +1714,20 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "opentelemetry-appender-tracing"
|
||||
version = "0.27.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab5feffc321035ad94088a7e5333abb4d84a8726e54a802e736ce9dd7237e85b"
|
||||
dependencies = [
|
||||
"log",
|
||||
"opentelemetry",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "opentelemetry-otlp"
|
||||
version = "0.27.0"
|
||||
|
@ -1743,6 +1759,17 @@ dependencies = [
|
|||
"tonic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "opentelemetry-resource-detectors"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf6e83d3dca8fe93cfac95cd07313721c9e6e9f8655e7b06d061d4924df1a0cd"
|
||||
dependencies = [
|
||||
"opentelemetry",
|
||||
"opentelemetry-semantic-conventions",
|
||||
"opentelemetry_sdk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "opentelemetry-semantic-conventions"
|
||||
version = "0.27.0"
|
||||
|
@ -1813,7 +1840,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
|||
|
||||
[[package]]
|
||||
name = "patagia-agent"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
@ -1828,7 +1855,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "patagia-controller"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
@ -1844,6 +1871,7 @@ dependencies = [
|
|||
"trace-request",
|
||||
"tracing",
|
||||
"tracing-slog",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2776,10 +2804,12 @@ dependencies = [
|
|||
"sha2",
|
||||
"smallvec",
|
||||
"thiserror 2.0.9",
|
||||
"time",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
"webpki-roots",
|
||||
]
|
||||
|
||||
|
@ -2860,7 +2890,9 @@ dependencies = [
|
|||
"sqlx-core",
|
||||
"stringprep",
|
||||
"thiserror 2.0.9",
|
||||
"time",
|
||||
"tracing",
|
||||
"uuid",
|
||||
"whoami",
|
||||
]
|
||||
|
||||
|
@ -2897,7 +2929,9 @@ dependencies = [
|
|||
"sqlx-core",
|
||||
"stringprep",
|
||||
"thiserror 2.0.9",
|
||||
"time",
|
||||
"tracing",
|
||||
"uuid",
|
||||
"whoami",
|
||||
]
|
||||
|
||||
|
@ -2920,8 +2954,10 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_urlencoded",
|
||||
"sqlx-core",
|
||||
"time",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3356,7 +3392,7 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
|
|||
|
||||
[[package]]
|
||||
name = "trace-request"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"dropshot",
|
||||
"http",
|
||||
|
@ -3999,7 +4035,7 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
|
|||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.0.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
|
14
Cargo.toml
14
Cargo.toml
|
@ -34,12 +34,6 @@ clap = { version = "4.5.23", features = [
|
|||
dropshot = "0.15.1"
|
||||
http = "1.2.0"
|
||||
once_cell = "1.20.2"
|
||||
opentelemetry = "0.27.1"
|
||||
opentelemetry-appender-tracing = { version = "0.27.0", features = ["log", "experimental_metadata_attributes"] }
|
||||
opentelemetry-otlp = { version = "0.27.0", features = ["grpc-tonic", "gzip-tonic", "zstd-tonic", "tls", "tls-roots", "trace"] }
|
||||
opentelemetry_sdk = { version = "0.27.1", features = ["metrics", "rt-tokio"] }
|
||||
opentelemetry-semantic-conventions = "0.27.0"
|
||||
opentelemetry-stdout = "0.27.0"
|
||||
progenitor = "0.8.0"
|
||||
reqwest = { version = "0.12.12", features = ["json", "stream", "rustls-tls"] }
|
||||
schemars = "0.8.21"
|
||||
|
@ -48,16 +42,8 @@ serde = { version = "1.0.217", features = ["derive"] }
|
|||
slog = "2.7.0"
|
||||
slog-async = "2.8.0"
|
||||
tokio = { version = "1.42.0", features = ["full"] }
|
||||
tonic = "0.12.3"
|
||||
tracing = "0.1.41"
|
||||
tracing-core = "0.1.33"
|
||||
tracing-chrome = "0.7.2"
|
||||
tracing-opentelemetry = "0.28.0"
|
||||
tracing-slog = { git = "https://github.com/oxidecomputer/tracing-slog", default-features = false }
|
||||
tracing-subscriber = { version = "0.3.19", default-features = false, features = [
|
||||
"std",
|
||||
"ansi",
|
||||
"env-filter",
|
||||
"fmt",
|
||||
] }
|
||||
uuid = { version = "1", features = [ "serde", "v4" ] }
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[package]
|
||||
name = "patagia-agent"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
[package]
|
||||
name = "patagia-controller"
|
||||
description = "Patagia controller server"
|
||||
version = "0.1.0"
|
||||
description = "Patagia control plane server"
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
|
@ -15,11 +15,12 @@ schemars.workspace = true
|
|||
serde.workspace = true
|
||||
slog-async.workspace = true
|
||||
slog.workspace = true
|
||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio", "tls-rustls"] }
|
||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio", "tls-rustls", "time", "uuid"] }
|
||||
tokio.workspace = true
|
||||
trace-request = { path = "../trace-request" }
|
||||
tracing-slog.workspace = true
|
||||
tracing.workspace = true
|
||||
uuid.workspace = true
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["http"]
|
||||
|
|
|
@ -18,7 +18,7 @@ struct Cli {
|
|||
#[arg(
|
||||
long = "telemetry-otlp-endpoint",
|
||||
default_value = "http://localhost:4317",
|
||||
env = "OTEL_EXPORTER_OTLP_ENDPOINT",
|
||||
env = "OTEL_EXPORTER_OTLP_ENDPOINT"
|
||||
)]
|
||||
otlp_endpoint: Option<String>,
|
||||
|
||||
|
@ -68,9 +68,9 @@ async fn main() -> Result<()> {
|
|||
|
||||
tracing::info!(database_url, listen_address=args.listen_address, "Starting server");
|
||||
|
||||
let _pool = PgPool::connect(&database_url).await?;
|
||||
let pg = PgPool::connect(&database_url).await?;
|
||||
|
||||
let ctx = ControllerContext::new();
|
||||
let ctx = ControllerContext::new(pg);
|
||||
let api = api::api()?;
|
||||
ServerBuilder::new(api, Arc::new(ctx), logger)
|
||||
.config(config)
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
pub struct ControllerContext {}
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
pub struct ControllerContext {
|
||||
pub pg_pool: PgPool,
|
||||
}
|
||||
|
||||
impl ControllerContext {
|
||||
pub fn new() -> ControllerContext {
|
||||
ControllerContext {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ControllerContext {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
pub fn new(pg_pool : PgPool) -> ControllerContext {
|
||||
ControllerContext {
|
||||
pg_pool
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use dropshot::{endpoint, HttpError, HttpResponseOk, Path, RequestContext};
|
|||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use trace_request::trace_request;
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -10,7 +11,7 @@ use crate::context::ControllerContext;
|
|||
/// User
|
||||
#[derive(Serialize, JsonSchema)]
|
||||
struct User {
|
||||
id: String,
|
||||
id: Uuid,
|
||||
name: String,
|
||||
}
|
||||
|
||||
|
@ -18,10 +19,9 @@ struct User {
|
|||
#[derive(Deserialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct UsersPathParams {
|
||||
user_id: String,
|
||||
user_id: Uuid,
|
||||
}
|
||||
|
||||
|
||||
/// Fetch user info.
|
||||
#[endpoint {
|
||||
method = GET,
|
||||
|
@ -33,13 +33,23 @@ pub(crate) async fn get_user_by_id(
|
|||
rqctx: RequestContext<Arc<ControllerContext>>,
|
||||
params: Path<UsersPathParams>,
|
||||
) -> Result<HttpResponseOk<User>, HttpError> {
|
||||
|
||||
let id = params.into_inner().user_id;
|
||||
tracing::debug!(id, "Getting user by id");
|
||||
tracing::debug!(id = id.to_string(), "Getting user by id");
|
||||
|
||||
let pg = rqctx.context().pg_pool.to_owned();
|
||||
|
||||
let rec = sqlx::query!(r#"SELECT * FROM users WHERE id = $1"#, id)
|
||||
.fetch_one(&pg)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::RowNotFound => HttpError::for_not_found(None, format!("User not found by id: {:?}", id)),
|
||||
err => HttpError::for_internal_error( format!("Error: {}", err))
|
||||
}
|
||||
)?;
|
||||
|
||||
let user = User {
|
||||
id: "00000000-0000-0000-0000-000000000000".to_string(),
|
||||
name: "Sune Testsson".to_string(),
|
||||
id: rec.id,
|
||||
name: rec.name,
|
||||
};
|
||||
|
||||
Ok(HttpResponseOk(user))
|
||||
|
|
|
@ -1,17 +1,24 @@
|
|||
[package]
|
||||
name = "instrumentation"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
http.workspace = true
|
||||
once_cell.workspace = true
|
||||
opentelemetry-otlp.workspace = true
|
||||
opentelemetry_sdk.workspace = true
|
||||
opentelemetry-semantic-conventions.workspace = true
|
||||
opentelemetry.workspace = true
|
||||
tonic.workspace = true
|
||||
tracing-opentelemetry.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
opentelemetry-otlp = { version = "0.27.0", features = ["grpc-tonic", "gzip-tonic", "zstd-tonic", "tls", "tls-roots", "trace"] }
|
||||
opentelemetry_sdk = { version = "0.27.1", features = ["metrics", "rt-tokio"] }
|
||||
opentelemetry-semantic-conventions = "0.27.0"
|
||||
opentelemetry-appender-tracing = { version = "0.27.0", features = ["log", "experimental_metadata_attributes"] }
|
||||
opentelemetry-resource-detectors = { version = "0.6.0" }
|
||||
opentelemetry = "0.27.1"
|
||||
tonic = "0.12.3"
|
||||
tracing-opentelemetry = "0.28.0"
|
||||
tracing-subscriber = { version = "0.3.19", default-features = false, features = [
|
||||
"std",
|
||||
"ansi",
|
||||
"env-filter",
|
||||
"fmt",
|
||||
] }
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use once_cell::sync::Lazy;
|
||||
use opentelemetry::{trace::TracerProvider as _, KeyValue};
|
||||
use opentelemetry_otlp::{WithExportConfig, WithTonicConfig};
|
||||
use opentelemetry_sdk::{
|
||||
|
@ -9,25 +8,15 @@ use opentelemetry_sdk::{
|
|||
trace::{RandomIdGenerator, Sampler, TracerProvider},
|
||||
Resource,
|
||||
};
|
||||
use opentelemetry_semantic_conventions as semcov;
|
||||
use tracing_opentelemetry::{MetricsLayer, OpenTelemetryLayer};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
static RESOURCE: Lazy<Resource> = Lazy::new(|| {
|
||||
Resource::new(vec![
|
||||
KeyValue::new(
|
||||
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
|
||||
env!("CARGO_PKG_NAME"),
|
||||
),
|
||||
KeyValue::new(
|
||||
opentelemetry_semantic_conventions::resource::SERVICE_VERSION,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
),
|
||||
])
|
||||
});
|
||||
use std::time::Duration;
|
||||
|
||||
// Construct MeterProvider for MetricsLayer
|
||||
fn init_meter_provider(otel_endpoint: &String) -> Result<SdkMeterProvider> {
|
||||
fn init_meter_provider(otel_endpoint: &String, resource: Resource) -> Result<SdkMeterProvider> {
|
||||
let exporter = opentelemetry_otlp::MetricExporter::builder()
|
||||
.with_tonic()
|
||||
.with_endpoint(otel_endpoint)
|
||||
|
@ -38,7 +27,7 @@ fn init_meter_provider(otel_endpoint: &String) -> Result<SdkMeterProvider> {
|
|||
.map_err(|e| anyhow!("Error creating OTLP metric exporter: {:?}", e))?;
|
||||
|
||||
let meter_provider = MeterProviderBuilder::default()
|
||||
.with_resource(RESOURCE.clone())
|
||||
.with_resource(resource)
|
||||
.with_reader(
|
||||
PeriodicReader::builder(exporter, runtime::Tokio)
|
||||
.with_interval(std::time::Duration::from_secs(10))
|
||||
|
@ -52,7 +41,7 @@ fn init_meter_provider(otel_endpoint: &String) -> Result<SdkMeterProvider> {
|
|||
}
|
||||
|
||||
// Construct TracerProvider for OpenTelemetryLayer
|
||||
fn init_tracer_provider(otel_endpoint: &String) -> Result<TracerProvider> {
|
||||
fn init_tracer_provider(otel_endpoint: &String, resource: Resource) -> Result<TracerProvider> {
|
||||
let exporter = opentelemetry_otlp::SpanExporter::builder()
|
||||
.with_tonic()
|
||||
.with_tls_config(tonic::transport::ClientTlsConfig::new().with_native_roots())
|
||||
|
@ -65,7 +54,7 @@ fn init_tracer_provider(otel_endpoint: &String) -> Result<TracerProvider> {
|
|||
.with_sampler(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
|
||||
1.0,
|
||||
))))
|
||||
.with_resource(RESOURCE.clone())
|
||||
.with_resource(resource)
|
||||
.with_id_generator(RandomIdGenerator::default())
|
||||
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
|
||||
.build();
|
||||
|
@ -75,6 +64,25 @@ fn init_tracer_provider(otel_endpoint: &String) -> Result<TracerProvider> {
|
|||
|
||||
// Initialize tracing-subscriber and return TracingGuard for opentelemetry-related termination processing
|
||||
pub fn init_tracing(otel_endpoint: Option<&String>, log_stderr: bool) -> Result<TracingGuard> {
|
||||
let resource = {
|
||||
let r = Resource::new([KeyValue::new(
|
||||
semcov::resource::SERVICE_VERSION,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
)]);
|
||||
|
||||
let detected = Resource::from_detectors(
|
||||
Duration::from_secs(5),
|
||||
vec![
|
||||
Box::new(opentelemetry_sdk::resource::SdkProvidedResourceDetector),
|
||||
Box::new(opentelemetry_sdk::resource::EnvResourceDetector::new()),
|
||||
Box::new(opentelemetry_resource_detectors::OsResourceDetector),
|
||||
Box::new(opentelemetry_resource_detectors::ProcessResourceDetector),
|
||||
Box::new(opentelemetry_sdk::resource::TelemetryResourceDetector),
|
||||
],
|
||||
);
|
||||
r.merge(&detected)
|
||||
};
|
||||
|
||||
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(""));
|
||||
|
||||
|
@ -84,7 +92,7 @@ pub fn init_tracing(otel_endpoint: Option<&String>, log_stderr: bool) -> Result<
|
|||
};
|
||||
|
||||
let meter_provider = match otel_endpoint {
|
||||
Some(endpoint) => Some(init_meter_provider(endpoint)?),
|
||||
Some(endpoint) => Some(init_meter_provider(endpoint, resource.clone())?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
@ -94,7 +102,7 @@ pub fn init_tracing(otel_endpoint: Option<&String>, log_stderr: bool) -> Result<
|
|||
};
|
||||
|
||||
let tracer_provider = match otel_endpoint {
|
||||
Some(endpoint) => Some(init_tracer_provider(endpoint)?),
|
||||
Some(endpoint) => Some(init_tracer_provider(endpoint, resource)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[package]
|
||||
name = "trace-request"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "xtask"
|
||||
version = "0.0.0"
|
||||
edition = "2021"
|
||||
version.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "xtask"
|
||||
|
|
Loading…
Reference in a new issue