using ObservableGauge instrument with non trivial callback function (aync / blocking) #2205
Unanswered
pitoniak32
asked this question in
Q&A
Replies: 1 comment
-
Update:I got it "working", this will run the callbacks on the periodic metric interval and the results show correctly in my backend. But it feels really dirty, and hoping someone might be able to point to a cleaner option 😅 let healthcheck = HealthCheck {
url: "http://localhost:3000/_manage/health".to_string(),
metadata: HealthCheckMetaData {
team: "mine".to_string(),
feature: "test".to_string(),
},
};
meter
.u64_observable_gauge("health_check")
.with_callback(move |observer| {
let check = healthcheck.clone();
let url = healthcheck.url.clone();
let (tx, rx) = std::sync::mpsc::channel();
tokio::task::spawn_blocking(move || {
let res = reqwest::blocking::get(url)
.unwrap()
.json::<HealthCheckResult>()
.unwrap();
tx.send(res).unwrap();
});
let code = match rx.recv().unwrap().status {
HealthCheckStatus::Down => 0,
HealthCheckStatus::Up => 1,
HealthCheckStatus::Degraded => 2,
HealthCheckStatus::OutOfService => 3,
HealthCheckStatus::Unknown => 4,
};
println!("recording [{code}] - {} : {}", check.metadata.feature, check.url);
observer.observe(
code,
&[
KeyValue::new("team", check.metadata.team.clone()),
KeyValue::new("feature", check.metadata.feature.clone()),
],
);
}).init() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am looking to use the
InstrumentKind::ObservableGauge
to register some http requests, and record the results of them as a measurement. I was hoping I am missing something and there is an easy work around for this.I think this is related to: #1437
this is the closest I have gotten:
The issue I run into is allowing the observer to run in the async block / getting the result of the http response. If I remove the
tokio::task::spawn_blocking(...)
I get a runtime error for dropping the async runtime.Thank you in advance!!
Beta Was this translation helpful? Give feedback.
All reactions