Compare commits

..

No commits in common. "e5b8bfff991c2290c0cf5af0084f6e57ecba2464" and "442c937e063ab8148477cdc180b0339261b3782c" have entirely different histories.

3 changed files with 14 additions and 13 deletions

View File

@ -7,18 +7,23 @@ 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, info, warn }; use tracing::{ error, debug, 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 |
@ -28,8 +33,7 @@ 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() {
@ -46,6 +50,7 @@ pub async fn update_metrics(
}, },
} }
} }
// dbg!(gauge);
}, },
MetricsValueType::ArrayOfStrings => { MetricsValueType::ArrayOfStrings => {
warn!("String arrays are unsupported, ignoring ..."); warn!("String arrays are unsupported, ignoring ...");

View File

@ -39,11 +39,10 @@ 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_desc &metric.id
); );
match gauge { match gauge {

View File

@ -1,28 +1,25 @@
// 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, Clone)] #[derive(Serialize, Deserialize, Debug)]
pub struct MetricOutput<'a> { pub struct MetricOutput {
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<'a> { pub struct PrometheusMetrics {
pub service_name: String, pub service_name: String,
pub endpoint_name: String, pub endpoint_name: String,
pub metrics: Vec<MetricOutput<'a>>, pub metrics: Vec<MetricOutput>,
} }
} }