Skip to content

Commit bfa2248

Browse files
author
Micah Huber
authored
Add mutex to datadog code that is not thread safe (#71)
1 parent 995f580 commit bfa2248

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
local.sh
1616

1717
.vscode
18-
.vscode/*
18+
.vscode/*
19+
20+
.envrc

pkg/datadog/datadog.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/fairwindsops/astro/pkg/metrics"
2323
log "github.com/sirupsen/logrus"
2424
"github.com/zorkian/go-datadog-api"
25+
"sync"
2526
)
2627

2728
// ClientAPI defines the interface for the Datadog client, for testing purposes
@@ -35,6 +36,7 @@ type ClientAPI interface {
3536
// DDMonitorManager is a higher-level wrapper around the Datadog API
3637
type DDMonitorManager struct {
3738
Datadog ClientAPI
39+
mux sync.Mutex
3840
}
3941

4042
var ddMonitorManagerInstance *DDMonitorManager
@@ -54,11 +56,15 @@ func GetInstance() *DDMonitorManager {
5456
// It returns the Id of the monitor created or updated.
5557
func (ddman *DDMonitorManager) AddOrUpdate(monitor *datadog.Monitor) (*datadog.Monitor, error) {
5658
log.Infof("Update templated monitor:%v", *monitor.Name)
59+
ddman.mux.Lock()
60+
defer ddman.mux.Unlock()
61+
5762
// check if monitor exists
5863
ddMonitor, err := ddman.GetProvisionedMonitor(monitor)
5964
if err != nil {
6065
//monitor doesn't exist
6166
provisioned, err := ddman.Datadog.CreateMonitor(monitor)
67+
6268
if err != nil {
6369
metrics.DatadogErrCounter.Inc()
6470
log.Errorf("Error creating monitor %s: %s", *monitor.Name, err)

0 commit comments

Comments
 (0)