Skip to content

Commit 7ee2579

Browse files
committed
Hide addresses in telemetry
Signed-off-by: Caleb Schoepp <[email protected]>
1 parent 6143506 commit 7ee2579

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

crates/factor-key-value/src/host.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl key_value::HostStore for KeyValueDispatch {
8585
.await)
8686
}
8787

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

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

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

119-
#[instrument(name = "spin_key_value.exists", skip(self, store), err(level = Level::INFO), fields(otel.kind = "client"))]
119+
#[instrument(name = "spin_key_value.exists", skip(self, store, key), err(level = Level::INFO), fields(otel.kind = "client"))]
120120
async fn exists(
121121
&mut self,
122122
store: Resource<key_value::Store>,

crates/factor-outbound-mysql/src/host.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use spin_world::v1::mysql as v1;
55
use spin_world::v2::mysql::{self as v2, Connection};
66
use spin_world::v2::rdbms_types as v2_types;
77
use spin_world::v2::rdbms_types::ParameterValue;
8+
use tracing::field::Empty;
89
use tracing::{instrument, Level};
910

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

3940
#[async_trait]
4041
impl<C: Client> v2::HostConnection for InstanceState<C> {
41-
#[instrument(name = "spin_outbound_mysql.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "mysql"))]
42+
#[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))]
4243
async fn open(&mut self, address: String) -> Result<Resource<Connection>, v2::Error> {
44+
spin_factor_outbound_networking::record_address_fields(&address);
45+
4346
if !self
4447
.is_address_allowed(&address)
4548
.await

crates/factor-outbound-networking/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use config::{
2121
};
2222

2323
pub use runtime_config::ComponentTlsConfigs;
24+
use url::Url;
2425

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

@@ -248,3 +249,22 @@ impl<F: Fn(&str, &str) + Send + Sync> DisallowedHostHandler for F {
248249
self(scheme, authority);
249250
}
250251
}
252+
253+
/// Records the address host, port, and database as fields on the current tracing span.
254+
///
255+
/// This should only be called from within a function that has been instrumented with a span.
256+
///
257+
/// The following fields must be pre-declared as empty on the span or they will not show up.
258+
/// ```
259+
/// use tracing::field::Empty;
260+
/// #[tracing::instrument(fields(db.address = Empty, server.port = Empty, db.namespace = Empty))]
261+
/// fn open() {}
262+
/// ```
263+
pub fn record_address_fields(address: &str) {
264+
if let Ok(url) = Url::parse(address) {
265+
let span = tracing::Span::current();
266+
span.record("db.address", url.host_str().unwrap_or_default());
267+
span.record("server.port", url.port().unwrap_or_default());
268+
span.record("db.namespace", url.path().trim_start_matches('/'));
269+
}
270+
}

crates/factor-outbound-pg/src/host.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use spin_world::v1::rdbms_types as v1_types;
55
use spin_world::v2::postgres::{self as v2, Connection};
66
use spin_world::v2::rdbms_types;
77
use spin_world::v2::rdbms_types::{ParameterValue, RowSet};
8+
use tracing::field::Empty;
89
use tracing::instrument;
910
use tracing::Level;
1011

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

6465
#[async_trait]
6566
impl<C: Send + Sync + Client> v2::HostConnection for InstanceState<C> {
66-
#[instrument(name = "spin_outbound_pg.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "postgresql"))]
67+
#[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))]
6768
async fn open(&mut self, address: String) -> Result<Resource<Connection>, v2::Error> {
69+
spin_factor_outbound_networking::record_address_fields(&address);
70+
6871
if !self
6972
.is_address_allowed(&address)
7073
.await

crates/factor-outbound-redis/src/host.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use spin_world::v1::{redis as v1, redis_types};
66
use spin_world::v2::redis::{
77
self as v2, Connection as RedisConnection, Error, RedisParameter, RedisResult,
88
};
9+
use tracing::field::Empty;
910
use tracing::{instrument, Level};
1011

1112
pub struct InstanceState {
@@ -53,8 +54,10 @@ impl v2::Host for crate::InstanceState {
5354

5455
#[async_trait]
5556
impl v2::HostConnection for crate::InstanceState {
56-
#[instrument(name = "spin_outbound_redis.open_connection", skip(self), err(level = Level::INFO), fields(otel.kind = "client", db.system = "redis"))]
57+
#[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))]
5758
async fn open(&mut self, address: String) -> Result<Resource<RedisConnection>, Error> {
59+
spin_factor_outbound_networking::record_address_fields(&address);
60+
5861
if !self
5962
.is_address_allowed(&address)
6063
.await

0 commit comments

Comments
 (0)