Skip to content

Commit

Permalink
added stateCode
Browse files Browse the repository at this point in the history
  • Loading branch information
pompushko committed Sep 17, 2024
1 parent 96494d8 commit 1e43e57
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
24 changes: 13 additions & 11 deletions async_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
import httpx
from datetime import datetime, timedelta
from prometheus_client import Gauge, make_asgi_app, CollectorRegistry, CONTENT_TYPE_LATEST, generate_latest
from prometheus_client import Gauge, CollectorRegistry, CONTENT_TYPE_LATEST, generate_latest
import uvicorn
from fastapi import FastAPI, Request, Response
from contextlib import asynccontextmanager
Expand Down Expand Up @@ -74,6 +74,8 @@ async def fetch_data():
station_id = data.get('id', 'unknown')
station_name = status.get('description', 'unknown')

state_code = data.get('stateCode', 'unknown')

current_state = status.get('currentState', {})
current_value = current_state.get('value', 0)

Expand All @@ -83,11 +85,11 @@ async def fetch_data():
warning_val = status.get('warningValue', 0)

# Update metrics
metrics['current_state_value'].labels(station_id=station_id, station_name=station_name).set(current_value)
metrics['trend'].labels(station_id=station_id, station_name=station_name).set(trend_value)
metrics['status_metric'].labels(station_id=station_id, station_name=station_name).set(status_value)
metrics['alarm_value'].labels(station_id=station_id, station_name=station_name).set(alarm_val)
metrics['warning_value'].labels(station_id=station_id, station_name=station_name).set(warning_val)
metrics['current_state_value'].labels(station_id=station_id, station_name=station_name, state_code=state_code).set(current_value)
metrics['trend'].labels(station_id=station_id, station_name=station_name, state_code=state_code).set(trend_value)
metrics['status_metric'].labels(station_id=station_id, station_name=station_name, state_code=state_code).set(status_value)
metrics['alarm_value'].labels(station_id=station_id, station_name=station_name, state_code=state_code).set(alarm_val)
metrics['warning_value'].labels(station_id=station_id, station_name=station_name, state_code=state_code).set(warning_val)

logger.info(f"Metrics updated for station {station_id} - {station_name}")

Expand Down Expand Up @@ -116,11 +118,11 @@ async def lifespan(app: FastAPI):
global metrics
# Define metrics within the lifespan to ensure they're registered only once
metrics = {
'current_state_value': Gauge('current_state_value', 'Current state value', ['station_id', 'station_name'], registry=registry),
'trend': Gauge('trend', 'Trend', ['station_id', 'station_name'], registry=registry),
'status_metric': Gauge('status', 'Status', ['station_id', 'station_name'], registry=registry),
'alarm_value': Gauge('alarm_value', 'Alarm value', ['station_id', 'station_name'], registry=registry),
'warning_value': Gauge('warning_value', 'Warning value', ['station_id', 'station_name'], registry=registry),
'current_state_value': Gauge('current_state_value', 'Current state value', ['station_id', 'station_name', 'state_code'], registry=registry),
'trend': Gauge('trend', 'Trend', ['station_id', 'station_name', 'state_code'], registry=registry),
'status_metric': Gauge('status', 'Status', ['station_id', 'station_name', 'state_code'], registry=registry),
'alarm_value': Gauge('alarm_value', 'Alarm value', ['station_id', 'station_name', 'state_code'], registry=registry),
'warning_value': Gauge('warning_value', 'Warning value', ['station_id', 'station_name', 'state_code'], registry=registry),
}

# Startup code
Expand Down
77 changes: 77 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
networks:
grafnet:
name: grafnet
external: false
driver: bridge

services:
grafana:
image: grafana/grafana
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
- ./provisioning/:/etc/grafana/provisioning/
networks:
- grafnet
depends_on:
- prometheus
env_file:
- ./config.monitoring
restart: always

prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus/:/etc/prometheus/
- prometheus-data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
- '--web.external-url=https://prometheus.domain.com'
ports:
- 9090:9090
links:
- alertmanager:alertmanager
- imgw-exporter:imgw-exporter
depends_on:
- imgw-exporter
networks:
- grafnet
restart: always

alertmanager:
image: prom/alertmanager:latest
ports:
- 9093:9093
volumes:
- ./alertmanager/:/etc/alertmanager/
networks:
- grafnet
restart: always
command:
- '--config.file=/etc/alertmanager/config.yml'
- '--storage.path=/alertmanager'
- "--web.external-url=https://alertmanager.domain.com"

imgw-exporter:
image: pompushko/imgw-exporter:latest
environment:
- STATION_ID=151160170,151170030,151160190,150170040,151170050
ports:
- "9634:8000"
networks:
- grafnet
restart: always
deploy:
mode: global

volumes:
grafana-data:
name: grafana-data
external: false
prometheus-data:
name: prometheus-data
external: false

0 comments on commit 1e43e57

Please sign in to comment.