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

Added an example connector #139

Merged
merged 2 commits into from
Oct 16, 2023
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
13 changes: 10 additions & 3 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ Draft = "true"
description = "blocked because not needed"
what = "fooo service"

[[github]]
Repos = ['synyx/tuwat', 'synyx/buchungsstreber']
Tag = 'gh'
# This adds example alerts in every flavor, useful for development
# Should be disabled in production use
[[example]]
Tag = 'demo'

# The github connector runs out af the box as configured below,
# but keep in mind that github rate limits these requests.
#[[github]]
#Repos = ['synyx/tuwat', 'synyx/buchungsstreber']
#Tag = 'gh'

#[[icinga2]]
#Tag = "synyx"
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/synyx/tuwat/pkg/connectors"
"github.com/synyx/tuwat/pkg/connectors/alertmanager"
"github.com/synyx/tuwat/pkg/connectors/example"
"github.com/synyx/tuwat/pkg/connectors/github"
"github.com/synyx/tuwat/pkg/connectors/gitlabmr"
"github.com/synyx/tuwat/pkg/connectors/icinga2"
Expand Down Expand Up @@ -86,6 +87,7 @@ type rootConfig struct {
GitHubIssues []github.Config `toml:"github"`
Redmines []redmine.Config `toml:"redmine"`
Orderview []orderview.Config `toml:"orderview"`
Example []example.Config `toml:"example"`
}

func NewConfiguration() (*Config, error) {
Expand Down Expand Up @@ -204,6 +206,9 @@ func (cfg *Config) loadMainConfig(file string) error {
for _, connectorConfig := range rootConfig.Orderview {
cfg.Connectors = append(cfg.Connectors, orderview.NewConnector(&connectorConfig))
}
for _, connectorConfig := range rootConfig.Example {
cfg.Connectors = append(cfg.Connectors, example.NewConnector(&connectorConfig))
}

// Add template for
cfg.WhereTemplate, err = template.New("where").
Expand Down
114 changes: 114 additions & 0 deletions pkg/connectors/example/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package example

import (
"context"
"fmt"
"github.com/synyx/tuwat/pkg/connectors"
html "html/template"
"time"
)

type Connector struct {
config Config
}

type Config struct {
Tag string
}

func NewConnector(cfg *Config) *Connector {
return &Connector{*cfg}
}

func (c *Connector) Tag() string {
return c.config.Tag
}

// Collect returns a few example warnings, one of each type
// This eliminates the need to select a source that contains a given error type during development
func (c *Connector) Collect(_ context.Context) ([]connectors.Alert, error) {

var alerts []connectors.Alert

alerts = append(alerts, exampleOkAlert())
alerts = append(alerts, exampleWarningAlert())
alerts = append(alerts, exampleCriticalAlert())
alerts = append(alerts, exampleUnknownAlert())

return alerts, nil
}

func exampleOkAlert() connectors.Alert {
alert := connectors.Alert{
Labels: map[string]string{
"Project": "Example Alerts",
"Culprit": "Geras",
"EscalateTo": "BuJo",
},
Start: time.Now().Add(-time.Hour * 2),
State: connectors.OK,
Description: "OK",
Details: "Nothing to worry about, just informing you that nothing bad happened",
Links: []html.HTML{
html.HTML("<a href=\"https://go.dev/\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
return alert
}
func exampleWarningAlert() connectors.Alert {
alert := connectors.Alert{
Labels: map[string]string{
"Project": "Example Alerts",
"Culprit": "Geras",
"EscalateTo": "BuJo",
},
Start: time.Now().Add(-time.Hour * 2),
State: connectors.Warning,
Description: "Warning",
Details: "This is a warning! Coffee is low!",
Links: []html.HTML{
html.HTML("<a href=\"https://go.dev/\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
return alert
}

func exampleCriticalAlert() connectors.Alert {
alert := connectors.Alert{
Labels: map[string]string{
"Project": "Example Alerts",
"Culprit": "Geras",
"EscalateTo": "BuJo",
},
Start: time.Now().Add(-time.Hour * 2),
State: connectors.Critical,
Description: "Critical",
Details: "This is a critical alert! We ran out of coffee!",
Links: []html.HTML{
html.HTML("<a href=\"https://go.dev/\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
return alert
}

func exampleUnknownAlert() connectors.Alert {
alert := connectors.Alert{
Labels: map[string]string{
"Project": "Example Alerts",
"Culprit": "Geras",
"EscalateTo": "BuJo",
},
Start: time.Now().Add(-time.Hour * 2),
State: connectors.Unknown,
Description: "Unknown",
Details: "We have no idea what happened here",
Links: []html.HTML{
html.HTML("<a href=\"https://go.dev/\" target=\"_blank\" alt=\"Home\">🏠</a>"),
},
}
return alert
}

func (c *Connector) String() string {
return fmt.Sprintf("Example Connector")
}