Skip to content

Commit fe5fbc6

Browse files
committed
feat(grafana): compelete grafana data sources
1 parent aa387a4 commit fe5fbc6

File tree

9 files changed

+237
-20
lines changed

9 files changed

+237
-20
lines changed

app/media/MyGrafana/mysql.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/media/MyGrafana/tempo.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: 1
2+
datasources:
3+
- name: Tempo
4+
type: tempo
5+
access: proxy
6+
orgId: 1
7+
url: http://tempo-query-frontend.tempo.svc.cluster.local:3100
8+
basicAuth: false
9+
version: 1
10+
editable: true
11+
apiVersion: 1
12+
uid: tempo
13+
jsonData:
14+
httpMethod: GET
15+
serviceMap:
16+
datasourceUid: Mimir-OtelMetrics-Tenant
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Optional
2+
from pydantic import BaseModel, validator, ValidationError
3+
4+
5+
6+
7+
class PostgresInput(BaseModel):
8+
name:str = "Postgres"
9+
url:str = "localhost:5432"
10+
user:str = "grafana"
11+
editable: bool = True
12+
database:str = "grafana"
13+
sslmode:str = "disable"
14+
password:str = "Password!"
15+
maxOpenConns:int = 100
16+
maxIdleConns:int = 100
17+
maxIdleConnsAuto:bool = True
18+
connMaxLifetime:int = 14400
19+
postgresVersion:int = 903
20+
timescaledb:bool = False
21+
22+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List, Optional
2+
from pydantic import BaseModel, validator, ValidationError
3+
4+
5+
class TraceID(BaseModel):
6+
datasourceUid:str = "my_jaeger_uid"
7+
name:str = "traceID"
8+
class PrometheusInput(BaseModel):
9+
name:str = "Prometheus"
10+
url:str = "http://localhost:9090"
11+
editable: bool = True
12+
httpMethod:str = "POST"
13+
manageAlerts:bool = True
14+
prometheusType:str = "Prometheus"
15+
prometheusVersion:str = "2.44.0"
16+
cacheLevel:str = 'High'
17+
disableRecordingRules:bool = False
18+
incrementalQueryOverlapWindow:str = "10m"
19+
trace_id: TraceID

app/models/grafana/tempo_models.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Optional, List
2+
from pydantic import BaseModel,PrivateAttr,Field
3+
4+
class TracesToLogsV2(BaseModel):
5+
datasourceUid: str = 'loki'
6+
spanStartTimeShift: str = '-2m'
7+
spanEndTimeShift: str = '2m'
8+
filterByTraceID: bool = True
9+
filterBySpanID: bool = True
10+
11+
class ServiceMap(BaseModel):
12+
datasourceUid: str = 'Mimir-OtelMetrics-Tenant'
13+
14+
class NodeGraph(BaseModel):
15+
enabled: bool = True
16+
17+
class JsonData(BaseModel):
18+
httpMethod: str = 'GET'
19+
tracesToLogsV2: Optional[TracesToLogsV2] = TracesToLogsV2()
20+
serviceMap: Optional[ServiceMap] = ServiceMap()
21+
nodeGraph: Optional[NodeGraph] = NodeGraph()
22+
23+
class Datasource(BaseModel):
24+
name: str = 'Tempo'
25+
type: str = Field(default='tempo')
26+
access: str = Field(default="proxy")
27+
orgId: int = Field(default=1)
28+
url: str = 'http://tempo-query-frontend.tempo.svc.cluster.local:3100'
29+
basicAuth: bool = False
30+
version: int = Field(default=1)
31+
editable: bool = True
32+
apiVersion: int = Field(default=1)
33+
uid: str = Field(default="tempo")
34+
jsonData: JsonData = JsonData()
35+
36+
class TempoInput(BaseModel):
37+
apiVersion: int = Field(default=1)
38+
datasources: List[Datasource] = [Datasource()]
39+

app/routes/grafana_data_sources.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from app.app_instance import app
2-
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput,MimirInput,MysqlInput)
2+
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput,MimirInput,MysqlInput,PostgresInput,
3+
PrometheusInput,TempoInput)
34
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template
45
from app.template_generators.grafana_data_sources.elasticsearch import elasticsearch_template
56
from app.template_generators.grafana_data_sources.loki import loki_template
67
from app.template_generators.grafana_data_sources.mimir import mimir_template
78
from app.template_generators.grafana_data_sources.mysql import mysql_template
9+
from app.template_generators.grafana_data_sources.postgresql import postgres_template
10+
from app.template_generators.grafana_data_sources.prometheus import pormetheus_template
11+
from app.template_generators.grafana_data_sources.tempo import tempo_template
812
import shutil
913
import os
1014

