Skip to content

Commit 274019f

Browse files
author
Foivos Filippopoulos
committed
Change logging
1 parent 4fff6d7 commit 274019f

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strings"
77

8+
log "github.com/sirupsen/logrus"
89
"github.com/utilitywarehouse/gcp-disk-snapshotter/models"
910
"github.com/utilitywarehouse/gcp-disk-snapshotter/snapshot"
1011
"github.com/utilitywarehouse/gcp-disk-snapshotter/watch"
@@ -16,15 +17,29 @@ var (
1617
flagZones = flag.String("zones", "", "(Required) Comma separated list of zones where projects disks may live")
1718
flagLabels = flag.String("labels", "", "(Required) Comma separated list of disk labels in format <name>:<value>")
1819
flagSnapPrefix = flag.String("snap_prefix", "", "Prefix for created snapshots")
20+
flagWatchInterval = flag.Int("watch_interval", 60, "Interval between watch cycles in seconds. Defaults to 60s")
1921
flagRetentionHours = flag.Int("retention_hours", 720, "Retention Duration in hours. Defaults to 720h = 1 month")
2022
flagIntervalSeconds = flag.Int("interval_secs", 43200, "Interval between snapshots in seconds. Defaults to 43200s = 12h")
23+
flagLogLevel = flag.String("log_level", "info", "Log Level, defaults to INFO")
2124
)
2225

2326
func usage() {
2427
flag.Usage()
2528
os.Exit(2)
2629
}
2730

31+
func initLogging(logLevel string) {
32+
log.SetFormatter(&log.TextFormatter{
33+
FullTimestamp: true,
34+
})
35+
36+
level, err := log.ParseLevel(logLevel)
37+
if err != nil {
38+
log.Fatal("error parsing log level: %v", err)
39+
}
40+
log.SetLevel(level)
41+
}
42+
2843
func main() {
2944

3045
// Flag Parsing
@@ -57,13 +72,18 @@ func main() {
5772
}
5873

5974
snapPrefix := *flagSnapPrefix
75+
watchInterval := *flagWatchInterval
6076
retentionHours := *flagRetentionHours
6177
intervalSecs := *flagIntervalSeconds
78+
logLevel := *flagLogLevel
79+
80+
// Init logging
81+
initLogging(logLevel)
6282

6383
// Create a snapshotter
6484
gsc := snapshot.CreateGCPSnapClient(project, snapPrefix, zones, *labels)
6585

6686
// Start watching
67-
watch.Watch(gsc, retentionHours, intervalSecs)
87+
watch.Watch(gsc, watchInterval, retentionHours, intervalSecs)
6888

6989
}

snapshot/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package snapshot
33
import (
44
"context"
55
"fmt"
6-
"log"
6+
log "github.com/sirupsen/logrus"
77
"net/http"
88
"strings"
99
"time"
@@ -46,12 +46,12 @@ func CreateGCPSnapClient(project, snapPrefix string, zones []string, labels mode
4646
ctx := context.Background()
4747
googleClient, err := google.DefaultClient(ctx, compute.ComputeScope)
4848
if err != nil {
49-
log.Fatal(err)
49+
log.Fatal("Failed to create google client:", err)
5050
}
5151

5252
computeService, err := compute.New(googleClient)
5353
if err != nil {
54-
log.Fatal(err)
54+
log.Fatal("Failed to create compute service:", err)
5555
}
5656

5757
return &GCPSnapClient{

watch/watch.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package watch
22

33
import (
4+
"fmt"
45
"time"
56

67
log "github.com/sirupsen/logrus"
@@ -11,16 +12,18 @@ import (
1112
// From https://golang.org/src/time/format.go
1213
const GCPSnapshotTimestampLayout string = "2006-01-02T15:04:05Z07:00"
1314

14-
func Watch(gsc *snapshot.GCPSnapClient, retentionHours, intervalSecs int) {
15+
func Watch(gsc *snapshot.GCPSnapClient, watchInterval, retentionHours, intervalSecs int) {
1516

1617
for t := time.Tick(time.Second * 60); ; <-t {
1718

1819
retentionStart := time.Now().Add(-time.Duration(retentionHours) * time.Hour)
1920
lastAcceptedCreation := time.Now().Add(time.Duration(-intervalSecs) * time.Second)
21+
2022
log.WithFields(log.Fields{
21-
"Retention Start": retentionStart,
23+
"Watch Interval in secs": watchInterval,
24+
"Retention Period Start": retentionStart,
2225
"last accepted snap time": lastAcceptedCreation,
23-
}).Info("Initiating watch cycle")
26+
}).Info("Initiating new disk watch cycle")
2427

2528
// Get disks
2629
disks, err := gsc.GetDiskList()
@@ -30,7 +33,7 @@ func Watch(gsc *snapshot.GCPSnapClient, retentionHours, intervalSecs int) {
3033
}
3134

3235
for _, disk := range disks {
33-
log.Info("Checking disk: ", disk.Name)
36+
log.Debug("Checking disk: ", disk.Name)
3437

3538
// Get snapshots per disk created by the snapshotter
3639
snaps, err := gsc.ListClientCreatedSnapshots(disk.SelfLink)
@@ -93,11 +96,12 @@ func deleteSnapshots(gsc *snapshot.GCPSnapClient, sl []compute.Snapshot) error {
9396
}
9497

9598
func createSnapshot(gsc *snapshot.GCPSnapClient, d compute.Disk) error {
96-
log.Info("Taking snapshot of disk: ", d.Name)
99+
log.Debug("Attempt to take snapshot of disk: ", d.Name)
97100
op, err := gsc.CreateSnapshot(d.Name, d.Zone)
98101
if err != nil {
99102
return err
100103
}
104+
log.Info(fmt.Sprintf("New snapshot of disk: %v operation: %v", d.Name, op))
101105

102106
// Create snapshot is a zonal operation!!!
103107
go pollZonalOperation(gsc, op, d.Zone)
@@ -109,8 +113,7 @@ func pollZonalOperation(gsc *snapshot.GCPSnapClient, operation, zone string) {
109113
for {
110114
status, err := gsc.GetZonalOperationStatus(operation, zone)
111115
if err != nil {
112-
log.Info("Operation failed: ", operation)
113-
log.Error(err)
116+
log.Error("Operation failed: ", operation, err)
114117
break
115118
}
116119
if status == "DONE" {
@@ -125,8 +128,7 @@ func pollGlobalOperation(gsc *snapshot.GCPSnapClient, operation string) {
125128
for {
126129
status, err := gsc.GetGlobalOperationStatus(operation)
127130
if err != nil {
128-
log.Info("Operation failed: ", operation)
129-
log.Error(err)
131+
log.Error("Operation failed: ", operation, err)
130132
break
131133
}
132134
if status == "DONE" {

0 commit comments

Comments
 (0)