-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from LWJ/sentry_provider
Add support for Sentry provider
- Loading branch information
Showing
8 changed files
with
256 additions
and
4 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
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
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,74 @@ | ||
/* | ||
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" | ||
"github.com/fluxcd/pkg/runtime/events" | ||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
// Sentry holds the client instance | ||
type Sentry struct { | ||
Client *sentry.Client | ||
} | ||
|
||
// NewSentry creates a Sentry client from the provided Data Source Name (DSN) | ||
func NewSentry(dsn string) (*Sentry, error) { | ||
client, err := sentry.NewClient(sentry.ClientOptions{ | ||
Dsn: dsn, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Sentry{ | ||
Client: client, | ||
}, nil | ||
} | ||
|
||
// Post event to Sentry | ||
func (s *Sentry) Post(event events.Event) error { | ||
// Skip any update events | ||
if isCommitStatus(event.Metadata, "update") { | ||
return nil | ||
} | ||
|
||
// Send event to Sentry | ||
s.Client.CaptureEvent(toSentryEvent(event), nil, nil) | ||
return nil | ||
} | ||
|
||
// Maps a controller-issued event to a Sentry event | ||
func toSentryEvent(event events.Event) *sentry.Event { | ||
// Prepare Metadata | ||
extra := make(map[string]interface{}, len(event.Metadata)) | ||
for k, v := range event.Metadata { | ||
extra[k] = v | ||
} | ||
|
||
// Construct event | ||
obj := event.InvolvedObject | ||
return &sentry.Event{ | ||
Timestamp: event.Timestamp.Time, | ||
Level: sentry.Level(event.Severity), | ||
ServerName: event.ReportingController, | ||
Transaction: fmt.Sprintf("%s: %s/%s", obj.Kind, obj.Namespace, obj.Name), | ||
Extra: extra, | ||
Message: event.Message, | ||
} | ||
} |
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,68 @@ | ||
/* | ||
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 ( | ||
"github.com/fluxcd/pkg/runtime/events" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewSentry(t *testing.T) { | ||
s, err := NewSentry("https://test@localhost/1") | ||
require.NoError(t, err) | ||
assert.Equal(t, s.Client.Options().Dsn, "https://test@localhost/1") | ||
} | ||
|
||
func TestToSentryEvent(t *testing.T) { | ||
// Construct test event | ||
e := events.Event{ | ||
InvolvedObject: corev1.ObjectReference{ | ||
Kind: "GitRepository", | ||
Namespace: "flux-system", | ||
Name: "test-app", | ||
}, | ||
Severity: "info", | ||
Timestamp: metav1.Date(2020, 01, 01, 0, 0, 0, 0, time.UTC), | ||
Message: "message", | ||
Metadata: map[string]string{ | ||
"key1": "val1", | ||
"key2": "val2", | ||
}, | ||
ReportingController: "source-controller", | ||
} | ||
|
||
// Map to Sentry event | ||
s := toSentryEvent(e) | ||
|
||
// Assertions | ||
assert.Equal(t, time.Date(2020, 01, 01, 0, 0, 0, 0, time.UTC), s.Timestamp) | ||
assert.Equal(t, sentry.LevelInfo, s.Level) | ||
assert.Equal(t, "source-controller", s.ServerName) | ||
assert.Equal(t, "GitRepository: flux-system/test-app", s.Transaction) | ||
assert.Equal(t, map[string]interface{}{ | ||
"key1": "val1", | ||
"key2": "val2", | ||
}, s.Extra) | ||
assert.Equal(t, "message", s.Message) | ||
} |