node: remove google logs from pkg/telemetry
This commit is contained in:
parent
ab4e415f2f
commit
382cb9a0aa
|
@ -1,68 +0,0 @@
|
|||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
google_cloud_logging "cloud.google.com/go/logging"
|
||||
"github.com/blendle/zapdriver"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"google.golang.org/api/option"
|
||||
)
|
||||
|
||||
// ExternalLoggerGoogleCloud implements ExternalLogger for the Google GCP cloud logging.
|
||||
type ExternalLoggerGoogleCloud struct {
|
||||
*google_cloud_logging.Logger
|
||||
labels map[string]string // labels to add to each cloud log
|
||||
}
|
||||
|
||||
func (logger *ExternalLoggerGoogleCloud) log(time time.Time, message json.RawMessage, level zapcore.Level) {
|
||||
entry := google_cloud_logging.Entry{
|
||||
Timestamp: time,
|
||||
Payload: message,
|
||||
Severity: googleLogLevelSeverity[level],
|
||||
Labels: logger.labels,
|
||||
}
|
||||
// call google cloud logger
|
||||
logger.Log(entry)
|
||||
}
|
||||
|
||||
func (logger *ExternalLoggerGoogleCloud) close() error {
|
||||
return logger.Flush()
|
||||
}
|
||||
|
||||
// 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 googleLogLevelSeverity = 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,
|
||||
}
|
||||
|
||||
// NewGoogleCloudLogger 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 NewGoogleCloudLogger(ctx context.Context, project string, skipPrivateLogs bool, labels map[string]string, opts ...option.ClientOption) (*Telemetry, error) {
|
||||
gc, err := google_cloud_logging.NewClient(ctx, project, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create logging client: %v", err)
|
||||
}
|
||||
|
||||
gc.OnError = func(err error) {
|
||||
fmt.Printf("telemetry: logging client error: %v\n", err)
|
||||
}
|
||||
|
||||
return &Telemetry{
|
||||
encoder: &guardianTelemetryEncoder{
|
||||
Encoder: zapcore.NewJSONEncoder(zapdriver.NewProductionEncoderConfig()),
|
||||
logger: &ExternalLoggerGoogleCloud{Logger: gc.Logger("wormhole"), labels: labels},
|
||||
skipPrivateLogs: skipPrivateLogs,
|
||||
},
|
||||
}, nil
|
||||
}
|
|
@ -7,7 +7,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
google_cloud_logging "cloud.google.com/go/logging"
|
||||
"github.com/grafana/loki/pkg/logproto"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
@ -23,16 +24,14 @@ func (logger *externalLoggerMock) log(time time.Time, message json.RawMessage, l
|
|||
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{
|
||||
// do the following to make sure that the conversion into a loki log entry works
|
||||
entry := logproto.Entry{
|
||||
Timestamp: time,
|
||||
Payload: message,
|
||||
Severity: googleLogLevelSeverity[level],
|
||||
Line: string(message),
|
||||
}
|
||||
|
||||
_, err := google_cloud_logging.ToLogEntry(entry, "someProjectId")
|
||||
_, err := entry.Marshal()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("message could not be converted to google cloud log entry: %v", err))
|
||||
panic(fmt.Sprintf("message could not be converted to loki log entry: %v", err))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue