Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to gogoproto #715

Merged
merged 3 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 8 additions & 33 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"sync"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -393,7 +392,7 @@ func (api *API) addSilence(w http.ResponseWriter, r *http.Request) {
}
// Drop start time for new silences so we default to now.
if sil.ID == "" && sil.StartsAt.Before(time.Now()) {
psil.StartsAt = nil
psil.StartsAt = time.Time{}
}

sid, err := api.silences.Create(psil)
Expand Down Expand Up @@ -502,23 +501,11 @@ func matchesFilterLabels(s *types.Silence, matchers []*labels.Matcher) bool {
}

func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
startsAt, err := ptypes.TimestampProto(s.StartsAt)
if err != nil {
return nil, err
}
endsAt, err := ptypes.TimestampProto(s.EndsAt)
if err != nil {
return nil, err
}
updatedAt, err := ptypes.TimestampProto(s.UpdatedAt)
if err != nil {
return nil, err
}
sil := &silencepb.Silence{
Id: s.ID,
StartsAt: startsAt,
EndsAt: endsAt,
UpdatedAt: updatedAt,
StartsAt: s.StartsAt,
EndsAt: s.EndsAt,
UpdatedAt: s.UpdatedAt,
}
for _, m := range s.Matchers {
matcher := &silencepb.Matcher{
Expand All @@ -532,31 +519,19 @@ func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
sil.Matchers = append(sil.Matchers, matcher)
}
sil.Comments = append(sil.Comments, &silencepb.Comment{
Timestamp: updatedAt,
Timestamp: s.UpdatedAt,
Author: s.CreatedBy,
Comment: s.Comment,
})
return sil, nil
}

