-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send anonymized telemetry to scarf.sh
- Loading branch information
1 parent
558f2dd
commit f7f2db9
Showing
7 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) 2023 - 2025 Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
use std::{str::FromStr, time::Duration}; | ||
|
||
use http::{uri::PathAndQuery, HeaderMap, HeaderValue, Uri}; | ||
use restate_core::{cancellation_watcher, TaskCenter, TaskKind}; | ||
use restate_service_client::HttpClient; | ||
use restate_types::config::CommonOptions; | ||
use tokio::time::Instant; | ||
use tracing::debug; | ||
|
||
use crate::build_info::{ | ||
RESTATE_SERVER_DEBUG, RESTATE_SERVER_TARGET_TRIPLE, RESTATE_SERVER_VERSION, | ||
}; | ||
|
||
static TELEMETRY_URI: &str = "https://restate.gateway.scarf.sh/restate-server/"; | ||
const TELEMETRY_PERIOD: Duration = Duration::from_secs(3600 * 24); | ||
|
||
pub enum Telemetry { | ||
Enabled(TelemetryEnabled), | ||
Disabled, | ||
} | ||
|
||
pub struct TelemetryEnabled { | ||
client: HttpClient, | ||
user_agent: String, | ||
start_time: Instant, | ||
} | ||
|
||
impl Telemetry { | ||
pub fn create(options: &CommonOptions) -> Self { | ||
if options.disable_telemetry { | ||
Self::Disabled | ||
} else { | ||
let client = HttpClient::from_options(&options.service_client.http); | ||
let session_id = ulid::Ulid::new().to_string(); | ||
let user_agent = format!("restate-server {session_id}"); | ||
|
||
Self::Enabled(TelemetryEnabled { | ||
client, | ||
user_agent, | ||
start_time: Instant::now(), | ||
}) | ||
} | ||
} | ||
|
||
pub fn start(self) { | ||
match self { | ||
Telemetry::Disabled => {} | ||
Telemetry::Enabled(enabled) => { | ||
if let Err(err) = | ||
TaskCenter::spawn_child(TaskKind::RoleRunner, "telemetry-service", async { | ||
let cancelled = cancellation_watcher(); | ||
|
||
tokio::select! { | ||
result = enabled.run() => { | ||
result | ||
} | ||
_ = cancelled =>{ | ||
} | ||
} | ||
|
||
Ok(()) | ||
}) | ||
{ | ||
debug!(error = %err, "Failed to start telemetry service"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl TelemetryEnabled { | ||
async fn run(self) { | ||
let mut interval = tokio::time::interval(TELEMETRY_PERIOD); | ||
loop { | ||
interval.tick().await; // first tick completes immediately | ||
self.send_telemetry().await; | ||
} | ||
} | ||
|
||
async fn send_telemetry(&self) { | ||
let uptime_hours = | ||
(Instant::now().duration_since(self.start_time).as_secs() / 3600).to_string(); | ||
|
||
let uri = Uri::from_str(&format!( | ||
"{TELEMETRY_URI}?target={RESTATE_SERVER_TARGET_TRIPLE}&version={RESTATE_SERVER_VERSION}&debug={RESTATE_SERVER_DEBUG}&uptime={uptime_hours}" | ||
)).expect("uri must parse"); | ||
|
||
debug!(%uri, user_agent = %self.user_agent, "Sending telemetry data"); | ||
|
||
match self | ||
.client | ||
.request( | ||
uri, | ||
None, | ||
http::Method::GET, | ||
http_body_util::Empty::new(), | ||
PathAndQuery::from_static("/"), | ||
HeaderMap::from_iter([( | ||
http::header::USER_AGENT, | ||
HeaderValue::from_str(&self.user_agent) | ||
.expect("user agent must be a valid header value"), | ||
)]), | ||
) | ||
.await | ||
{ | ||
Ok(resp) => { | ||
debug!(status = %resp.status(), "Sent telemetry data") | ||
} | ||
Err(err) => { | ||
debug!(error = %err, "Failed to send telemetry data") | ||
} | ||
} | ||
} | ||
} |