Skip to content

Commit d7cf7a2

Browse files
author
Travis Tomsu
authored
feat(logging): Use shared logging client and proper severity levels (#20)
1 parent 74c90ef commit d7cf7a2

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

internal/stats.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package internal
33

44
import (
5+
"context"
56
"fmt"
67
"log"
78
"net/http"
@@ -24,6 +25,7 @@ const (
2425
)
2526

2627
const (
28+
StdOutLogID = "spinnaker-stats-stdout"
2729
DefaultLogID = "spinnaker-log-events-staging"
2830
ProdLogID = "spinnaker-log-events-prod"
2931

@@ -41,12 +43,36 @@ var (
4143
ENV_CONFIGURATION,
4244
}
4345

46+
infoLogger *log.Logger
47+
debugLogger *log.Logger
48+
49+
// The eventLogger is the logger that actually writes out the structured
50+
// data from the request payload.
51+
eventLogger *logging.Logger
4452
)
4553

54+
// TODO(ttomsu): Consider wrapping this into a proper Server object and stop abusing package initialization.
55+
// This would also allow a flag to dump stdout to a terminal during local development.
4656
func init() {
47-
if (os.Getenv(LogIDEnvKey) == LogIdProdValue) {
57+
var loggingClient *logging.Client
58+
var err error
59+
if loggingClient, err = logging.NewClient(context.Background(), projectID); err != nil {
60+
log.Fatalf("could not create logging client: %v", err)
61+
return
62+
}
63+
64+
stdOutLogger := loggingClient.Logger(StdOutLogID)
65+
infoLogger = stdOutLogger.StandardLogger(logging.Info)
66+
debugLogger = stdOutLogger.StandardLogger(logging.Debug)
67+
68+
if os.Getenv(LogIDEnvKey) == LogIdProdValue {
4869
logID = ProdLogID
4970
}
71+
infoLogger.Printf("Using event log id: %v", logID)
72+
73+
eventLogger = loggingClient.Logger(logID,
74+
logging.EntryCountThreshold(5),
75+
logging.DelayThreshold(time.Duration(LOGGING_DELAY)*time.Second))
5076
}
5177

5278
func LogEvent(w http.ResponseWriter, r *http.Request) {
@@ -55,7 +81,7 @@ func LogEvent(w http.ResponseWriter, r *http.Request) {
5581
HandleGet(w, r)
5682
return
5783
case http.MethodPost:
58-
log.Println("Received POST method for ", r.URL)
84+
debugLogger.Println("Received POST method for ", r.URL)
5985
handlePost(w, r)
6086
default:
6187
http.Error(w, "405 - Method Not Allowed, punk!", http.StatusMethodNotAllowed)
@@ -75,27 +101,17 @@ func handlePost(w http.ResponseWriter, r *http.Request) {
75101

76102
defer r.Body.Close()
77103
if err := um.Unmarshal(r.Body, event); err != nil {
78-
log.Printf("Error unmarshalling Event: %v", err)
104+
debugLogger.Printf("Error unmarshalling Event: %v", err)
79105
http.Error(w, "Bad input", http.StatusUnprocessableEntity)
80106
return
81107
}
82-
log.Printf("Unmarshaled:\n%+v\n", proto.MarshalTextString(event))
83-
84-
client, err := logging.NewClient(r.Context(), projectID)
85-
if err != nil {
86-
log.Printf("could not create logging client: %v", err)
87-
http.Error(w, "Something went wrong logging this request", http.StatusInternalServerError)
88-
return
89-
}
108+
debugLogger.Printf("Unmarshaled:\n%+v\n", proto.MarshalTextString(event))
90109

91-
logger := client.Logger(logID,
92-
logging.EntryCountThreshold(5),
93-
logging.DelayThreshold(time.Duration(LOGGING_DELAY)*time.Second))
94110
entry := logging.Entry{
95111
Payload: event,
96112
Severity: logging.Info,
97113
Timestamp: time.Now().UTC(),
98114
}
99-
logger.Log(entry)
115+
eventLogger.Log(entry)
100116
fmt.Fprint(w, "Done.")
101117
}

0 commit comments

Comments
 (0)