Skip to content

Commit

Permalink
add minor changes to sentry dsn data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 13, 2023
1 parent f90e584 commit 301df17
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crons.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy
hub.WithScope(func(scope *sentry.Scope) {

// If DSN annotation provided, we bind a new client with that DSN
client, ok := dsnData.GetClientFromObject(ctx, &job.ObjectMeta, hub.Client().Options())
client, ok := dsnClientMapping.GetClientFromObject(ctx, &job.ObjectMeta, hub.Client().Options())
if ok {
hub.BindClient(client)
}
Expand Down
35 changes: 16 additions & 19 deletions sentry_dsn_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,32 @@ import (

var DSNAnnotation = "k8s.sentry.io/dsn"

// map from Sentry DSN to Client
var dsnClientMapping = NewDsnClientMapping()

// Map from Sentry DSN to Client
type DsnClientMapping struct {
mutex sync.RWMutex
clientMap map[string]*sentry.Client
customDsnFlag bool
}

func NewDsnData() *DsnClientMapping {
func NewDsnClientMapping() *DsnClientMapping {
return &DsnClientMapping{
mutex: sync.RWMutex{},
clientMap: make(map[string]*sentry.Client),
customDsnFlag: isTruthy(os.Getenv("SENTRY_K8S_CUSTOM_DSNS")),
}
}

// return client if added successfully
// Return client if added successfully
// (also returns client if already exists)
func (d *DsnClientMapping) AddClientToMap(options sentry.ClientOptions) (*sentry.Client, error) {
d.mutex.Lock()
defer d.mutex.Unlock()

// check if we already encountered this dsn
existingClient, ok := d.clientMap[options.Dsn]
if ok {
return existingClient, nil
}

// create a new client for the dsn
// Create a new client for the dsn
// even if client already exists, it
// will be re-initialized with a new client
newClient, err := sentry.NewClient(
sentry.ClientOptions{
Dsn: options.Dsn,
Expand All @@ -51,16 +49,15 @@ func (d *DsnClientMapping) AddClientToMap(options sentry.ClientOptions) (*sentry
return nil, err
}
d.clientMap[options.Dsn] = newClient

return newClient, nil
}

// retrieve a client with given dsn
// Retrieve a client with given dsn
func (d *DsnClientMapping) GetClientFromMap(dsn string) (*sentry.Client, bool) {
d.mutex.RLock()
defer d.mutex.RUnlock()

// check if we have this dsn
// Check if we have this dsn
existingClient, ok := d.clientMap[dsn]
return existingClient, ok
}
Expand All @@ -75,20 +72,20 @@ func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, objectMeta *
return nil, false
}

// find DSN annotation from the object
// Find DSN annotation from the object
altDsn, err := searchDsn(ctx, objectMeta)
if err != nil {
return nil, false
}

// if we did find an alternative DSN
// If we did find an alternative DSN
if altDsn != "" {
// attempt to retrieve the corresponding client
client, _ := dsnData.GetClientFromMap(altDsn)
// Attempt to retrieve the corresponding client
client, _ := dsnClientMapping.GetClientFromMap(altDsn)
if client == nil {
// create new client
clientOptions.Dsn = altDsn
client, err = dsnData.AddClientToMap(clientOptions)
client, err = dsnClientMapping.AddClientToMap(clientOptions)
if err != nil {
return nil, false
}
Expand All @@ -99,7 +96,7 @@ func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, objectMeta *
}
}

// recursive function to find if there is a DSN annotation
// Recursive function to find if there is a DSN annotation
func searchDsn(ctx context.Context, object *metav1.ObjectMeta) (string, error) {

dsn, ok := object.Annotations[DSNAnnotation]
Expand Down
2 changes: 1 addition & 1 deletion watcher_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func handleWatchEvent(ctx context.Context, event *watch.Event, cutoffTime metav1
objectMeta, ok := findObjectMeta(ctx, eventObject.InvolvedObject.Kind, eventObject.InvolvedObject.Namespace, eventObject.InvolvedObject.Name)
if ok {
// if DSN annotation provided, we bind a new client with that DSN
client, ok := dsnData.GetClientFromObject(ctx, objectMeta, hub.Client().Options())
client, ok := dsnClientMapping.GetClientFromObject(ctx, objectMeta, hub.Client().Options())
if ok {
hub.BindClient(client)
}
Expand Down
3 changes: 1 addition & 2 deletions watcher_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const podsWatcherName = "pods"

var cronsMetaData = NewCronsMetaData()
var dsnData = NewDsnData()

func handlePodTerminationEvent(ctx context.Context, containerStatus *v1.ContainerStatus, pod *v1.Pod, scope *sentry.Scope) *sentry.Event {
logger := zerolog.Ctx(ctx)
Expand Down Expand Up @@ -118,7 +117,7 @@ func handlePodWatchEvent(ctx context.Context, event *watch.Event) {
hub.WithScope(func(scope *sentry.Scope) {

// If DSN annotation provided, we bind a new client with that DSN
client, ok := dsnData.GetClientFromObject(ctx, &podObject.ObjectMeta, hub.Client().Options())
client, ok := dsnClientMapping.GetClientFromObject(ctx, &podObject.ObjectMeta, hub.Client().Options())
if ok {
hub.BindClient(client)
}
Expand Down

0 comments on commit 301df17

Please sign in to comment.