@@ -62,4 +66,38 @@ async def mysql_template_route(request:MysqlInput) -> Output:
6266

6367
mysql_template(request)
6468

69+
return Output(output='output')
70+
71+
@app.post("/api/grafana/postgres")
72+
async def postgres_template_route(request:PostgresInput) -> Output:
73+
74+
dir = 'app/media/MyGrafana'
75+
if os.path.exists(dir):
76+
shutil.rmtree(dir)
77+
78+
postgres_template(request)
79+
80+
return Output(output='output')
81+
82+
@app.post("/api/grafana/prometheus")
83+
async def prometheus_template_route(request:PrometheusInput) -> Output:
84+
85+
dir = 'app/media/MyGrafana'
86+
if os.path.exists(dir):
87+
shutil.rmtree(dir)
88+
89+
pormetheus_template(request)
90+
91+
return Output(output='output')
92+
93+
94+
@app.post("/api/grafana/tempo")
95+
async def tempo_template_route(request:TempoInput) -> Output:
96+
97+
dir = 'app/media/MyGrafana'
98+
if os.path.exists(dir):
99+
shutil.rmtree(dir)
100+
101+
tempo_template(request)
102+
65103
return Output(output='output')
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import yaml
2+
import os
3+
4+
def postgres_template(input):
5+
json_template = {
6+
"apiVersion": 1,
7+
"datasources": [
8+
{
9+
"name": input.name,
10+
"type": "postgres",
11+
"url": input.url,
12+
"user": input.user,
13+
"editable": input.editable,
14+
"secureJsonData": {
15+
"password": input.password
16+
},
17+
"jsonData": {
18+
"database": input.database,
19+
"sslmode": input.sslmode,
20+
"maxOpenConns": input.maxOpenConns,
21+
"maxIdleConns": input.maxIdleConns,
22+
"maxIdleConnsAuto": input.maxIdleConnsAuto,
23+
"connMaxLifetime": input.connMaxLifetime,
24+
"postgresVersion": input.postgresVersion,
25+
"timescaledb": input.timescaledb
26+
}
27+
}
28+
]
29+
}
30+
31+
32+
dir = "app/media/MyGrafana"
33+
os.makedirs(dir)
34+
os.path.join(dir, 'postgresql.yml')
35+
36+
file=open("app/media/MyGrafana/postgresql.yml","w")
37+
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)
38+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import yaml
2+
import os
3+
4+
def pormetheus_template(input):
5+
json_template = {
6+
"apiVersion": 1,
7+
"datasources": [
8+
{
9+
"name": input.name,
10+
"uid": "prometheus",
11+
"type": "prometheus",
12+
"access": "proxy",
13+
"url": input.url,
14+
"editable": input.editable,
15+
"jsonData": {
16+
"httpMethod": input.httpMethod,
17+
"manageAlerts": input.manageAlerts,
18+
"prometheusType": input.prometheusType,
19+
"prometheusVersion": input.prometheusVersion,
20+
"cacheLevel": input.cacheLevel,
21+
"disableRecordingRules": input.disableRecordingRules,
22+
"incrementalQueryOverlapWindow": input.incrementalQueryOverlapWindow,
23+
"exemplarTraceIdDestinations": [
24+
{
25+
"datasourceUid": input.trace_id.datasourceUid,
26+
"name": input.trace_id.name
27+
}
28+
]
29+
}
30+
}
31+
]
32+
}
33+
34+
35+
dir = "app/media/MyGrafana"
36+
os.makedirs(dir)
37+
os.path.join(dir, 'prometheus.yml')
38+
39+
file=open("app/media/MyGrafana/prometheus.yml","w")
40+
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)
41+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import yaml
2+
import os
3+
4+
def remove_none_values(d):
5+
if isinstance(d, dict):
6+
return {k: remove_none_values(v) for k, v in d.items() if v is not None}
7+
elif isinstance(d, list):
8+
return [remove_none_values(i) for i in d if i is not None]
9+
return d
10+
11+
def tempo_template(input):
12+
dir = 'app/media/MyGrafana'
13+
compose_total = input.dict(exclude_none=True)
14+
15+
16+
os.makedirs(dir)
17+
os.path.join(dir, 'tempo.yaml')
18+
19+
file=open("app/media/MyGrafana/tempo.yaml","w")
20+
yaml.dump(compose_total,file,default_flow_style=False, sort_keys=False)
21+
file.close()
22+
23+

0 commit comments

Comments
 (0)