Skip to content

Commit 88419ce

Browse files
authored
Merge pull request #3624 from ChihweiLHBird/zhiwei/optimization-1
Miscellaneous performance optimizations
2 parents 1a419a8 + f2cec2e commit 88419ce

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

crates/key-value-spin/src/store.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ impl Store for SqliteStore {
159159
}
160160

161161
async fn exists(&self, key: &str) -> Result<bool, Error> {
162-
Ok(self.get(key, usize::MAX).await?.is_some())
162+
task::block_in_place(|| {
163+
self.connection
164+
.lock()
165+
.unwrap()
166+
.prepare_cached("SELECT 1 FROM spin_key_value WHERE store=$1 AND key=$2 LIMIT 1")
167+
.map_err(log_error)?
168+
.exists([&self.name, key])
169+
.map_err(log_error)
170+
})
163171
}
164172

165173
async fn get_keys(&self, max_result_bytes: usize) -> Result<Vec<String>, Error> {

crates/telemetry/src/env.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::env::VarError;
1+
use std::{env::VarError, sync::OnceLock};
22

33
use opentelemetry_otlp::{
44
OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
@@ -47,11 +47,16 @@ pub fn otel_metrics_enabled() -> bool {
4747
/// - `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`
4848
///
4949
/// Note that this is overridden if OTEL_SDK_DISABLED is set and not empty.
50+
///
51+
/// The result is cached after the first call because this is checked on every guest log write.
5052
pub fn otel_logs_enabled() -> bool {
51-
any_vars_set(&[
52-
OTEL_EXPORTER_OTLP_ENDPOINT,
53-
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
54-
]) && !otel_sdk_disabled()
53+
static ENABLED: OnceLock<bool> = OnceLock::new();
54+
*ENABLED.get_or_init(|| {
55+
any_vars_set(&[
56+
OTEL_EXPORTER_OTLP_ENDPOINT,
57+
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
58+
]) && !otel_sdk_disabled()
59+
})
5560
}
5661

5762
/// Returns a boolean indicating if the compatibility layer that emits tracing events from

crates/trigger-http/src/server.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub struct HttpServer<F: RuntimeFactors> {
9898
router: Router,
9999
/// The app being triggered.
100100
trigger_app: Arc<TriggerApp<F>>,
101+
/// The application name, resolved once for use as the `app_id` telemetry attribute.
102+
app_id: Arc<str>,
101103
// Component ID -> component trigger config
102104
component_trigger_configs: HashMap<spin_http::routes::TriggerLookupKey, HttpTriggerConfig>,
103105
// Component ID -> handler type
@@ -158,6 +160,12 @@ impl<F: RuntimeFactors> HttpServer<F> {
158160

159161
let trigger_app = Arc::new(trigger_app);
160162

163+
let app_id: Arc<str> = trigger_app
164+
.app()
165+
.get_metadata(APP_NAME_KEY)?
166+
.unwrap_or_else(|| "<unnamed>".into())
167+
.into();
168+
161169
let component_handler_types = component_trigger_configs
162170
.iter()
163171
.filter_map(|(key, trigger_config)| match key {
@@ -180,6 +188,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
180188
find_free_port,
181189
router,
182190
trigger_app,
191+
app_id,
183192
http1_max_buf_size,
184193
component_trigger_configs,
185194
component_handler_types,
@@ -350,18 +359,12 @@ impl<F: RuntimeFactors> HttpServer<F> {
350359
client_addr: SocketAddr,
351360
) -> anyhow::Result<Response<Body>> {
352361
set_req_uri(&mut req, server_scheme)?;
353-
let app_id = self
354-
.trigger_app
355-
.app()
356-
.get_metadata(APP_NAME_KEY)?
357-
.unwrap_or_else(|| "<unnamed>".into());
358-
359362
let lookup_key = route_match.lookup_key();
360363

361364
spin_telemetry::metrics::counter!(
362365
spin.request_count = 1,
363366
trigger_type = "http",
364-
app_id = app_id,
367+
app_id = self.app_id.clone(),
365368
component_id = lookup_key.to_string()
366369
);
367370

0 commit comments

Comments
 (0)