Skip to content

Commit

Permalink
feat: Automatic Release detection (#335)
Browse files Browse the repository at this point in the history
Attempts to guess the release info, either by getting the last git
commit hash or by looking up some known environment variables that tracks
release identification.
  • Loading branch information
AramisHM committed Jun 1, 2021
1 parent f7d425a commit 5d9e534
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func NewClient(options ClientOptions) (*Client, error) {
}

if options.Release == "" {
options.Release = os.Getenv("SENTRY_RELEASE")
options.Release = GetDefaultRelease()
}

if options.Environment == "" {
Expand Down
1 change: 1 addition & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func TestCaptureEvent(t *testing.T) {
t.Fatal("missing event")
}
want := &Event{
Release: GetDefaultRelease(),
EventID: eventID,
Timestamp: timestamp,
ServerName: serverName,
Expand Down
5 changes: 5 additions & 0 deletions fasthttp/sentryfasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestIntegration(t *testing.T) {
},

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelFatal,
Message: "test",
Request: &sentry.Request{
Expand All @@ -56,6 +57,7 @@ func TestIntegration(t *testing.T) {
},

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "post: payload",
Request: &sentry.Request{
Expand All @@ -77,6 +79,7 @@ func TestIntegration(t *testing.T) {
},

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "get",
Request: &sentry.Request{
Expand All @@ -99,6 +102,7 @@ func TestIntegration(t *testing.T) {
},

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "post: 15 KB",
Request: &sentry.Request{
Expand All @@ -123,6 +127,7 @@ func TestIntegration(t *testing.T) {
},

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "body ignored",
Request: &sentry.Request{
Expand Down
5 changes: 5 additions & 0 deletions http/sentryhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestIntegration(t *testing.T) {
}),

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelFatal,
Message: "test",
Request: &sentry.Request{
Expand Down Expand Up @@ -60,6 +61,7 @@ func TestIntegration(t *testing.T) {
}),

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "post: payload",
Request: &sentry.Request{
Expand All @@ -83,6 +85,7 @@ func TestIntegration(t *testing.T) {
}),

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "get",
Request: &sentry.Request{
Expand Down Expand Up @@ -110,6 +113,7 @@ func TestIntegration(t *testing.T) {
}),

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "post: 15 KB",
Request: &sentry.Request{
Expand All @@ -136,6 +140,7 @@ func TestIntegration(t *testing.T) {
}),

WantEvent: &sentry.Event{
Release: sentry.GetDefaultRelease(),
Level: sentry.LevelInfo,
Message: "body ignored",
Request: &sentry.Request{
Expand Down
2 changes: 2 additions & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func TestStartSpan(t *testing.T) {
t.Fatalf("sent %d events, want 1", got)
}
want := &Event{
Release: GetDefaultRelease(),
Type: transactionType,
Transaction: transaction,
Contexts: map[string]interface{}{
Expand Down Expand Up @@ -188,6 +189,7 @@ func TestStartChild(t *testing.T) {
t.Fatalf("sent %d events, want 1", got)
}
want := &Event{
Release: GetDefaultRelease(),
Type: transactionType,
Transaction: "Test Transaction",
Contexts: map[string]interface{}{
Expand Down
44 changes: 44 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package sentry

import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
)

Expand Down Expand Up @@ -37,3 +40,44 @@ func prettyPrint(data interface{}) {
dbg, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(dbg))
}

// GetDefaultRelease attempts to guess a default release.
func GetDefaultRelease() string {
if release := os.Getenv("SENTRY_RELEASE"); release == "" {
// Attempt to get the last commit hash with git.
var stdout bytes.Buffer
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
Logger.Println("Failed attempt to run git rev-parse.")
} else {
shastr := strings.TrimSpace(stdout.String())
if len(shastr) == 40 { // sha1 hash length
return shastr
}
}

// Look for environment variables known to hold versioning info.
if release == "" {
envVars := []string{
"HEROKU_SLUG_COMMIT",
"SOURCE_VERSION",
"CODEBUILD_RESOLVED_SOURCE_VERSION",
"CIRCLE_SHA1",
"GAE_DEPLOYMENT_ID",
"GITHUB_SHA", // GitHub Actions - https://help.github.com/en/actions
"COMMIT_REF", // Netlify - https://docs.netlify.com/
"VERCEL_GIT_COMMIT_SHA", // Vercel - https://vercel.com/
"ZEIT_GITHUB_COMMIT_SHA", // Zeit (now known as Vercel)
"ZEIT_GITLAB_COMMIT_SHA",
"ZEIT_BITBUCKET_COMMIT_SHA"}
for _, env := range envVars {
if envVal := os.Getenv(env); envVal != "" {
return envVal // Stop at first non-empty variable.
}
}
}
}
return ""
}

0 comments on commit 5d9e534

Please sign in to comment.