Skip to content

Commit

Permalink
Upgrade to OTel v0.23.0
Browse files Browse the repository at this point in the history
This required re-working how the spin-telemetry logging layer works.

Signed-off-by: Caleb Schoepp <caleb.schoepp@fermyon.com>
  • Loading branch information
calebschoepp committed Sep 16, 2024
1 parent 4770c9f commit 951ff41
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ anyhow = "1.0.75"
conformance-tests = { git = "https://github.com/fermyon/conformance-tests", rev = "387b7f375df59e6254a7c29cf4a53507a9f46d32" }
http-body-util = "0.1.0"
hyper = { version = "1.0.0", features = ["full"] }
opentelemetry = { version = "0.22.0", features = ["metrics", "trace", "logs"] }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio", "logs_level_enabled", "metrics"] }
opentelemetry = { version = "0.23.0", features = ["metrics", "trace", "logs"] }
opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio", "logs_level_enabled", "metrics"] }
reqwest = { version = "0.12", features = ["stream", "blocking"] }
# In `rustls` turn off the `aws_lc_rs` default feature and turn on `ring`.
# If both `aws_lc_rs` and `ring` are enabled, a panic at runtime will occur.
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "logging", "tls12"] }
test-environment = { git = "https://github.com/fermyon/conformance-tests", rev = "387b7f375df59e6254a7c29cf4a53507a9f46d32" }
tracing = { version = "0.1", features = ["log"] }
tracing-opentelemetry = { version = "0.23.0", default-features = false, features = ["metrics"] }
tracing-opentelemetry = { version = "0.24.0", default-features = false, features = ["metrics"] }

wasi-common-preview1 = { version = "22.0.0", package = "wasi-common", features = [
"tokio",
Expand Down
5 changes: 3 additions & 2 deletions crates/telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ edition = { workspace = true }
anyhow = { workspace = true }
http0 = { version = "0.2.9", package = "http" }
http1 = { version = "1.0.0", package = "http" }
once_cell = "1.19.0"
opentelemetry = { workspace = true }
opentelemetry-otlp = { version = "0.15.0", default-features = false, features = ["http-proto", "trace", "http", "reqwest-client", "metrics", "grpc-tonic", "logs"] }
opentelemetry-semantic-conventions = "0.14.0"
opentelemetry_sdk = { workspace = true }
opentelemetry-otlp = { version = "0.16.0", default-features = false, features = ["http-proto", "trace", "http", "reqwest-client", "metrics", "grpc-tonic", "logs"] }
opentelemetry-semantic-conventions = "0.14.0"
terminal = { path = "../terminal" }
tracing = { version = "0.1.37", features = ["log"] }
tracing-appender = "0.2.2"
Expand Down
38 changes: 19 additions & 19 deletions crates/telemetry/src/logs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{ascii::escape_default, sync::OnceLock, time::Duration};

use anyhow::bail;
use opentelemetry::{
global,
logs::{Logger, LoggerProvider},
};
use opentelemetry::logs::{LogRecord, Logger, LoggerProvider};
use opentelemetry_otlp::LogExporterBuilder;
use opentelemetry_sdk::{
logs::{BatchConfigBuilder, BatchLogProcessor},
logs::{BatchConfigBuilder, BatchLogProcessor, Logger as SdkLogger},
resource::{EnvResourceDetector, TelemetryResourceDetector},
Resource,
};
Expand All @@ -17,6 +14,8 @@ use crate::{
env::{self, otel_logs_enabled, OtlpProtocol},
};

static LOGGER: OnceLock<SdkLogger> = OnceLock::new();

/// Handle an application log. Has the potential to both forward the log to OTel and to emit it as a
/// tracing event.
pub fn handle_app_log(buf: &[u8]) {
Expand All @@ -30,20 +29,19 @@ fn app_log_to_otel(buf: &[u8]) {
return;
}

let logger = global::logger_provider().logger("spin");
if let Ok(s) = std::str::from_utf8(buf) {
logger.emit(
opentelemetry::logs::LogRecord::builder()
.with_body(s.to_owned())
.build(),
);
if let Some(logger) = LOGGER.get() {
if let Ok(s) = std::str::from_utf8(buf) {
let mut record = logger.create_log_record();
record.set_body(s.to_string().into());
logger.emit(record);
} else {
let mut record = logger.create_log_record();
record.set_body(escape_non_utf8_buf(buf).into());
record.add_attribute("app_log_non_utf8", true);
logger.emit(record);
}
} else {
logger.emit(
opentelemetry::logs::LogRecord::builder()
.with_body(escape_non_utf8_buf(buf))
.with_attribute("app_log_non_utf8", true)
.build(),
);
tracing::trace!("OTel logger not initialized, failed to log");
}
}

Expand Down Expand Up @@ -106,7 +104,9 @@ pub(crate) fn init_otel_logging_backend(spin_version: String) -> anyhow::Result<
)
.build();

global::set_logger_provider(provider);
LOGGER
.set(provider.logger("spin"))
.expect("shouldn't be set concurrently");

Ok(())
}

0 comments on commit 951ff41

Please sign in to comment.