Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold project and generate alertmanager dashboard #1

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,33 @@ go.work.sum

# env file
.env
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env


vendor/
vendor_jsonnet/
25 changes: 25 additions & 0 deletions dashboards/alertmanager.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local alertmanager = (import 'github.com/prometheus/alertmanager/doc/alertmanager-mixin/dashboards/overview.libsonnet');
local defaultConfig = (import 'github.com/prometheus/alertmanager/doc/alertmanager-mixin/config.libsonnet');
local mixin = alertmanager {
_config+:: defaultConfig._config {
alertmanagerClusterLabels: 'namespace,job',
alertmanagerNameLabels: 'pod',
alertmanagerCriticalIntegrationsRegEx: 'slack|pagerduty|email|webhook',
alertmanagerSelector: 'job="alertmanager"',
},
};

{
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: {
name: 'grafana-dashboard-observatorium-alertmanager-overview',
labels+: { grafana_dashboard: 'true' },
annotations+: {
'grafana-folder': '/grafana-dashboard-definitions/Observatorium',
},
},
data: {
'alertmanager-overview.json': std.manifestJsonEx(mixin.grafanaDashboards['alertmanager-overview.json'], ' '),
},
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/rhobs/monitoring

go 1.23.3

require (
github.com/ghodss/yaml v1.0.0
github.com/google/go-jsonnet v0.20.0
github.com/magefile/mage v1.15.0
)

require (
gopkg.in/yaml.v2 v2.2.7 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/google/go-jsonnet v0.20.0 h1:WG4TTSARuV7bSm4PMB4ohjxe33IHT5WVTrJSU33uT4g=
github.com/google/go-jsonnet v0.20.0/go.mod h1:VbgWF9JX7ztlv770x/TolZNGGFfiHEVx9G6ca2eUmeA=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
15 changes: 15 additions & 0 deletions jsonnetfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/prometheus/alertmanager.git",
"subdir": "doc/alertmanager-mixin"
}
},
"version": "main"
}
],
"legacyImports": true
}
56 changes: 56 additions & 0 deletions jsonnetfile.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/grafana/grafonnet.git",
"subdir": "gen/grafonnet-latest"
}
},
"version": "d20e609202733790caf5b554c9945d049f243ae3",
"sum": "V9vAj21qJOc2DlMPDgB1eEjSQU4A+sAA4AXuJ6bd4xc="
},
{
"source": {
"git": {
"remote": "https://github.com/grafana/grafonnet.git",
"subdir": "gen/grafonnet-v11.4.0"
}
},
"version": "d20e609202733790caf5b554c9945d049f243ae3",
"sum": "aVAX09paQYNOoCSKVpuk1exVIyBoMt/C50QJI+Q/3nA="
},
{
"source": {
"git": {
"remote": "https://github.com/jsonnet-libs/docsonnet.git",
"subdir": "doc-util"
}
},
"version": "6ac6c69685b8c29c54515448eaca583da2d88150",
"sum": "BrAL/k23jq+xy9oA7TWIhUx07dsA/QLm3g7ktCwe//U="
},
{
"source": {
"git": {
"remote": "https://github.com/jsonnet-libs/xtd.git",
"subdir": ""
}
},
"version": "1199b50e9d2ff53d4bb5fb2304ad1fb69d38e609",
"sum": "LfbgcJbilu4uBdKYZSvmkoOTPwEAzg10L3/VqKAIWtA="
},
{
"source": {
"git": {
"remote": "https://github.com/prometheus/alertmanager.git",
"subdir": "doc/alertmanager-mixin"
}
},
"version": "d0eaa9fcd116fd2ef87390cd0f6966b2dfaee6f5",
"sum": "Mf4h1BYLle2nrgjf/HXrBbl0Zk8N+xaoEM017o0BC+k="
}
],
"legacyImports": false
}
71 changes: 71 additions & 0 deletions magefiles/dashboards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"os"

"github.com/ghodss/yaml"
"github.com/google/go-jsonnet"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

const (
vendorPath = "./vendor_jsonnet"

dashboardSource = "./dashboards/"
dashboardPath = "./resources/dashboards/"
)

type (
Dashboards mg.Namespace
)

// Alertmanager Generates the Alertmanager dashboard
func (d Dashboards) Alertmanager() error {
err := sh.Run("jb", "update", "github.com/prometheus/alertmanager/doc/alertmanager-mixin@main", `--jsonnetpkg-home=vendor_jsonnet`)

const (
filename = "alertmanager.jsonnet"
fileOut = "alertmanager.yaml"
)

vm := getVM()
j, err := vm.EvaluateFile(getDashboardFile(filename))
if err != nil {
panic(err)
}

err = writeDashboardFile(fileOut, j)
if err != nil {
return fmt.Errorf("failed to write dashboard file: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just panic everywhere. I don't really think returning errors anywhere would help if something were broken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from my understand, from reading the mage docs this s the preferred way because if we start to chain things together then, in targets like mage dashboards we can still build the majority of the work without having to bomb out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah we can follow mage best practices. Although, in our case, would it really matter. Majority of work is the final jsonnet gen step I guess. I'm fine either way

}
return nil
}

func getVM() *jsonnet.VM {
vm := jsonnet.MakeVM()
vm.Importer(&jsonnet.FileImporter{
JPaths: []string{
vendorPath,
},
})
return vm
}

func getDashboardFile(filename string) string {
return fmt.Sprint(dashboardSource, filename)
}

func writeDashboardFile(filename string, jsonContent string) error {
b, err := yaml.JSONToYAML([]byte(jsonContent))
if err != nil {
return fmt.Errorf("failed to convert json to yaml: %w", err)
}

err = os.WriteFile(fmt.Sprint(dashboardPath, filename), b, 0644)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do a os.mkdir, just in case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually fails in terms of idempotency when i added it

if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
Loading