Skip to content

Commit

Permalink
Hide addresses in telemetry
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Schoepp <[email protected]>
  • Loading branch information
calebschoepp committed Oct 7, 2024
1 parent 6143506 commit 7ee2579
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/factor-key-value/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl key_value::HostStore for KeyValueDispatch {
.await)
}

#[instrument(name = "spin_key_value.get", skip(self, store), err(level = Level::INFO), fields(otel.kind = "client"))]
#[instrument(name = "spin_key_value.get", skip(self, store, key), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn get(
&mut self,
store: Resource<key_value::Store>,
Expand All @@ -95,7 +95,7 @@ impl key_value::HostStore for KeyValueDispatch {
Ok(store.get(&key).await)
}

#[instrument(name = "spin_key_value.set", skip(self, store, value), err(level = Level::INFO), fields(otel.kind = "client"))]
#[instrument(name = "spin_key_value.set", skip(self, store, key, value), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn set(
&mut self,
store: Resource<key_value::Store>,
Expand All @@ -106,7 +106,7 @@ impl key_value::HostStore for KeyValueDispatch {
Ok(store.set(&key, &value).await)
}

#[instrument(name = "spin_key_value.delete", skip(self, store), err(level = Level::INFO), fields(otel.kind = "client"))]
#[instrument(name = "spin_key_value.delete", skip(self, store, key), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn delete(
&mut self,
store: Resource<key_value::Store>,
Expand All @@ -116,7 +116,7 @@ impl key_value::HostStore for KeyValueDispatch {
Ok(store.delete(&key).await)
}

#[instrument(name = "spin_key_value.exists", skip(self, store), err(level = Level::INFO), fields(otel.kind = "client"))]
#[instrument(name = "spin_key_value.exists", skip(self, store, key), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn exists(
&mut self,
store: Resource<key_value::Store>,
Expand Down
5 changes: 4 additions & 1 deletion crates/factor-outbound-mysql/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use spin_world::v1::mysql as v1;
use spin_world::v2::mysql::{self as v2, Connection};
use spin_world::v2::rdbms_types as v2_types;
use spin_world::v2::rdbms_types::ParameterValue;
use tracing::field::Empty;
use tracing::{instrument, Level};

use crate::client::Client;
Expand Down Expand Up @@ -38,8 +39,10 @@ impl<C: Client> v2::Host for InstanceState<C> {}

#[async_trait]
impl<C: Client> v2::HostConnection for InstanceState<C> {
#[instrument(name = "spin_outbound_mysql.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "mysql"))]
#[instrument(name = "spin_outbound_mysql.open", skip(self, address), err(level = Level::INFO), fields(otel.kind = "client", db.system = "mysql", db.address = Empty, server.port = Empty, db.namespace = Empty))]
async fn open(&mut self, address: String) -> Result<Resource<Connection>, v2::Error> {
spin_factor_outbound_networking::record_address_fields(&address);

if !self
.is_address_allowed(&address)
.await
Expand Down
20 changes: 20 additions & 0 deletions crates/factor-outbound-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use config::{
};

pub use runtime_config::ComponentTlsConfigs;
use url::Url;

pub type SharedFutureResult<T> = Shared<BoxFuture<'static, Result<Arc<T>, Arc<anyhow::Error>>>>;

Expand Down Expand Up @@ -248,3 +249,22 @@ impl<F: Fn(&str, &str) + Send + Sync> DisallowedHostHandler for F {
self(scheme, authority);
}
}

/// Records the address host, port, and database as fields on the current tracing span.
///
/// This should only be called from within a function that has been instrumented with a span.
///
/// The following fields must be pre-declared as empty on the span or they will not show up.
/// ```
/// use tracing::field::Empty;
/// #[tracing::instrument(fields(db.address = Empty, server.port = Empty, db.namespace = Empty))]
/// fn open() {}
/// ```
pub fn record_address_fields(address: &str) {
if let Ok(url) = Url::parse(address) {
let span = tracing::Span::current();
span.record("db.address", url.host_str().unwrap_or_default());
span.record("server.port", url.port().unwrap_or_default());
span.record("db.namespace", url.path().trim_start_matches('/'));
}
}
5 changes: 4 additions & 1 deletion crates/factor-outbound-pg/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use spin_world::v1::rdbms_types as v1_types;
use spin_world::v2::postgres::{self as v2, Connection};
use spin_world::v2::rdbms_types;
use spin_world::v2::rdbms_types::{ParameterValue, RowSet};
use tracing::field::Empty;
use tracing::instrument;
use tracing::Level;

Expand Down Expand Up @@ -63,8 +64,10 @@ impl<C: Send + Sync + Client> v2::Host for InstanceState<C> {}

#[async_trait]
impl<C: Send + Sync + Client> v2::HostConnection for InstanceState<C> {
#[instrument(name = "spin_outbound_pg.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "postgresql"))]
#[instrument(name = "spin_outbound_pg.open", skip(self, address), err(level = Level::INFO), fields(otel.kind = "client", db.system = "postgresql", db.address = Empty, server.port = Empty, db.namespace = Empty))]
async fn open(&mut self, address: String) -> Result<Resource<Connection>, v2::Error> {
spin_factor_outbound_networking::record_address_fields(&address);

if !self
.is_address_allowed(&address)
.await
Expand Down
5 changes: 4 additions & 1 deletion crates/factor-outbound-redis/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use spin_world::v1::{redis as v1, redis_types};
use spin_world::v2::redis::{
self as v2, Connection as RedisConnection, Error, RedisParameter, RedisResult,
};
use tracing::field::Empty;
use tracing::{instrument, Level};

pub struct InstanceState {
Expand Down Expand Up @@ -53,8 +54,10 @@ impl v2::Host for crate::InstanceState {

#[async_trait]
impl v2::HostConnection for crate::InstanceState {
#[instrument(name = "spin_outbound_redis.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "redis"))]
#[instrument(name = "spin_outbound_redis.open_connection", skip(self, address), err(level = Level::INFO), fields(otel.kind = "client", db.system = "redis", db.address = Empty, server.port = Empty, db.namespace = Empty))]
async fn open(&mut self, address: String) -> Result<Resource<RedisConnection>, Error> {
spin_factor_outbound_networking::record_address_fields(&address);

if !self
.is_address_allowed(&address)
.await
Expand Down

0 comments on commit 7ee2579

Please sign in to comment.