Skip to content

Commit

Permalink
feat(grafana): compelete grafana data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Dec 17, 2024
1 parent aa387a4 commit fe5fbc6
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 20 deletions.
19 changes: 0 additions & 19 deletions app/media/MyGrafana/mysql.yml

This file was deleted.

16 changes: 16 additions & 0 deletions app/media/MyGrafana/tempo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: 1
datasources:
- name: Tempo
type: tempo
access: proxy
orgId: 1
url: http://tempo-query-frontend.tempo.svc.cluster.local:3100
basicAuth: false
version: 1
editable: true
apiVersion: 1
uid: tempo
jsonData:
httpMethod: GET
serviceMap:
datasourceUid: Mimir-OtelMetrics-Tenant
22 changes: 22 additions & 0 deletions app/models/grafana/postgresql_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError




class PostgresInput(BaseModel):
name:str = "Postgres"
url:str = "localhost:5432"
user:str = "grafana"
editable: bool = True
database:str = "grafana"
sslmode:str = "disable"
password:str = "Password!"
maxOpenConns:int = 100
maxIdleConns:int = 100
maxIdleConnsAuto:bool = True
connMaxLifetime:int = 14400
postgresVersion:int = 903
timescaledb:bool = False


19 changes: 19 additions & 0 deletions app/models/grafana/prometheus_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError


class TraceID(BaseModel):
datasourceUid:str = "my_jaeger_uid"
name:str = "traceID"
class PrometheusInput(BaseModel):
name:str = "Prometheus"
url:str = "http://localhost:9090"
editable: bool = True
httpMethod:str = "POST"
manageAlerts:bool = True
prometheusType:str = "Prometheus"
prometheusVersion:str = "2.44.0"
cacheLevel:str = 'High'
disableRecordingRules:bool = False
incrementalQueryOverlapWindow:str = "10m"
trace_id: TraceID
39 changes: 39 additions & 0 deletions app/models/grafana/tempo_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Optional, List
from pydantic import BaseModel,PrivateAttr,Field

class TracesToLogsV2(BaseModel):
datasourceUid: str = 'loki'
spanStartTimeShift: str = '-2m'
spanEndTimeShift: str = '2m'
filterByTraceID: bool = True
filterBySpanID: bool = True

class ServiceMap(BaseModel):
datasourceUid: str = 'Mimir-OtelMetrics-Tenant'

class NodeGraph(BaseModel):
enabled: bool = True

class JsonData(BaseModel):
httpMethod: str = 'GET'
tracesToLogsV2: Optional[TracesToLogsV2] = TracesToLogsV2()
serviceMap: Optional[ServiceMap] = ServiceMap()
nodeGraph: Optional[NodeGraph] = NodeGraph()

class Datasource(BaseModel):
name: str = 'Tempo'
type: str = Field(default='tempo')
access: str = Field(default="proxy")
orgId: int = Field(default=1)
url: str = 'http://tempo-query-frontend.tempo.svc.cluster.local:3100'
basicAuth: bool = False
version: int = Field(default=1)
editable: bool = True
apiVersion: int = Field(default=1)
uid: str = Field(default="tempo")
jsonData: JsonData = JsonData()

class TempoInput(BaseModel):
apiVersion: int = Field(default=1)
datasources: List[Datasource] = [Datasource()]

40 changes: 39 additions & 1 deletion app/routes/grafana_data_sources.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from app.app_instance import app
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput,MimirInput,MysqlInput)
from app.models import (AlertManagerInput,Output,ElasticSearchInput,LokiInput,MimirInput,MysqlInput,PostgresInput,
PrometheusInput,TempoInput)
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template
from app.template_generators.grafana_data_sources.elasticsearch import elasticsearch_template
from app.template_generators.grafana_data_sources.loki import loki_template
from app.template_generators.grafana_data_sources.mimir import mimir_template
from app.template_generators.grafana_data_sources.mysql import mysql_template
from app.template_generators.grafana_data_sources.postgresql import postgres_template
from app.template_generators.grafana_data_sources.prometheus import pormetheus_template
from app.template_generators.grafana_data_sources.tempo import tempo_template
import shutil
import os

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

mysql_template(request)

return Output(output='output')

@app.post("/api/grafana/postgres")
async def postgres_template_route(request:PostgresInput) -> Output:

dir = 'app/media/MyGrafana'
if os.path.exists(dir):
shutil.rmtree(dir)

postgres_template(request)

return Output(output='output')

@app.post("/api/grafana/prometheus")
async def prometheus_template_route(request:PrometheusInput) -> Output:

dir = 'app/media/MyGrafana'
if os.path.exists(dir):
shutil.rmtree(dir)

pormetheus_template(request)

return Output(output='output')


@app.post("/api/grafana/tempo")
async def tempo_template_route(request:TempoInput) -> Output:

dir = 'app/media/MyGrafana'
if os.path.exists(dir):
shutil.rmtree(dir)

tempo_template(request)

return Output(output='output')
38 changes: 38 additions & 0 deletions app/template_generators/grafana_data_sources/postgresql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import yaml
import os

def postgres_template(input):
json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"type": "postgres",
"url": input.url,
"user": input.user,
"editable": input.editable,
"secureJsonData": {
"password": input.password
},
"jsonData": {
"database": input.database,
"sslmode": input.sslmode,
"maxOpenConns": input.maxOpenConns,
"maxIdleConns": input.maxIdleConns,
"maxIdleConnsAuto": input.maxIdleConnsAuto,
"connMaxLifetime": input.connMaxLifetime,
"postgresVersion": input.postgresVersion,
"timescaledb": input.timescaledb
}
}
]
}


dir = "app/media/MyGrafana"
os.makedirs(dir)
os.path.join(dir, 'postgresql.yml')

file=open("app/media/MyGrafana/postgresql.yml","w")
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)

41 changes: 41 additions & 0 deletions app/template_generators/grafana_data_sources/prometheus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import yaml
import os

def pormetheus_template(input):
json_template = {
"apiVersion": 1,
"datasources": [
{
"name": input.name,
"uid": "prometheus",
"type": "prometheus",
"access": "proxy",
"url": input.url,
"editable": input.editable,
"jsonData": {
"httpMethod": input.httpMethod,
"manageAlerts": input.manageAlerts,
"prometheusType": input.prometheusType,
"prometheusVersion": input.prometheusVersion,
"cacheLevel": input.cacheLevel,
"disableRecordingRules": input.disableRecordingRules,
"incrementalQueryOverlapWindow": input.incrementalQueryOverlapWindow,
"exemplarTraceIdDestinations": [
{
"datasourceUid": input.trace_id.datasourceUid,
"name": input.trace_id.name
}
]
}
}
]
}


dir = "app/media/MyGrafana"
os.makedirs(dir)
os.path.join(dir, 'prometheus.yml')

file=open("app/media/MyGrafana/prometheus.yml","w")
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False)

23 changes: 23 additions & 0 deletions app/template_generators/grafana_data_sources/tempo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import yaml
import os

def remove_none_values(d):
if isinstance(d, dict):
return {k: remove_none_values(v) for k, v in d.items() if v is not None}
elif isinstance(d, list):
return [remove_none_values(i) for i in d if i is not None]
return d

def tempo_template(input):
dir = 'app/media/MyGrafana'
compose_total = input.dict(exclude_none=True)


os.makedirs(dir)
os.path.join(dir, 'tempo.yaml')

file=open("app/media/MyGrafana/tempo.yaml","w")
yaml.dump(compose_total,file,default_flow_style=False, sort_keys=False)
file.close()


0 comments on commit fe5fbc6

Please sign in to comment.