Skip to content

Commit

Permalink
node/telemetry: Fix message encoding for Google Cloud Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tbjump authored and evan-gray committed Mar 9, 2023
1 parent bed48eb commit f311517
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
28 changes: 14 additions & 14 deletions node/pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"time"

"cloud.google.com/go/logging"
google_cloud_logging "cloud.google.com/go/logging"
"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
Expand All @@ -23,17 +23,17 @@ type Telemetry struct {
}

type ExternalLogger interface {
log(time time.Time, message []byte, level zapcore.Level)
log(time time.Time, message json.RawMessage, level zapcore.Level)
flush() error
}

type ExternalLoggerGoogleCloud struct {
*logging.Logger
*google_cloud_logging.Logger
labels map[string]string // labels to add to each cloud log
}

func (logger *ExternalLoggerGoogleCloud) log(time time.Time, message []byte, level zapcore.Level) {
logger.Log(logging.Entry{
func (logger *ExternalLoggerGoogleCloud) log(time time.Time, message json.RawMessage, level zapcore.Level) {
logger.Log(google_cloud_logging.Entry{
Timestamp: time,
Payload: message,
Severity: logLevelSeverity[level],
Expand All @@ -55,14 +55,14 @@ type guardianTelemetryEncoder struct {
// Mirrors the conversion done by zapdriver. We need to convert this
// to proto severity for usage with the SDK client library
// (the JSON value encoded by zapdriver is ignored).
var logLevelSeverity = map[zapcore.Level]logging.Severity{
zapcore.DebugLevel: logging.Debug,
zapcore.InfoLevel: logging.Info,
zapcore.WarnLevel: logging.Warning,
zapcore.ErrorLevel: logging.Error,
zapcore.DPanicLevel: logging.Critical,
zapcore.PanicLevel: logging.Alert,
zapcore.FatalLevel: logging.Emergency,
var logLevelSeverity = map[zapcore.Level]google_cloud_logging.Severity{
zapcore.DebugLevel: google_cloud_logging.Debug,
zapcore.InfoLevel: google_cloud_logging.Info,
zapcore.WarnLevel: google_cloud_logging.Warning,
zapcore.ErrorLevel: google_cloud_logging.Error,
zapcore.DPanicLevel: google_cloud_logging.Critical,
zapcore.PanicLevel: google_cloud_logging.Alert,
zapcore.FatalLevel: google_cloud_logging.Emergency,
}

func (enc *guardianTelemetryEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
Expand Down Expand Up @@ -112,7 +112,7 @@ func NewExternalLogger(skipPrivateLogs bool, externalLogger ExternalLogger) (*Te
// New creates a new Telemetry logger with Google Cloud Logging
// skipPrivateLogs: if set to `true`, logs with the field zap.Bool("_privateLogEntry", true) will not be logged by telemetry.
func New(ctx context.Context, project string, serviceAccountJSON []byte, skipPrivateLogs bool, labels map[string]string) (*Telemetry, error) {
gc, err := logging.NewClient(ctx, project, option.WithCredentialsJSON(serviceAccountJSON))
gc, err := google_cloud_logging.NewClient(ctx, project, option.WithCredentialsJSON(serviceAccountJSON))
if err != nil {
return nil, fmt.Errorf("unable to create logging client: %v", err)
}
Expand Down
17 changes: 16 additions & 1 deletion node/pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package telemetry

import (
"encoding/json"
"sync/atomic"
"testing"
"time"

google_cloud_logging "cloud.google.com/go/logging"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -15,10 +17,23 @@ type externalLoggerMock struct {
eventCounter *atomic.Int64
}

func (logger *externalLoggerMock) log(time time.Time, message []byte, level zapcore.Level) {
func (logger *externalLoggerMock) log(time time.Time, message json.RawMessage, level zapcore.Level) {
if logger.eventCounter != nil {
logger.eventCounter.Add(1)
}

// do the following to make sure that the conversion into a google_cloud_logging.Entry works
entry := google_cloud_logging.Entry{
Timestamp: time,
Payload: message,
Severity: logLevelSeverity[level],
}

_, err := google_cloud_logging.ToLogEntry(entry, "/")
if err != nil {
panic("message could not be converted to google cloud log entry")
}

}
func (logger *externalLoggerMock) flush() error {
return nil
Expand Down

0 comments on commit f311517

Please sign in to comment.