notifier: Replace usage of sync/atomic with uber-go/atomic

Signed-off-by: Javier Palomo <javier.palomo.almena@gmail.com>
This commit is contained in:
Javier Palomo 2020-07-28 14:19:58 +02:00
parent 4b5bbc864e
commit 71e88d841b
2 changed files with 16 additions and 14 deletions

View file

@ -26,23 +26,22 @@ import (
"path"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/relabel"
"go.uber.org/atomic"
)
const (
@ -466,7 +465,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
var (
wg sync.WaitGroup
numSuccess uint64
numSuccess atomic.Uint64
)
for _, ams := range amSets {
var (
@ -527,7 +526,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
level.Error(n.logger).Log("alertmanager", url, "count", len(alerts), "msg", "Error sending alert", "err", err)
n.metrics.errors.WithLabelValues(url).Inc()
} else {
atomic.AddUint64(&numSuccess, 1)
numSuccess.Inc()
}
n.metrics.latency.WithLabelValues(url).Observe(time.Since(begin).Seconds())
n.metrics.sent.WithLabelValues(url).Add(float64(len(alerts)))
@ -541,7 +540,7 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
wg.Wait()
return numSuccess > 0
return numSuccess.Load() > 0
}
func alertsToOpenAPIAlerts(alerts []*Alert) models.PostableAlerts {

View file

@ -22,7 +22,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"sync/atomic"
"testing"
"time"
@ -37,6 +36,8 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/prometheus/prometheus/util/testutil"
"go.uber.org/atomic"
)
func TestPostPath(t *testing.T) {
@ -102,10 +103,12 @@ func TestHandlerSendAll(t *testing.T) {
var (
errc = make(chan error, 1)
expected = make([]*Alert, 0, maxBatchSize)
status1, status2 = int32(http.StatusOK), int32(http.StatusOK)
status1, status2 atomic.Int32
)
status1.Store(int32(http.StatusOK))
status2.Store(int32(http.StatusOK))
newHTTPServer := func(u, p string, status *int32) *httptest.Server {
newHTTPServer := func(u, p string, status atomic.Int32) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
defer func() {
@ -128,11 +131,11 @@ func TestHandlerSendAll(t *testing.T) {
if err == nil {
err = alertsEqual(expected, alerts)
}
w.WriteHeader(int(atomic.LoadInt32(status)))
w.WriteHeader(int(status.Load()))
}))
}
server1 := newHTTPServer("prometheus", "testing_password", &status1)
server2 := newHTTPServer("", "", &status2)
server1 := newHTTPServer("prometheus", "testing_password", status1)
server2 := newHTTPServer("", "", status2)
defer server1.Close()
defer server2.Close()
@ -194,11 +197,11 @@ func TestHandlerSendAll(t *testing.T) {
testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
checkNoErr()
atomic.StoreInt32(&status1, int32(http.StatusNotFound))
status1.Store(int32(http.StatusNotFound))
testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly")
checkNoErr()
atomic.StoreInt32(&status2, int32(http.StatusInternalServerError))
status2.Store(int32(http.StatusNotFound))
testutil.Assert(t, !h.sendAll(h.queue...), "all sends succeeded unexpectedly")
checkNoErr()
}