Skip to content

Commit

Permalink
Ignore/truncate metric messages that are bigger than a certain thresh…
Browse files Browse the repository at this point in the history
…old to protect against clogging all of cloudprober due to a bug in one of the modules.

ORIGINAL_AUTHOR=Clyde-Xu <clydexu@google.com>
PiperOrigin-RevId: 164014062
  • Loading branch information
Clyde-Xu authored and manugarg committed Aug 2, 2017
1 parent 5afb627 commit b1147ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cloudprober.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"strconv"
"time"

"github.com/golang/glog"
"github.com/google/cloudprober/config"
"github.com/google/cloudprober/logger"
"github.com/google/cloudprober/metrics"
Expand Down Expand Up @@ -154,6 +155,12 @@ func (pr *Prober) Start(ctx context.Context) {
var em *metrics.EventMetrics
for {
em = <-dataChan
var s = em.String()
if len(s) > logger.MaxLogEntrySize {
glog.Warningf("Metric entry for timestamp %d dropped due to large size: %d", em.Timestamp, len(s))
continue
}

// Replicate the surfacer message to every surfacer we have
// registered. Note that s.Write() is expected to be
// non-blocking to avoid blocking of EventMetrics message
Expand Down
12 changes: 11 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const (
// Regular Expression for all characters that are illegal for log names
// Ref: https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry
disapprovedRegExp = "[^A-Za-z0-9_/.-]"

// MaxLogEntrySize Max value of each log entry size
MaxLogEntrySize = 4096
)

// Logger implements a logger that logs messages to Google Cloud Logging. It provides a suite
Expand Down Expand Up @@ -144,8 +147,15 @@ func payloadToString(payload interface{}) string {
// client is not initialized (e.g. if not running on GCE) or cloud logging fails for some reason,
// it writes logs through the traditional logger.
func (l *Logger) log(severity logging.Severity, payload interface{}) {
textPayload := payloadToString(payload)
if len(textPayload) > MaxLogEntrySize {
truncateMsg := "... (truncated)"
truncateMsgLen := len(truncateMsg)
textPayload = textPayload[:MaxLogEntrySize-truncateMsgLen] + truncateMsg
payload = textPayload
}
if l.logc == nil {
genericLog(2, severity, payloadToString(payload))
genericLog(2, severity, textPayload)
return
}
l.logger.Log(logging.Entry{
Expand Down

0 comments on commit b1147ad

Please sign in to comment.