Skip to content

Commit cb8729a

Browse files
authored
Merge pull request #715 from prometheus/gogoproto
Move to gogoproto
2 parents af8cfdd + 15efe6d commit cb8729a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+20558
-642
lines changed

api/api.go

+8-33
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"sync"
2121
"time"
2222

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

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

504503
func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
505-
startsAt, err := ptypes.TimestampProto(s.StartsAt)
506-
if err != nil {
507-
return nil, err
508-
}
509-
endsAt, err := ptypes.TimestampProto(s.EndsAt)
510-
if err != nil {
511-
return nil, err
512-
}
513-
updatedAt, err := ptypes.TimestampProto(s.UpdatedAt)
514-
if err != nil {
515-
return nil, err
516-
}
517504
sil := &silencepb.Silence{
518505
Id: s.ID,
519-
StartsAt: startsAt,
520-
EndsAt: endsAt,
521-
UpdatedAt: updatedAt,
506+
StartsAt: s.StartsAt,
507+
EndsAt: s.EndsAt,
508+
UpdatedAt: s.UpdatedAt,
522509
}
523510
for _, m := range s.Matchers {
524511
matcher := &silencepb.Matcher{
@@ -532,31 +519,19 @@ func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
532519
sil.Matchers = append(sil.Matchers, matcher)
533520
}
534521
sil.Comments = append(sil.Comments, &silencepb.Comment{
535-
Timestamp: updatedAt,
522+
Timestamp: s.UpdatedAt,
536523
Author: s.CreatedBy,
537524
Comment: s.Comment,
538525
})
539526
return sil, nil
540527
}
541528

