Skip to content

Commit 4e30a5a

Browse files
committed
Add Retention config option
1 parent 7293710 commit 4e30a5a

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

cwlogger.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ type Config struct {
2828
// handled during a PutLogEvents API call and caused a log events to be
2929
// dropped.
3030
ErrorReporter func(err error)
31+
32+
// An optional log group retention time in days. This value is only taken into
33+
// account when creating a log group that does not yet exist. Set to 0
34+
// (default) for no retention policy. Refer to the PutRetentionPolicy API
35+
// documentation for valid values.
36+
Retention int
3137
}
3238

3339
// A Logger represents a single CloudWatch Logs log group.
@@ -40,6 +46,7 @@ type Logger struct {
4046
wg sync.WaitGroup
4147
done chan bool
4248
errorReporter func(err error)
49+
retention int
4350
}
4451

4552
// New creates a new Logger.
@@ -67,6 +74,7 @@ func New(config *Config) (*Logger, error) {
6774
errorReporter: errorReporter,
6875
name: &config.LogGroupName,
6976
svc: config.Client,
77+
retention: config.Retention,
7078
prefix: randomHex(32),
7179
batcher: newBatcher(),
7280
done: make(chan bool),
@@ -135,6 +143,12 @@ func (lg *Logger) createIfNotExists() error {
135143
}
136144
}
137145
}
146+
if lg.retention != 0 {
147+
_, err = lg.svc.PutRetentionPolicy(&cloudwatchlogs.PutRetentionPolicyInput{
148+
LogGroupName: lg.name,
149+
RetentionInDays: aws.Int64(int64(lg.retention)),
150+
})
151+
}
138152
return err
139153
}
140154

cwlogger_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,43 @@ func TestCreatesGroupAndStream(t *testing.T) {
5656
assert.True(t, logStreamCreated)
5757
}
5858

59+
func TestCreatesRetentionPolicy(t *testing.T) {
60+
logGroupCreated := false
61+
retentionPolicyCreated := false
62+
config := &Config{
63+
LogGroupName: "test",
64+
Retention: 90,
65+
}
66+
67+
newLoggerWithServer(config, func(w http.ResponseWriter, r *http.Request) {
68+
if action(r) == "CreateLogGroup" {
69+
logGroupCreated = true
70+
}
71+
if action(r) == "PutRetentionPolicy" {
72+
if !logGroupCreated {
73+
assert.Fail(t, "CreateLogGroup must be called before PutRetentionPolicy")
74+
var data PutRetentionPolicy
75+
parseBody(r, &data)
76+
assert.Equal(t, "test", data.LogGroupName)
77+
assert.Equal(t, 90, data.RetentionInDays)
78+
}
79+
retentionPolicyCreated = true
80+
}
81+
})
82+
83+
assert.True(t, logGroupCreated)
84+
assert.True(t, retentionPolicyCreated)
85+
}
86+
5987
func TestHandlesExistingGroup(t *testing.T) {
6088
logStreamCreated := false
89+
retentionPolicyCreated := false
90+
config := &Config{
91+
LogGroupName: "test",
92+
Retention: 30,
93+
}
6194

62-
newLoggerWithServer(defaultConfig, func(w http.ResponseWriter, r *http.Request) {
95+
newLoggerWithServer(config, func(w http.ResponseWriter, r *http.Request) {
6396
if action(r) == "CreateLogGroup" {
6497
w.WriteHeader(http.StatusBadRequest)
6598
w.Write([]byte(`
@@ -72,9 +105,13 @@ func TestHandlesExistingGroup(t *testing.T) {
72105
if action(r) == "CreateLogStream" {
73106
logStreamCreated = true
74107
}
108+
if action(r) == "PutRetentionPolicy" {
109+
retentionPolicyCreated = true
110+
}
75111
})
76112

77113
assert.True(t, logStreamCreated)
114+
assert.False(t, retentionPolicyCreated)
78115
}
79116

80117
func TestSendsLogsToCloudWatchLogs(t *testing.T) {
@@ -415,6 +452,22 @@ func TestLogStreamCreationFails(t *testing.T) {
415452
assert.Nil(t, logger)
416453
}
417454

455+
func TestPutRetentionPolicyFails(t *testing.T) {
456+
client := newClientWithServer(func(w http.ResponseWriter, r *http.Request) {
457+
if action(r) == "PutRetentionPolicy" {
458+
w.WriteHeader(http.StatusInternalServerError)
459+
w.Write([]byte(`{"__type": "ServiceUnavailableException"}`))
460+
}
461+
})
462+
logger, err := New(&Config{
463+
Client: client,
464+
LogGroupName: "test",
465+
Retention: 180,
466+
})
467+
assert.Error(t, err)
468+
assert.Nil(t, logger)
469+
}
470+
418471
func TestIgnoresBatchItCannotRetry(t *testing.T) {
419472
var calls int
420473

@@ -499,6 +552,11 @@ type PutLogEvents struct {
499552
LogEvents []*LogEvent `json:"logEvents"`
500553
}
501554

555+
type PutRetentionPolicy struct {
556+
LogGroupName string `json:"logGroupName"`
557+
RetentionInDays string `json:"retentionInDays"`
558+
}
559+
502560
type LogEvent struct {
503561
Timestamp int64 `json:"timestamp"`
504562
Message string `json:"message"`

0 commit comments

Comments
 (0)