-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grafana): add grafana alertmanager
- Loading branch information
1 parent
fb77323
commit d7d738f
Showing
14 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: 1 | ||
datasources: | ||
- name: string | ||
uid: string | ||
type: alertmanager | ||
url: string | ||
access: proxy | ||
orgId: 1 | ||
jsonData: | ||
implementation: cortex | ||
handleGrafanaManagedAlerts: true | ||
editable: true | ||
basicAuth: true | ||
basicAuthUser: string | ||
secureJsonData: | ||
basicAuthPassword: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Alertmanager | ||
uid: alertmanager | ||
type: alertmanager | ||
url: http://localhost:9093 | ||
access: proxy | ||
orgId: 1 | ||
jsonData: | ||
# Valid options for implementation include mimir, cortex and prometheus | ||
implementation: [prometheus|cortex|mimir] | ||
# Whether or not Grafana should send alert instances to this Alertmanager | ||
handleGrafanaManagedAlerts: [false|true] | ||
|
||
editable: [true|false] | ||
# optionally | ||
basicAuth: true | ||
basicAuthUser: my_user | ||
secureJsonData: | ||
basicAuthPassword: test_password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: elasticsearch-v7-filebeat | ||
type: elasticsearch | ||
access: proxy | ||
url: http://localhost:9200 | ||
editable: [true|false] | ||
jsonData: | ||
index: '[filebeat-]YYYY.MM.DD' | ||
interval: Daily | ||
timeField: '@timestamp' | ||
logMessageField: message | ||
logLevelField: fields.level |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Loki | ||
uid: loki | ||
type: loki | ||
orgId: 1 | ||
access: proxy | ||
editable: [true|false] | ||
url: http://localhost:3100 | ||
jsonData: | ||
timeout: 60 | ||
maxLines: 1000 | ||
|
||
# optionally | ||
basicAuth: true | ||
basicAuthUser: my_user | ||
secureJsonData: | ||
basicAuthPassword: test_password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Mimir | ||
uid: mimir | ||
type: prometheus | ||
access: proxy | ||
orgId: 1 | ||
url: http://mimir-nginx.mimir.svc.cluster.local/prometheus | ||
editable: [true|false] | ||
version: 1 | ||
jsonData: | ||
httpHeaderName1: "X-Scope-OrgID" | ||
alertmanagerUid: "alertmanager" | ||
secureJsonData: | ||
httpHeaderValue1: "pods" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: MySQL | ||
type: mysql | ||
url: localhost:3306 | ||
user: grafana | ||
editable: [true|false] | ||
jsonData: | ||
tlsAuth: true | ||
tlsSkipVerify: true | ||
database: grafana | ||
maxOpenConns: 100 # Grafana v5.4+ | ||
maxIdleConns: 100 # Grafana v5.4+ | ||
maxIdleConnsAuto: true # Grafana v9.5.1+ | ||
connMaxLifetime: 14400 # Grafana v5.4+ | ||
secureJsonData: | ||
password: ${GRAFANA_MYSQL_PASSWORD} | ||
tlsClientCert: ${GRAFANA_TLS_CLIENT_CERT} | ||
tlsCACert: ${GRAFANA_TLS_CA_CERT} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Postgres | ||
type: postgres | ||
url: localhost:5432 | ||
user: grafana # Database user’s login/username | ||
editable: [true|false] | ||
secureJsonData: | ||
password: 'Password!' | ||
jsonData: | ||
database: grafana | ||
sslmode: 'disable' # disable/require/verify-ca/verify-full | ||
maxOpenConns: 100 # Grafana v5.4+ | ||
maxIdleConns: 100 # Grafana v5.4+ | ||
maxIdleConnsAuto: true # Grafana v9.5.1+ | ||
connMaxLifetime: 14400 # Grafana v5.4+ | ||
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10 | ||
timescaledb: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: Prometheus | ||
uid: prometheus | ||
type: prometheus | ||
access: proxy | ||
# Access mode - proxy (server in the UI) or direct (browser in the UI). | ||
url: http://localhost:9090 | ||
editable: [true|false] | ||
jsonData: | ||
httpMethod: POST | ||
manageAlerts: true | ||
prometheusType: Prometheus | ||
prometheusVersion: 2.44.0 | ||
cacheLevel: 'High' | ||
disableRecordingRules: false | ||
incrementalQueryOverlapWindow: 10m | ||
exemplarTraceIdDestinations: | ||
# Field with internal link pointing to data source in Grafana. | ||
# datasourceUid value can be anything, but it should be unique across all defined data source uids. | ||
- datasourceUid: my_jaeger_uid | ||
name: traceID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 | ||
tracesToLogsV2: # If you are going to link your tracing data with logs, configure <<tracesToLogsV2>> | ||
datasourceUid: 'loki' | ||
spanStartTimeShift: '-2m' | ||
spanEndTimeShift: '2m' | ||
filterByTraceID: true | ||
filterBySpanID: true | ||
serviceMap: # If you are going to add serviceGraph feature to tempo, configure <<serviceMap.datasourceUid>> | ||
datasourceUid: 'Mimir-OtelMetrics-Tenant' | ||
nodeGraph: # If you are going to add nodeGraph feature to tempo, enable <<nodeGraph>> | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import List, Optional | ||
from pydantic import BaseModel, validator, ValidationError | ||
|
||
class BasicAuth(BaseModel): | ||
basicAuthUser:str | ||
basicAuthPassword:str | ||
|
||
|
||
class AlertManagerInput(BaseModel): | ||
name:str | ||
url:str | ||
uid:str | ||
implementation:str | ||
|
||
handleGrafanaManagedAlerts:bool = True | ||
editable: bool = True | ||
basic_auth:Optional[BasicAuth] | ||
|
||
@validator("implementation") | ||
def validator_implementation(cls,value): | ||
valid = ['prometheus','cortex','mimir'] | ||
if value not in valid: | ||
raise ValueError(f"implementation must be in {valid}") | ||
return value | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from app.app_instance import app | ||
from app.models import (AlertManagerInput,Output) | ||
from app.template_generators.grafana_data_sources.alertmanager import alert_manager_template | ||
import shutil | ||
import os | ||
|
||
@app.post("/api/grafana/alertmanager") | ||
async def alertmanager_template(request:AlertManagerInput) -> Output: | ||
|
||
dir = 'app/media/MyGrafana' | ||
if os.path.exists(dir): | ||
shutil.rmtree(dir) | ||
|
||
alert_manager_template(request) | ||
|
||
return Output(output='output') | ||
|
61 changes: 61 additions & 0 deletions
61
app/template_generators/grafana_data_sources/alertmanager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import yaml | ||
import os | ||
|
||
def alert_manager_template(input): | ||
if input.basic_auth is None: | ||
json_template = { | ||
"apiVersion": 1, | ||
"datasources": [ | ||
{ | ||
"name": input.name, | ||
"uid": input.uid, | ||
"type": "alertmanager", | ||
"url": input.url, | ||
"access": "proxy", | ||
"orgId": 1, | ||
"jsonData": { | ||
"implementation": input.implementation, | ||
"handleGrafanaManagedAlerts": input.handleGrafanaManagedAlerts | ||
}, | ||
"editable": input.editable, | ||
|
||
} | ||
] | ||
} | ||
dir = "app/media/MyGrafana" | ||
os.makedirs(dir) | ||
os.path.join(dir, 'alertmanager.yml') | ||
|
||
file=open("app/media/MyGrafana/alertmanager.yml","w") | ||
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False) | ||
|
||
else: | ||
json_template = { | ||
"apiVersion": 1, | ||
"datasources": [ | ||
{ | ||
"name": input.name, | ||
"uid": input.uid, | ||
"type": "alertmanager", | ||
"url": input.url, | ||
"access": "proxy", | ||
"orgId": 1, | ||
"jsonData": { | ||
"implementation": input.implementation, | ||
"handleGrafanaManagedAlerts": input.handleGrafanaManagedAlerts | ||
}, | ||
"editable": input.editable, | ||
"basicAuth": True, | ||
"basicAuthUser": input.basic_auth.basicAuthUser, | ||
"secureJsonData": { | ||
"basicAuthPassword": input.basic_auth.basicAuthPassword | ||
} | ||
} | ||
] | ||
} | ||
dir = "app/media/MyGrafana" | ||
os.makedirs(dir) | ||
os.path.join(dir, 'alertmanager.yml') | ||
|
||
file=open("app/media/MyGrafana/alertmanager.yml","w") | ||
yaml.dump(json_template,file,default_flow_style=False,sort_keys=False) |