Skip to content

Commit

Permalink
Add mutex to datadog code that is not thread safe (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah Huber authored Dec 16, 2019
1 parent 995f580 commit bfa2248
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
local.sh

.vscode
.vscode/*
.vscode/*

.envrc
6 changes: 6 additions & 0 deletions pkg/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/fairwindsops/astro/pkg/metrics"
log "github.com/sirupsen/logrus"
"github.com/zorkian/go-datadog-api"
"sync"
)

// ClientAPI defines the interface for the Datadog client, for testing purposes
Expand All @@ -35,6 +36,7 @@ type ClientAPI interface {
// DDMonitorManager is a higher-level wrapper around the Datadog API
type DDMonitorManager struct {
Datadog ClientAPI
mux sync.Mutex
}

var ddMonitorManagerInstance *DDMonitorManager
Expand All @@ -54,11 +56,15 @@ func GetInstance() *DDMonitorManager {
// It returns the Id of the monitor created or updated.
func (ddman *DDMonitorManager) AddOrUpdate(monitor *datadog.Monitor) (*datadog.Monitor, error) {
log.Infof("Update templated monitor:%v", *monitor.Name)
ddman.mux.Lock()
defer ddman.mux.Unlock()

// check if monitor exists
ddMonitor, err := ddman.GetProvisionedMonitor(monitor)
if err != nil {
//monitor doesn't exist
provisioned, err := ddman.Datadog.CreateMonitor(monitor)

if err != nil {
metrics.DatadogErrCounter.Inc()
log.Errorf("Error creating monitor %s: %s", *monitor.Name, err)
Expand Down

0 comments on commit bfa2248

Please sign in to comment.