542529
func silenceFromProto(s *silencepb.Silence) (*types.Silence, error) {
543-
startsAt, err := ptypes.Timestamp(s.StartsAt)
544-
if err != nil {
545-
return nil, err
546-
}
547-
endsAt, err := ptypes.Timestamp(s.EndsAt)
548-
if err != nil {
549-
return nil, err
550-
}
551-
updatedAt, err := ptypes.Timestamp(s.UpdatedAt)
552-
if err != nil {
553-
return nil, err
554-
}
555530
sil := &types.Silence{
556531
ID: s.Id,
557-
StartsAt: startsAt,
558-
EndsAt: endsAt,
559-
UpdatedAt: updatedAt,
532+
StartsAt: s.StartsAt,
533+
EndsAt: s.EndsAt,
534+
UpdatedAt: s.UpdatedAt,
560535
}
561536
for _, m := range s.Matchers {
562537
matcher := &types.Matcher{

nflog/nflog.go

+10-43
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"sync"
2828
"time"
2929

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

341340
if prevle, ok := l.st[key]; ok {
342341
// Entry already exists, only overwrite if timestamp is newer.
343-
// This may with raciness or clock-drift across AM nodes.
344-
prevts, err := ptypes.Timestamp(prevle.Entry.Timestamp)
345-
if err != nil {
346-
return err
347-
}
348-
if prevts.After(now) {
342+
// This may happen with raciness or clock-drift across AM nodes.
343+
if prevle.Entry.Timestamp.After(now) {
349344
return nil
350345
}
351346
}
352347

353-
ts, err := ptypes.TimestampProto(now)
354-
if err != nil {
355-
return err
356-
}
357-
expts, err := ptypes.TimestampProto(now.Add(l.retention))
358-
if err != nil {
359-
return err
360-
}
361-
362348
e := &pb.MeshEntry{
363349
Entry: &pb.Entry{
364350
Receiver: r,
365351
GroupKey: gkey,
366-
Timestamp: ts,
352+
Timestamp: now,
367353
FiringAlerts: firingAlerts,
368354
ResolvedAlerts: resolvedAlerts,
369355
},
370-
ExpiresAt: expts,
356+
ExpiresAt: now.Add(l.retention),
371357
}
372358
l.gossip.GossipBroadcast(gossipData{
373359
key: e,
@@ -389,9 +375,10 @@ func (l *nlog) GC() (int, error) {
389375
defer l.mtx.Unlock()
390376

391377
for k, le := range l.st {
392-
if ets, err := ptypes.Timestamp(le.ExpiresAt); err != nil {
393-
return n, err
394-
} else if !ets.After(now) {
378+
if le.ExpiresAt.IsZero() {
379+
return n, errors.New("unexpected zero expiration timestamp")
380+
}
381+
if !le.ExpiresAt.After(now) {
395382
delete(l.st, k)
396383
n++
397384
}
@@ -589,17 +576,7 @@ func (gd gossipData) Merge(other mesh.GossipData) mesh.GossipData {
589576
gd[k] = e
590577
continue
591578
}
592-
pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
593-
if err != nil {
594-
// TODO(fabxc): log error and skip entry. What can actually error here?
595-
panic(err)
596-
}
597-
ets, err := ptypes.Timestamp(e.Entry.Timestamp)
598-
if err != nil {
599-
// TODO(fabxc): see above.
600-
panic(err)
601-
}
602-
if pts.Before(ets) {
579+
if prev.Entry.Timestamp.Before(e.Entry.Timestamp) {
603580
gd[k] = e
604581
}
605582
}
@@ -617,17 +594,7 @@ func (gd gossipData) mergeDelta(od gossipData) gossipData {
617594
delta[k] = e
618595
continue
619596
}
620-
pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
621-
if err != nil {
622-
// TODO(fabxc): log error and skip entry. What can actually error here?
623-
panic(err)
624-
}
625-
ets, err := ptypes.Timestamp(e.Entry.Timestamp)
626-
if err != nil {
627-
// TODO(fabxc): see above.
628-
panic(err)
629-
}
630-
if pts.Before(ets) {
597+
if prev.Entry.Timestamp.Before(e.Entry.Timestamp) {
631598
gd[k] = e
632599
delta[k] = e
633600
}

nflog/nflog_test.go

+14-32
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"testing"
2121
"time"
2222

23-
"github.com/golang/protobuf/ptypes"
24-
"github.com/golang/protobuf/ptypes/timestamp"
2523
pb "github.com/prometheus/alertmanager/nflog/nflogpb"
2624
"github.com/stretchr/testify/require"
2725
)
@@ -31,7 +29,7 @@ func TestNlogGC(t *testing.T) {
3129
// We only care about key names and expiration timestamps.
3230
newEntry := func(ts time.Time) *pb.MeshEntry {
3331
return &pb.MeshEntry{
34-
ExpiresAt: mustTimestampProto(ts),
32+
ExpiresAt: ts,
3533
}
3634
}
3735

@@ -69,27 +67,27 @@ func TestNlogSnapshot(t *testing.T) {
6967
Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1},
7068
GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"),
7169
Resolved: false,
72-
Timestamp: mustTimestampProto(now),
70+
Timestamp: now,
7371
},
74-
ExpiresAt: mustTimestampProto(now),
72+
ExpiresAt: now,
7573
}, {
7674
Entry: &pb.Entry{
7775
GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"),
7876
Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29},
7977
GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"),
8078
Resolved: true,
81-
Timestamp: mustTimestampProto(now),
79+
Timestamp: now,
8280
},
83-
ExpiresAt: mustTimestampProto(now),
81+
ExpiresAt: now,
8482
}, {
8583
Entry: &pb.Entry{
8684
GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"),
8785
Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0},
8886
GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"),
8987
Resolved: false,
90-
Timestamp: mustTimestampProto(now),
88+
Timestamp: now,
9189
},
92-
ExpiresAt: mustTimestampProto(now),
90+
ExpiresAt: now,
9391
},
9492
},
9593
},
@@ -160,7 +158,7 @@ func TestGossipDataMerge(t *testing.T) {
160158
// merging logic.
161159
newEntry := func(ts time.Time) *pb.MeshEntry {
162160
return &pb.MeshEntry{
163-
Entry: &pb.Entry{Timestamp: mustTimestampProto(ts)},
161+
Entry: &pb.Entry{Timestamp: ts},
164162
}
165163
}
166164
cases := []struct {
@@ -225,27 +223,27 @@ func TestGossipDataCoding(t *testing.T) {
225223
Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1},
226224
GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"),
227225
Resolved: false,
228-
Timestamp: mustTimestampProto(now),
226+
Timestamp: now,
229227
},
230-
ExpiresAt: mustTimestampProto(now),
228+
ExpiresAt: now,
231229
}, {
232230
Entry: &pb.Entry{
233231
GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"),
234232
Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29},
235233
GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"),
236234
Resolved: true,
237-
Timestamp: mustTimestampProto(now),
235+
Timestamp: now,
238236
},
239-
ExpiresAt: mustTimestampProto(now),
237+
ExpiresAt: now,
240238
}, {
241239
Entry: &pb.Entry{
242240
GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"),
243241
Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0},
244242
GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"),
245243
Resolved: false,
246-
Timestamp: mustTimestampProto(now),
244+
Timestamp: now,
247245
},
248-
ExpiresAt: mustTimestampProto(now),
246+
ExpiresAt: now,
249247
},
250248
},
251249
},
@@ -266,19 +264,3 @@ func TestGossipDataCoding(t *testing.T) {
266264
require.Equal(t, in, out, "decoded data doesn't match encoded data")
267265
}
268266
}
269-
270-
func mustTimestamp(ts *timestamp.Timestamp) time.Time {
271-
res, err := ptypes.Timestamp(ts)
272-
if err != nil {
273-
panic(err)
274-
}
275-
return res
276-
}
277-
278-
func mustTimestampProto(ts time.Time) *timestamp.Timestamp {
279-
res, err := ptypes.TimestampProto(ts)
280-
if err != nil {
281-
panic(err)
282-
}
283-
return res
284-
}

0 commit comments

Comments
 (0)