The OpenTelemetry transport automatically participates in distributed traces. When you start an active span in your application, the email sending operation (e.g., transport.send()) will automatically create a child span that inherits the current trace context. This allows you to visualize the relationship between a high-level business operation and the resulting email delivery.
import { trace } from "@opentelemetry/api";
import { createMessage } from "@upyo/core";
import { MailgunTransport } from "@upyo/mailgun";
import { createOpenTelemetryTransport } from "@upyo/opentelemetry";
const transport = createOpenTelemetryTransport(
new MailgunTransport({
apiKey: "your-api-key",
domain: "mg.example.com",
}),
{
serviceName: "user-service",
tracing: {
enabled: true,
recordSensitiveData: false,
},
}
);
const tracer = trace.getTracer("user-registration");
await tracer.startActiveSpan("user-registration", async (span) => {
try {
span.setAttributes({
"user.id": "12345",
"user.email": "newuser@example.com",
});
const message = createMessage({
from: "welcome@example.com",
to: "newuser@example.com",
subject: "Welcome to our platform",
content: { text: "Thank you for joining us!" },
});
// This automatically becomes a child span
await transport.send(message);
span.setStatus({ code: 1 }); // OK
} catch (error) {
if (error instanceof Error) {
span.recordException(error);
}
span.setStatus({ code: 2, message: String(error) }); // ERROR
throw error;
} finally {
span.end();
}
});