func silenceFromProto(s *silencepb.Silence) (*types.Silence, error) {
startsAt, err := ptypes.Timestamp(s.StartsAt)
if err != nil {
return nil, err
}
endsAt, err := ptypes.Timestamp(s.EndsAt)
if err != nil {
return nil, err
}
updatedAt, err := ptypes.Timestamp(s.UpdatedAt)
if err != nil {
return nil, err
}
sil := &types.Silence{
ID: s.Id,
StartsAt: startsAt,
EndsAt: endsAt,
UpdatedAt: updatedAt,
StartsAt: s.StartsAt,
EndsAt: s.EndsAt,
UpdatedAt: s.UpdatedAt,
}
for _, m := range s.Matchers {
matcher := &types.Matcher{
Expand Down
53 changes: 10 additions & 43 deletions nflog/nflog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sync"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
pb "github.com/prometheus/alertmanager/nflog/nflogpb"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -340,34 +339,21 @@ func (l *nlog) Log(r *pb.Receiver, gkey []byte, firingAlerts, resolvedAlerts []u

if prevle, ok := l.st[key]; ok {
// Entry already exists, only overwrite if timestamp is newer.
// This may with raciness or clock-drift across AM nodes.
prevts, err := ptypes.Timestamp(prevle.Entry.Timestamp)
if err != nil {
return err
}
if prevts.After(now) {
// This may happen with raciness or clock-drift across AM nodes.
if prevle.Entry.Timestamp.After(now) {
return nil
}
}

ts, err := ptypes.TimestampProto(now)
if err != nil {
return err
}
expts, err := ptypes.TimestampProto(now.Add(l.retention))
if err != nil {
return err
}

e := &pb.MeshEntry{
Entry: &pb.Entry{
Receiver: r,
GroupKey: gkey,
Timestamp: ts,
Timestamp: now,
FiringAlerts: firingAlerts,
ResolvedAlerts: resolvedAlerts,
},
ExpiresAt: expts,
ExpiresAt: now.Add(l.retention),
}
l.gossip.GossipBroadcast(gossipData{
key: e,
Expand All @@ -389,9 +375,10 @@ func (l *nlog) GC() (int, error) {
defer l.mtx.Unlock()

for k, le := range l.st {
if ets, err := ptypes.Timestamp(le.ExpiresAt); err != nil {
return n, err
} else if !ets.After(now) {
if le.ExpiresAt.IsZero() {
return n, errors.New("unexpected zero expiration timestamp")
}
if !le.ExpiresAt.After(now) {
delete(l.st, k)
n++
}
Expand Down Expand Up @@ -589,17 +576,7 @@ func (gd gossipData) Merge(other mesh.GossipData) mesh.GossipData {
gd[k] = e
continue
}
pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
if err != nil {
// TODO(fabxc): log error and skip entry. What can actually error here?
panic(err)
}
ets, err := ptypes.Timestamp(e.Entry.Timestamp)
if err != nil {
// TODO(fabxc): see above.
panic(err)
}
if pts.Before(ets) {
if prev.Entry.Timestamp.Before(e.Entry.Timestamp) {
gd[k] = e
}
}
Expand All @@ -617,17 +594,7 @@ func (gd gossipData) mergeDelta(od gossipData) gossipData {
delta[k] = e
continue
}
pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
if err != nil {
// TODO(fabxc): log error and skip entry. What can actually error here?
panic(err)
}
ets, err := ptypes.Timestamp(e.Entry.Timestamp)
if err != nil {
// TODO(fabxc): see above.
panic(err)
}
if pts.Before(ets) {
if prev.Entry.Timestamp.Before(e.Entry.Timestamp) {
gd[k] = e
delta[k] = e
}
Expand Down
46 changes: 14 additions & 32 deletions nflog/nflog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
pb "github.com/prometheus/alertmanager/nflog/nflogpb"
"github.com/stretchr/testify/require"
)
Expand All @@ -31,7 +29,7 @@ func TestNlogGC(t *testing.T) {
// We only care about key names and expiration timestamps.
newEntry := func(ts time.Time) *pb.MeshEntry {
return &pb.MeshEntry{
ExpiresAt: mustTimestampProto(ts),
ExpiresAt: ts,
}
}

Expand Down Expand Up @@ -69,27 +67,27 @@ func TestNlogSnapshot(t *testing.T) {
Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1},
GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"),
Resolved: false,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
}, {
Entry: &pb.Entry{
GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"),
Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29},
GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"),
Resolved: true,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
}, {
Entry: &pb.Entry{
GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"),
Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0},
GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"),
Resolved: false,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
},
},
},
Expand Down Expand Up @@ -160,7 +158,7 @@ func TestGossipDataMerge(t *testing.T) {
// merging logic.
newEntry := func(ts time.Time) *pb.MeshEntry {
return &pb.MeshEntry{
Entry: &pb.Entry{Timestamp: mustTimestampProto(ts)},
Entry: &pb.Entry{Timestamp: ts},
}
}
cases := []struct {
Expand Down Expand Up @@ -225,27 +223,27 @@ func TestGossipDataCoding(t *testing.T) {
Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1},
GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"),
Resolved: false,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
}, {
Entry: &pb.Entry{
GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"),
Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29},
GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"),
Resolved: true,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
}, {
Entry: &pb.Entry{
GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"),
Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0},
GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"),
Resolved: false,
Timestamp: mustTimestampProto(now),
Timestamp: now,
},
ExpiresAt: mustTimestampProto(now),
ExpiresAt: now,
},
},
},
Expand All @@ -266,19 +264,3 @@ func TestGossipDataCoding(t *testing.T) {
require.Equal(t, in, out, "decoded data doesn't match encoded data")
}
}

func mustTimestamp(ts *timestamp.Timestamp) time.Time {
res, err := ptypes.Timestamp(ts)
if err != nil {
panic(err)
}
return res
}

func mustTimestampProto(ts time.Time) *timestamp.Timestamp {
res, err := ptypes.TimestampProto(ts)
if err != nil {
panic(err)
}
return res
}
Loading