rc #7
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "exporter"
|
name = "exporter"
|
||||||
version = "0.1.0"
|
version = "0.2.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
||||||
|
|
@ -7,23 +7,18 @@ use crate::structs::v3::PrometheusMetrics;
|
||||||
use prometheus::{ Encoder, Gauge, Registry, TextEncoder};
|
use prometheus::{ Encoder, Gauge, Registry, TextEncoder};
|
||||||
use std::sync::{ Arc, MutexGuard };
|
use std::sync::{ Arc, MutexGuard };
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
use tracing::{ error, debug, info, warn };
|
use tracing::{ error, info, warn };
|
||||||
use crate::metrics::{MetricsProcesser, MetricsValueType};
|
use crate::metrics::{MetricsProcesser, MetricsValueType};
|
||||||
|
|
||||||
pub async fn update_metrics(
|
pub async fn update_metrics(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(request) : Json<PrometheusMetrics>
|
Json(request) : Json<PrometheusMetrics<'_>>
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
info!("post on /update");
|
info!("post on /update");
|
||||||
// let resp = Response::new("body");
|
|
||||||
// debug!("{:?}", request);
|
|
||||||
// debug!("{:?}", MetricsProcesser::get_type_of_value(&request));
|
|
||||||
let service = &request.service_name;
|
let service = &request.service_name;
|
||||||
let endpoint = &request.endpoint_name;
|
let endpoint = &request.endpoint_name;
|
||||||
|
|
||||||
for i in request.metrics {
|
for i in request.metrics {
|
||||||
// debug!("{:?}", &i);
|
|
||||||
// debug!("{:?}", MetricsProcesser::get_type_of_value(&i));
|
|
||||||
let metric_name = format!("{}_{}_{}", service, endpoint, &i.id);
|
let metric_name = format!("{}_{}_{}", service, endpoint, &i.id);
|
||||||
match MetricsProcesser::get_type_of_value(&i) {
|
match MetricsProcesser::get_type_of_value(&i) {
|
||||||
MetricsValueType::Array |
|
MetricsValueType::Array |
|
||||||
|
|
@ -33,7 +28,8 @@ pub async fn update_metrics(
|
||||||
MetricsValueType::Number => {
|
MetricsValueType::Number => {
|
||||||
let gauge = MetricsProcesser::gauge_from_number(
|
let gauge = MetricsProcesser::gauge_from_number(
|
||||||
&i,
|
&i,
|
||||||
&metric_name
|
&metric_name,
|
||||||
|
&i.desc.clone().unwrap_or_else(|| std::borrow::Cow::Borrowed(&i.id))
|
||||||
);
|
);
|
||||||
if let Some(gauge) = gauge {
|
if let Some(gauge) = gauge {
|
||||||
match state.registry.lock() {
|
match state.registry.lock() {
|
||||||
|
|
@ -50,7 +46,6 @@ pub async fn update_metrics(
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// dbg!(gauge);
|
|
||||||
},
|
},
|
||||||
MetricsValueType::ArrayOfStrings => {
|
MetricsValueType::ArrayOfStrings => {
|
||||||
warn!("String arrays are unsupported, ignoring ...");
|
warn!("String arrays are unsupported, ignoring ...");
|
||||||
|
|
|
||||||
|
|
@ -39,10 +39,11 @@ impl MetricsProcesser {
|
||||||
pub fn gauge_from_number(
|
pub fn gauge_from_number(
|
||||||
metric: &MetricOutput,
|
metric: &MetricOutput,
|
||||||
metric_name: &str,
|
metric_name: &str,
|
||||||
|
metric_desc: &str
|
||||||
) -> Option<Gauge> {
|
) -> Option<Gauge> {
|
||||||
let gauge = Gauge::new(
|
let gauge = Gauge::new(
|
||||||
metric_name,
|
metric_name,
|
||||||
&metric.id
|
metric_desc
|
||||||
);
|
);
|
||||||
|
|
||||||
match gauge {
|
match gauge {
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,28 @@
|
||||||
// use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use std::borrow::Cow;
|
||||||
// use anyhow::Result;
|
// use anyhow::Result;
|
||||||
// use std::sync::Arc;
|
// use std::sync::Arc;
|
||||||
|
|
||||||
pub mod v3 {
|
pub mod v3 {
|
||||||
pub use super::*;
|
pub use super::*;
|
||||||
// to prometheus and nmns
|
// to prometheus and nmns
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct MetricOutput {
|
pub struct MetricOutput<'a> {
|
||||||
pub id : String,
|
pub id : String,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
json_type : String,
|
json_type : String,
|
||||||
addr : String,
|
addr : String,
|
||||||
pub value : Value,
|
pub value : Value,
|
||||||
|
#[serde(rename = "description")]
|
||||||
|
pub desc : Option<Cow<'a, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct PrometheusMetrics {
|
pub struct PrometheusMetrics<'a> {
|
||||||
pub service_name: String,
|
pub service_name: String,
|
||||||
pub endpoint_name: String,
|
pub endpoint_name: String,
|
||||||
pub metrics: Vec<MetricOutput>,
|
pub metrics: Vec<MetricOutput<'a>>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue