Skip to content

Commit

Permalink
add grafana provider
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Sequeira <filipe@weave.works>
  • Loading branch information
fsequeira1 committed Feb 3, 2022
1 parent 33a0df0 commit 785750a
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/v1beta1/provider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
// ProviderSpec defines the desired state of Provider
type ProviderSpec struct {
// Type of provider
// +kubebuilder:validation:Enum=slack;discord;msteams;rocket;generic;github;gitlab;bitbucket;azuredevops;googlechat;webex;sentry;azureeventhub;telegram;lark;matrix;opsgenie;alertmanager;
// +kubebuilder:validation:Enum=slack;discord;msteams;rocket;generic;github;gitlab;bitbucket;azuredevops;googlechat;webex;sentry;azureeventhub;telegram;lark;matrix;opsgenie;alertmanager;grafana;
// +required
Type string `json:"type"`

Expand Down Expand Up @@ -71,6 +71,7 @@ type ProviderSpec struct {
const (
GenericProvider string = "generic"
SlackProvider string = "slack"
GrafanaProvider string = "grafana"
DiscordProvider string = "discord"
MSTeamsProvider string = "msteams"
RocketProvider string = "rocket"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ spec:
- matrix
- opsgenie
- alertmanager
- grafana
type: string
username:
description: Bot username for this provider
Expand Down
2 changes: 2 additions & 0 deletions internal/notifier/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (f Factory) Notifier(provider string) (Interface, error) {
n, err = NewOpsgenie(f.URL, f.ProxyURL, f.CertPool, f.Token)
case v1beta1.AlertManagerProvider:
n, err = NewAlertmanager(f.URL, f.ProxyURL, f.CertPool)
case v1beta1.GrafanaProvider:
n, err = NewGrafana(f.URL, f.ProxyURL, f.Token)
default:
err = fmt.Errorf("provider %s not supported", provider)
}
Expand Down
88 changes: 88 additions & 0 deletions internal/notifier/grafana.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2020 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package notifier

import (
"fmt"
"net/url"
"strings"

"github.com/fluxcd/pkg/runtime/events"
"github.com/hashicorp/go-retryablehttp"

)

// Discord holds the hook URL
type Grafana struct {
URL string
Token string
ProxyURL string
}

// GraphiteAnnotation represents a Grafana API annotation in Graphite format
type GraphitePayload struct {
//What string `json:"what"` //optional
When int64 `json:"when"` //optional unix timestamp (ms)
Text string `json:"text"`
Tags []string `json:"tags,omitempty"`
}

// NewGrafana validates the Grafana URL and returns a Grafana object
func NewGrafana(URL string, proxyURL string, token string) (*Grafana, error) {
_, err := url.ParseRequestURI(URL)
if err != nil {
return nil, fmt.Errorf("invalid Grafana URL %s", URL)
}

return &Grafana{
URL: URL,
ProxyURL: proxyURL,
Token: token,
}, nil
}

// Post annotation
func (s *Grafana) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
}

sfields := make([]string, 0, len(event.Metadata))
sfields = append(sfields, "flux")
sfields = append(sfields, event.ReportingController)
for k, v := range event.Metadata {
sfields = append(sfields, fmt.Sprintf("%s: %s", k, v))
}
// add tag to filter on grafana

payload := GraphitePayload{
When: event.Timestamp.Unix(),
Text: fmt.Sprintf("%s/%s.%s", strings.ToLower(event.InvolvedObject.Kind), event.InvolvedObject.Name, event.InvolvedObject.Namespace),
Tags: sfields,
}

err := postMessage(s.URL, s.ProxyURL, nil, payload, func(request *retryablehttp.Request) {
if s.Token != "" {
request.Header.Add("Authorization", "Bearer "+s.Token)
}
})
if err != nil {
return fmt.Errorf("postMessage failed: %w", err)
}
return nil
}
47 changes: 47 additions & 0 deletions internal/notifier/grafana_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2020 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package notifier

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestGrafana_Post(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
var payload = GraphitePayload{}
err = json.Unmarshal(b, &payload)
require.NoError(t, err)

require.Equal(t, "gitrepository/webapp.gitops-system", payload.Text)
require.Equal(t, "test: metadata", payload.Tags[0])
}))
defer ts.Close()

grafana, err := NewGrafana(ts.URL, "", "")
require.NoError(t, err)

err = grafana.Post(testEvent())
require.NoError(t, err)
}

0 comments on commit 785750a

Please sign in to comment.