Compare commits

..

No commits in common. "d5f0f470d61f14c41b0935bce5b676e72d4c586c" and "fbc24660ca513ffb7c652f5b9eb1051dc0b2af9b" have entirely different histories.

4 changed files with 15 additions and 14 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "exporter"
version = "0.2.3"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -7,18 +7,23 @@ use crate::structs::v3::PrometheusMetrics;
use prometheus::{ Encoder, Gauge, Registry, TextEncoder};
use std::sync::{ Arc, MutexGuard };
use crate::AppState;
use tracing::{ error, info, warn };
use tracing::{ error, debug, info, warn };
use crate::metrics::{MetricsProcesser, MetricsValueType};
pub async fn update_metrics(
State(state): State<Arc<AppState>>,
Json(request) : Json<PrometheusMetrics<'_>>
Json(request) : Json<PrometheusMetrics>
) -> impl IntoResponse {
info!("post on /update");
// let resp = Response::new("body");
// debug!("{:?}", request);
// debug!("{:?}", MetricsProcesser::get_type_of_value(&request));
let service = &request.service_name;
let endpoint = &request.endpoint_name;
for i in request.metrics {
// debug!("{:?}", &i);
// debug!("{:?}", MetricsProcesser::get_type_of_value(&i));
let metric_name = format!("{}_{}_{}", service, endpoint, &i.id);
match MetricsProcesser::get_type_of_value(&i) {
MetricsValueType::Array |
@ -28,8 +33,7 @@ pub async fn update_metrics(
MetricsValueType::Number => {
let gauge = MetricsProcesser::gauge_from_number(
&i,
&metric_name,
&i.desc.clone().unwrap_or_else(|| std::borrow::Cow::Borrowed(&i.id))
&metric_name
);
if let Some(gauge) = gauge {
match state.registry.lock() {
@ -46,6 +50,7 @@ pub async fn update_metrics(
},
}
}
// dbg!(gauge);
},
MetricsValueType::ArrayOfStrings => {
warn!("String arrays are unsupported, ignoring ...");

View File

@ -39,11 +39,10 @@ impl MetricsProcesser {
pub fn gauge_from_number(
metric: &MetricOutput,
metric_name: &str,
metric_desc: &str
) -> Option<Gauge> {
let gauge = Gauge::new(
metric_name,
metric_desc
&metric.id
);
match gauge {

View File

@ -1,28 +1,25 @@
// use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use serde_json::Value;
use std::borrow::Cow;
// use anyhow::Result;
// use std::sync::Arc;
pub mod v3 {
pub use super::*;
// to prometheus and nmns
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MetricOutput<'a> {
#[derive(Serialize, Deserialize, Debug)]
pub struct MetricOutput {
pub id : String,
#[serde(rename = "type")]
json_type : String,
addr : String,
pub value : Value,
#[serde(rename = "description")]
pub desc : Option<Cow<'a, String>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct PrometheusMetrics<'a> {
pub struct PrometheusMetrics {
pub service_name: String,
pub endpoint_name: String,
pub metrics: Vec<MetricOutput<'a>>,
pub metrics: Vec<MetricOutput>,
}
}