-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add Autometrics query types to Prometheus provider
- Loading branch information
Showing
11 changed files
with
905 additions
and
642 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,11 +33,11 @@ url = { version = "2.2.2", features = ["serde"] } | |
vergen = { version = "8.2.5", features = ["build", "git", "git2"] } | ||
|
||
[patch.crates-io] | ||
fiberplane-models = { git = "ssh://[email protected]/fiberplane/fiberplane.git", branch = "main" } | ||
fiberplane-provider-bindings = { git = "ssh://[email protected]/fiberplane/fiberplane.git", branch = "main" } | ||
#fiberplane-models = { git = "ssh://[email protected]/fiberplane/fiberplane.git", branch = "main" } | ||
#fiberplane-provider-bindings = { git = "ssh://[email protected]/fiberplane/fiberplane.git", branch = "main" } | ||
#fp-bindgen = { git = "ssh://[email protected]/fiberplane/fp-bindgen.git", branch = "release-3.0.0" } | ||
#fp-bindgen-support = { git = "ssh://[email protected]/fiberplane/fp-bindgen.git", branch = "release-3.0.0" } | ||
|
||
#[patch.'ssh://[email protected]/fiberplane/fiberplane.git'] | ||
#fiberplane-models = { path = "../fiberplane/fiberplane-models" } | ||
#fiberplane-provider-bindings = { path = "../fiberplane/fiberplane-provider-protocol/fiberplane-provider-bindings" } | ||
fiberplane-models = { path = "../fiberplane/fiberplane-models" } | ||
fiberplane-provider-bindings = { path = "../fiberplane/fiberplane-provider-protocol/fiberplane-provider-bindings" } |
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,38 @@ | ||
use fiberplane_models::autometrics::{AutometricsFunction, PrometheusResponse}; | ||
use fiberplane_pdk::prelude::*; | ||
use grafana_common::{query_direct_and_proxied, Config}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::BTreeSet; | ||
|
||
pub const ALL_FUNCTIONS_QUERY: &str = "x-autometrics-functions"; | ||
pub const CHILD_FUNCTIONS_QUERY: &str = "x-autometrics-child-functions"; | ||
|
||
pub const AUTOMETRICS_FUNCTIONS_MIME_TYPE: &str = "application/vnd.autometrics.functions"; | ||
|
||
#[derive(Deserialize, QuerySchema)] | ||
pub(crate) struct FunctionsQuery; | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, ProviderData, Serialize)] | ||
#[pdk(mime_type = AUTOMETRICS_FUNCTIONS_MIME_TYPE)] | ||
pub struct FunctionsVector(pub Vec<AutometricsFunction>); | ||
|
||
pub(crate) async fn query_all_functions(_query: FunctionsQuery, config: Config) -> Result<Blob> { | ||
let body = Blob::from({ | ||
let mut form_data = form_urlencoded::Serializer::new(String::new()); | ||
form_data.append_pair( | ||
"match[]", | ||
r#"{__name__=~"function_calls(_count)?(_total)?", function!="", module!=""}"#, | ||
); | ||
form_data | ||
}); | ||
|
||
let response: PrometheusResponse<Vec<AutometricsFunction>> = | ||
query_direct_and_proxied(&config, "prometheus", "/api/v1/series", Some(body)).await?; | ||
|
||
FunctionsVector(filter_unique_functions(response.data)).to_blob() | ||
} | ||
|
||
fn filter_unique_functions(functions: Vec<AutometricsFunction>) -> Vec<AutometricsFunction> { | ||
let unique_functions: BTreeSet<_> = functions.into_iter().collect(); | ||
unique_functions.into_iter().collect() | ||
} |
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,31 @@ | ||
use fiberplane_models::autometrics::PrometheusResponse; | ||
use fiberplane_models::MaybeSerializable; | ||
use fiberplane_pdk::prelude::*; | ||
use fp_bindgen::prelude::Serializable; | ||
use grafana_common::{query_direct_and_proxied, Config}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub const CONFIG_QUERY: &str = "x-prometheus-config"; | ||
|
||
pub const YAML_MIME_TYPE: &str = "text/yaml"; | ||
|
||
#[derive(Deserialize, QuerySchema)] | ||
pub(crate) struct ConfigQuery; | ||
|
||
#[derive(Clone, Deserialize, PartialEq, Serialize, Serializable)] | ||
pub struct ConfigYaml { | ||
yaml: String, | ||
} | ||
|
||
impl MaybeSerializable for ConfigYaml {} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, ProviderData, Serialize)] | ||
#[pdk(mime_type = YAML_MIME_TYPE)] | ||
pub struct Yaml(pub String); | ||
|
||
pub(crate) async fn query_config(_query: ConfigQuery, config: Config) -> Result<Blob> { | ||
let response: PrometheusResponse<ConfigYaml> = | ||
query_direct_and_proxied(&config, "prometheus", "/api/v1/status/config", None).await?; | ||
|
||
Yaml(response.data.yaml).to_blob() | ||
} |
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,91 @@ | ||
use super::instants::Instant; | ||
use fiberplane_pdk::prelude::Timestamp; | ||
use fiberplane_pdk::providers::*; | ||
use serde::Deserialize; | ||
use std::{ | ||
collections::BTreeMap, | ||
num::ParseFloatError, | ||
time::{Duration, SystemTime}, | ||
}; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PrometheusResponse { | ||
pub data: PrometheusData, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(tag = "resultType", content = "result", rename_all = "snake_case")] | ||
pub enum PrometheusData { | ||
Vector(Vec<InstantVector>), | ||
Matrix(Vec<RangeVector>), | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct InstantVector { | ||
pub metric: BTreeMap<String, String>, | ||
pub value: PrometheusPoint, | ||
} | ||
|
||
impl InstantVector { | ||
pub fn into_instant(self) -> Result<Instant, Error> { | ||
let mut labels = self.metric; | ||
let name = labels.remove("__name__").unwrap_or_else(|| "".to_owned()); | ||
let metric = self.value.to_metric()?; | ||
Ok(Instant { | ||
name, | ||
labels, | ||
metric, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Metadata { | ||
pub help: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct PrometheusMetadataResponse { | ||
pub data: BTreeMap<String, Vec<Metadata>>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct PrometheusPoint(f64, String); | ||
|
||
impl PrometheusPoint { | ||
pub fn to_metric(&self) -> Result<Metric, ParseFloatError> { | ||
let time = SystemTime::UNIX_EPOCH + Duration::from_millis((self.0 * 1000.0) as u64); | ||
Ok(Metric::builder() | ||
.time(Timestamp::from(time)) | ||
.value(self.1.parse()?) | ||
.otel(OtelMetadata::default()) | ||
.build()) | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct RangeVector { | ||
pub metric: BTreeMap<String, String>, | ||
pub values: Vec<PrometheusPoint>, | ||
} | ||
|
||
impl RangeVector { | ||
pub fn into_series(self) -> Result<Timeseries, Error> { | ||
let mut labels = self.metric; | ||
let name = labels.remove("__name__").unwrap_or_else(|| "".to_owned()); | ||
let metrics = self | ||
.values | ||
.into_iter() | ||
.map(|value| value.to_metric()) | ||
.collect::<Result<_, _>>()?; | ||
Ok(Timeseries::builder() | ||
.name(name) | ||
.labels(labels) | ||
.metrics(metrics) | ||
.otel(OtelMetadata::default()) | ||
.visible(true) | ||
.build()) | ||
} | ||
} |
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
Oops, something went wrong.