Skip to content

Commit

Permalink
Merge pull request #156 from crabnebula-dev/jonas/refactor/metrics
Browse files Browse the repository at this point in the history
refactor: remove specialized Metrics struct
  • Loading branch information
lucasfernog-crabnebula authored Dec 11, 2023
2 parents e590910 + 05f0f85 commit d149193
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 83 deletions.
2 changes: 1 addition & 1 deletion devtools/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Builder {
// This is pretty ugly code I know, but it looks nice in the terminal soo ¯\_(ツ)_/¯
fn print_link(addr: &SocketAddr) {
let url = if option_env!("__DEVTOOLS_LOCAL_DEVELOPMENT").is_some() {
"http://localhost:5173/dash/"
"http://localhost:5173/app/dash/"
} else {
"https://devtools.crabnebula.dev/app/dash/"
};
Expand Down
41 changes: 3 additions & 38 deletions devtools/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use devtools_wire_format::tauri::{
use futures::{FutureExt, Stream, TryStreamExt};
use std::net::SocketAddr;
use std::path::{Component, PathBuf};
use std::sync::Arc;
use tauri::http::header::HeaderValue;
use tauri::{AppHandle, Runtime};
use tokio::sync::{mpsc, RwLock};
use tokio::sync::mpsc;
use tonic::codegen::http::Method;
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
use tonic::codegen::BoxStream;
Expand Down Expand Up @@ -56,7 +55,6 @@ struct InstrumentService {

struct TauriService<R: Runtime> {
app_handle: AppHandle<R>,
metrics: Arc<RwLock<Metrics>>,
}

struct SourcesService<R: Runtime> {
Expand All @@ -68,11 +66,7 @@ struct MetaService<R: Runtime> {
}

impl<R: Runtime> Server<R> {
pub fn new(
cmd_tx: mpsc::Sender<Command>,
app_handle: AppHandle<R>,
metrics: Arc<RwLock<Metrics>>,
) -> Self {
pub fn new(cmd_tx: mpsc::Sender<Command>, app_handle: AppHandle<R>) -> Self {
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();

health_reporter
Expand All @@ -91,7 +85,6 @@ impl<R: Runtime> Server<R> {
},
tauri: TauriService {
app_handle: app_handle.clone(),
metrics,
}, // the TauriServer doesn't need a health_reporter. It can never fail.
meta: MetaService {
app_handle: app_handle.clone(),
Expand Down Expand Up @@ -203,9 +196,7 @@ impl<R: Runtime> tauri_server::Tauri for TauriService<R> {
&self,
_req: Request<MetricsRequest>,
) -> Result<Response<Metrics>, Status> {
let metrics = self.metrics.read().await;

Ok(Response::new(metrics.clone()))
Ok(Response::new(Metrics::default()))
}
}

Expand Down Expand Up @@ -424,13 +415,11 @@ mod test {
use devtools_wire_format::sources::sources_server::Sources;
use devtools_wire_format::tauri::tauri_server::Tauri;
use futures::StreamExt;
use std::time::SystemTime;

#[tokio::test]
async fn tauri_get_config() {
let tauri = TauriService {
app_handle: tauri::test::mock_app().handle(),
metrics: Default::default(),
};

let cfg = tauri
Expand All @@ -444,30 +433,6 @@ mod test {
);
}

#[tokio::test]
async fn tauri_get_metrics() {
let srv = TauriService {
app_handle: tauri::test::mock_app().handle(),
metrics: Default::default(),
};

let metrics = srv
.get_metrics(Request::new(MetricsRequest {}))
.await
.unwrap();
assert_eq!(metrics.into_inner(), *srv.metrics.read().await);

let mut m = srv.metrics.write().await;
m.initialized_at = Some(SystemTime::now().into());
drop(m);

let metrics = srv
.get_metrics(Request::new(MetricsRequest {}))
.await
.unwrap();
assert_eq!(metrics.into_inner(), *srv.metrics.read().await);
}

#[tokio::test]
async fn subscription() {
let (health_reporter, _) = tonic_health::server::health_reporter();
Expand Down
26 changes: 4 additions & 22 deletions devtools/src/tauri_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
use crate::aggregator::Aggregator;
use crate::server::Server;
use crate::Command;
use devtools_wire_format::tauri::Metrics;
use std::net::SocketAddr;
use std::sync::Arc;
use std::thread;
use std::time::SystemTime;
use std::time::{Duration, Instant};
use tauri::{RunEvent, Runtime};
use tokio::sync::{mpsc, RwLock};
use std::time::Duration;
use tauri::Runtime;
use tokio::sync::mpsc;

pub(crate) fn init<R: Runtime>(
addr: SocketAddr,
publish_interval: Duration,
aggregator: Aggregator,
cmd_tx: mpsc::Sender<Command>,
) -> tauri::plugin::TauriPlugin<R> {
let now = Instant::now();
let metrics = Arc::new(RwLock::new(Metrics {
initialized_at: Some(aggregator.base_time.to_timestamp(now)), // TODO this is horrific
ready_at: None,
}));

let m = metrics.clone();
tauri::plugin::Builder::new("probe")
.setup(move |app_handle| {
let server = Server::new(cmd_tx, app_handle.clone(), m);
let server = Server::new(cmd_tx, app_handle.clone());

// spawn the server and aggregator in a separate thread
// so we don't interfere with the application we're trying to instrument
Expand All @@ -51,13 +41,5 @@ pub(crate) fn init<R: Runtime>(

Ok(())
})
.on_event(move |_, event| {
if let RunEvent::Ready = event {
let mut metrics = metrics.blocking_write();
metrics.ready_at = Some(SystemTime::now().into());

tracing::debug!("Application is ready");
}
})
.build()
}
19 changes: 0 additions & 19 deletions web-client/src/components/boot-time.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions web-client/src/views/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Outlet, useParams } from "@solidjs/router";
import { Navigation } from "~/components/navigation";
import { BootTime } from "~/components/boot-time";
import { HealthStatus } from "~/components/health-status.tsx";
import { Logo } from "~/components/crabnebula-logo";
import { DisconnectButton } from "~/components/disconnect-button";
Expand All @@ -18,7 +17,6 @@ export default function Layout() {
<header class="grid">
<div class="border-b border-gray-800 flex px-2 py-1 items-center justify-between">
<HealthStatus />
<BootTime />
<DisconnectButton />
</div>
<Navigation />
Expand Down
4 changes: 3 additions & 1 deletion wire/proto/tauri.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ service Tauri {

rpc GetConfig(ConfigRequest) returns (Config) {}

rpc GetMetrics(MetricsRequest) returns (Metrics) {}
rpc GetMetrics(MetricsRequest) returns (Metrics) {
option deprecated = true;
}
}

message VersionsRequest {}
Expand Down

0 comments on commit d149193

Please sign in to comment.