Skip to content

Commit

Permalink
feat(outbound-mqtt): Trace outbound-mqtt host component
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Schoepp <[email protected]>
  • Loading branch information
calebschoepp committed Apr 8, 2024
1 parent d08b9c7 commit 4c416ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/outbound-mqtt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ spin-outbound-networking = { path = "../outbound-networking" }
table = { path = "../table" }
tokio = { version = "1", features = ["sync"] }
tracing = { workspace = true }

[lints]
workspace = true
10 changes: 10 additions & 0 deletions crates/outbound-mqtt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use spin_core::{async_trait, wasmtime::component::Resource};
use spin_world::v2::mqtt::{self as v2, Connection as MqttConnection, Error, Qos};

pub use host_component::OutboundMqttComponent;
use tracing::{instrument, Level};

pub struct OutboundMqtt {
allowed_hosts: spin_outbound_networking::AllowedHostsConfig,
Expand Down Expand Up @@ -59,6 +60,7 @@ impl v2::Host for OutboundMqtt {}

#[async_trait]
impl v2::HostConnection for OutboundMqtt {
#[instrument(name = "spin_outbound_mqtt.open_connection", skip(self, password), err(level = Level::INFO), fields(otel.kind = "client"))]
async fn open(
&mut self,
address: String,
Expand All @@ -80,6 +82,14 @@ impl v2::HostConnection for OutboundMqtt {
.await
}

/// Publish a message to the MQTT broker.
///
/// OTEL trace propagation is not directly supported in MQTT V3. You will need to embed the
/// current trace context into the payload yourself.
/// https://w3c.github.io/trace-context-mqtt/#mqtt-v3-recommendation.
#[instrument(name = "spin_outbound_mqtt.publish", skip(self, connection, payload), err(level = Level::INFO),
fields(otel.kind = "producer", otel.name = format!("{} publish", topic), messaging.operation = "publish",
messaging.system = "mqtt"))]
async fn publish(
&mut self,
connection: Resource<MqttConnection>,
Expand Down
1 change: 1 addition & 0 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c416ae

Please sign in to comment.