Node: Loki logging (#3183)

* Node: Loki logging

* Node: clean up loki stuff

* fix go.mod

* Code review rework

* Add reference to metrics used by loki

* Minor tweak

---------

Co-authored-by: tbjump <unknown>
This commit is contained in:
bruce-riley 2023-07-12 14:51:01 -05:00 committed by GitHub
parent f10e4d3003
commit 8120772e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1068 additions and 145 deletions

View File

@ -189,11 +189,16 @@ var (
disableHeartbeatVerify *bool
disableTelemetry *bool
disableTelemetry *bool
// Google cloud logging parameters
telemetryKey *string
telemetryServiceAccountFile *string
telemetryProject *string
// Loki cloud logging parameters
telemetryLokiURL *string
bigTablePersistenceEnabled *bool
bigTableGCPProject *string
bigTableInstanceName *string
@ -352,6 +357,8 @@ func init() {
telemetryProject = NodeCmd.Flags().String("telemetryProject", defaultTelemetryProject,
"Google Cloud Project to use for Telemetry logging")
telemetryLokiURL = NodeCmd.Flags().String("telemetryLokiURL", "", "Loki cloud logging URL")
bigTablePersistenceEnabled = NodeCmd.Flags().Bool("bigTablePersistenceEnabled", false, "Turn on forwarding events to BigTable")
bigTableGCPProject = NodeCmd.Flags().String("bigTableGCPProject", "", "Google Cloud project ID for storing events")
bigTableInstanceName = NodeCmd.Flags().String("bigTableInstanceName", "", "BigTable instance name for storing events")
@ -888,27 +895,19 @@ func runNode(cmd *cobra.Command, args []string) {
rootCtxCancel()
}()
var hasTelemetryCredential bool = *telemetryKey != "" || *telemetryServiceAccountFile != ""
usingLoki := *telemetryLokiURL != ""
usingGCP := *telemetryKey != "" || *telemetryServiceAccountFile != ""
var hasTelemetryCredential bool = usingGCP || usingLoki
// Telemetry is enabled by default in mainnet/testnet. In devnet it is disabled by default
if !*disableTelemetry && (!*unsafeDevMode || *unsafeDevMode && hasTelemetryCredential) {
if !hasTelemetryCredential {
logger.Fatal("Please either specify --telemetryKey or --telemetryServiceAccountFile or set --disableTelemetry=false")
logger.Fatal("Please either specify --telemetryKey, --telemetryServiceAccountFile or --telemetryLokiURL or set --disableTelemetry=false")
}
var options []googleapi_option.ClientOption
if *telemetryKey != "" {
creds, err := decryptTelemetryServiceAccount()
if err != nil {
logger.Fatal("Failed to decrypt telemetry service account", zap.Error(err))
}
options = append(options, googleapi_option.WithCredentialsJSON(creds))
}
if *telemetryServiceAccountFile != "" {
options = append(options, googleapi_option.WithCredentialsFile(*telemetryServiceAccountFile))
if usingLoki && usingGCP {
logger.Fatal("May only enable one telemetry logger at a time, either specify --telemetryLokiURL or --telemetryKey/--telemetryServiceAccountFile")
}
// Get libp2p peer ID from private key
@ -927,18 +926,45 @@ func runNode(cmd *cobra.Command, args []string) {
}
skipPrivateLogs := !*publicRpcLogToTelemetry
tm, err := telemetry.New(context.Background(), *telemetryProject, skipPrivateLogs, labels, options...)
if err != nil {
logger.Fatal("Failed to initialize telemetry", zap.Error(err))
}
defer tm.Close()
logger = tm.WrapLogger(logger)
logger.Info("Telemetry enabled",
zap.String("publicRpcLogDetail", *publicRpcLogDetailStr),
zap.Bool("logPublicRpcToTelemetry", *publicRpcLogToTelemetry))
} else {
logger.Info("Telemetry disabled")
var tm *telemetry.Telemetry
if usingLoki {
logger.Info("Using Loki telemetry logger",
zap.String("publicRpcLogDetail", *publicRpcLogDetailStr),
zap.Bool("logPublicRpcToTelemetry", *publicRpcLogToTelemetry))
tm, err = telemetry.NewLokiCloudLogger(context.Background(), logger, *telemetryLokiURL, "wormhole", true, labels)
if err != nil {
logger.Fatal("Failed to initialize telemetry", zap.Error(err))
}
} else {
logger.Info("Using Google Cloud telemetry logger",
zap.String("publicRpcLogDetail", *publicRpcLogDetailStr),
zap.Bool("logPublicRpcToTelemetry", *publicRpcLogToTelemetry))
var options []googleapi_option.ClientOption
if *telemetryKey != "" {
creds, err := decryptTelemetryServiceAccount()
if err != nil {
logger.Fatal("Failed to decrypt telemetry service account", zap.Error(err))
}
options = append(options, googleapi_option.WithCredentialsJSON(creds))
}
if *telemetryServiceAccountFile != "" {
options = append(options, googleapi_option.WithCredentialsFile(*telemetryServiceAccountFile))
}
tm, err = telemetry.NewGoogleCloudLogger(context.Background(), *telemetryProject, skipPrivateLogs, labels, options...)
if err != nil {
logger.Fatal("Failed to initialize telemetry", zap.Error(err))
}
}
defer tm.Close()
logger = tm.WrapLogger(logger) // Wrap logger with telemetry logger
}
// log golang version

View File

@ -3,10 +3,10 @@ module github.com/certusone/wormhole/node
go 1.20
require (
cloud.google.com/go/bigtable v1.10.1
cloud.google.com/go/bigtable v1.18.1
github.com/celo-org/celo-blockchain v1.5.5
github.com/cenkalti/backoff/v4 v4.1.3
github.com/coreos/go-systemd v0.0.0-20190620071333-e64a0ec8b42a
github.com/cenkalti/backoff/v4 v4.2.0
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/davecgh/go-spew v1.1.1
github.com/dgraph-io/badger/v3 v3.2103.1
github.com/ethereum/go-ethereum v1.10.21
@ -26,12 +26,12 @@ require (
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.9.0
github.com/near/borsh-go v0.3.0
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/tendermint/tendermint v0.34.24
github.com/tidwall/gjson v1.14.4
go.uber.org/zap v1.24.0
@ -53,10 +53,14 @@ require (
github.com/blendle/zapdriver v1.3.1
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/cosmos/cosmos-sdk v0.45.11
github.com/go-kit/kit v0.12.0
github.com/google/uuid v1.3.0
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/grafana/dskit v0.0.0-20230201083518-528d8a7d52f2
github.com/grafana/loki v1.6.2-0.20230503110102-9f809eda70ba
github.com/hashicorp/golang-lru v0.6.0
github.com/holiman/uint256 v1.2.1
github.com/prometheus/client_model v0.3.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
github.com/wormhole-foundation/wormchain v0.0.0-00010101000000-000000000000
github.com/wormhole-foundation/wormhole/sdk v0.0.0-20220926172624-4b38dc650bb0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
@ -70,7 +74,7 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
contrib.go.opencensus.io/exporter/stackdriver v0.13.14 // indirect
contrib.go.opencensus.io/exporter/stackdriver v0.13.11 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
@ -79,21 +83,29 @@ require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/Workiva/go-datastructures v1.0.53 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/algorand/go-codec/codec v1.1.8 // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/aws/aws-sdk-go v1.44.187 // indirect
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee // indirect
github.com/celo-org/celo-bls-go v0.2.4 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe // indirect
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha8 // indirect
@ -108,6 +120,7 @@ require (
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
@ -117,26 +130,39 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/envoyproxy/go-control-plane v0.11.1-0.20230524094728-9239064ad72f // indirect
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gagliardetto/binary v0.7.3 // indirect
github.com/gagliardetto/treeout v0.1.4 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/spec v0.20.7 // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.22.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/gateway v1.1.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/gogo/status v1.1.1 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
@ -145,7 +171,7 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/flatbuffers v1.12.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/orderedcode v0.0.1 // indirect
@ -153,15 +179,25 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/grafana/loki/pkg/push v0.0.0-20230127102416-571f88bc5765 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/consul/api v1.18.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/memberlist v0.5.0 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/huin/goupnp v1.1.0 // indirect
@ -175,8 +211,12 @@ require (
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/klauspost/compress v1.16.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
@ -194,6 +234,7 @@ require (
github.com/libp2p/go-yamux/v4 v4.0.0 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
@ -220,21 +261,28 @@ require (
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/onsi/gomega v1.27.4 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/alertmanager v0.25.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/exporter-toolkit v0.8.2 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/prometheus v0.42.0 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
@ -249,12 +297,17 @@ require (
github.com/rs/cors v1.8.2 // indirect
github.com/rs/zerolog v1.27.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/sercand/kuberesolver/v4 v4.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/streamingfast/logging v0.0.0-20220813175024-b4fbb0e893df // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/btcd v0.1.1 // indirect
@ -268,22 +321,33 @@ require (
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/tyler-smith/go-bip39 v1.0.2 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/weaveworks/common v0.0.0-20230531151736-e2613bee6b73 // indirect
github.com/weaveworks/promrus v1.2.0 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.etcd.io/etcd/api/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/v3 v3.5.5 // indirect
go.mongodb.org/mongo-driver v1.11.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.37.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.16.1 // indirect
go.uber.org/fx v1.19.2 // indirect
go.uber.org/goleak v1.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.7.0 // indirect

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,68 @@
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
}

131
node/pkg/telemetry/loki.go Normal file
View File

@ -0,0 +1,131 @@
// We are using promtail client version v2.8.2:
// promtail must be added using commit hashes instead of version tags, see https://github.com/grafana/loki/issues/2826
// go get github.com/grafana/loki/clients/pkg/promtail/client@9f809eda70babaf583bdf6bf335a28038f286618
package telemetry
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/api/option"
gkzap "github.com/go-kit/kit/log/zap"
"github.com/grafana/dskit/backoff"
"github.com/grafana/dskit/flagext"
"github.com/grafana/loki/clients/pkg/promtail/api"
"github.com/grafana/loki/clients/pkg/promtail/client"
"github.com/grafana/loki/pkg/logproto"
lokiflag "github.com/grafana/loki/pkg/util/flagext"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
)
// ExternalLoggerLoki implements ExternalLogger for the Grafana Loki cloud logging.
type ExternalLoggerLoki struct {
// c is the promtail client.
c client.Client
// labels is the set of labels to be added to each log entry, based on the severity (since severity is one of the labels).
labels map[zapcore.Level]model.LabelSet
// localLogger is the zap localLogger used to log errors generated by the loki adapter. It does not use telemetry.
localLogger *zap.Logger
}
func (logger *ExternalLoggerLoki) log(time time.Time, message json.RawMessage, level zapcore.Level) {
lokiLabels := logger.labels[level]
bytes, err := message.MarshalJSON()
if err != nil {
logger.localLogger.Error("Failed to marshal log message", zap.Error(err))
return
}
entry := api.Entry{
Entry: logproto.Entry{
Timestamp: time,
Line: string(bytes),
},
Labels: lokiLabels,
}
logger.c.Chan() <- entry
}
func (logger *ExternalLoggerLoki) close() error {
logger.c.Stop()
return nil
}
// NewLokiCloudLogger creates a new Telemetry logger using Grafana Loki Cloud Logging.
// skipPrivateLogs: if set to `true`, logs with the field zap.Bool("_privateLogEntry", true) will not be logged by telemetry.
func NewLokiCloudLogger(ctx context.Context, logger *zap.Logger, url string, productName string, skipPrivateLogs bool, labels map[string]string, opts ...option.ClientOption) (*Telemetry, error) {
// The localLogger is used to log errors generated by the loki adapter. It does not use telemetry.
localLogger := logger.With(zap.String("component", "loki"))
// The gkLogger is passed into the loki client, which expects a go-kit logger.
gkLogger := gkzap.NewZapSugarLogger(localLogger, zapcore.ErrorLevel)
// Loki pegs these metrics: https://github.com/grafana/loki/blob/main/clients/pkg/promtail/client/client.go#L71-L127
m := client.NewMetrics(prometheus.DefaultRegisterer)
serverURL := flagext.URLValue{}
err := serverURL.Set(url)
if err != nil {
return nil, fmt.Errorf("failed to parse Loki client url: %v", err)
}
cfg := client.Config{
URL: serverURL,
DropRateLimitedBatches: true,
Client: config.HTTPClientConfig{},
// TenantID: We are not using the tenantID.
// Using default values from by promtail:
// https://github.com/grafana/loki/blob/bad691b5091f1ad2f09dbfb30d5395b8f57a3bcd/docs/sources/clients/promtail/configuration.md
BatchWait: 1 * time.Minute,
BatchSize: 1048576,
BackoffConfig: backoff.Config{MinBackoff: 500 * time.Millisecond, MaxBackoff: 5 * time.Minute, MaxRetries: 10},
ExternalLabels: lokiflag.LabelSet{},
Timeout: 10 * time.Second,
}
clientMaxLineSize := 1024
clientMaxLineSizeTruncate := true
c, err := client.New(m, cfg, 0, clientMaxLineSize, clientMaxLineSizeTruncate, gkLogger)
if err != nil {
return nil, fmt.Errorf("failed to create Loki client: %v", err)
}
// Since severity is one of the labels, create a label set for each severity to avoid copying the labels map for each log entry.
lokiLabels := make(map[zapcore.Level]model.LabelSet)
for level := zapcore.DebugLevel; level <= zapcore.FatalLevel; level++ {
levLabels := model.LabelSet{}
for k, v := range labels {
levLabels[model.LabelName(k)] = model.LabelValue(v)
}
levLabels[model.LabelName("product")] = model.LabelValue(productName)
levLabels[model.LabelName("severity")] = model.LabelValue(level.CapitalString())
lokiLabels[level] = levLabels
}
return &Telemetry{
encoder: &guardianTelemetryEncoder{
Encoder: zapcore.NewJSONEncoder(zapdriver.NewProductionEncoderConfig()),
logger: &ExternalLoggerLoki{
c: c,
labels: lokiLabels,
localLogger: localLogger,
},
skipPrivateLogs: skipPrivateLogs,
},
}, nil
}

View File

@ -2,18 +2,14 @@ package telemetry
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"time"
google_cloud_logging "cloud.google.com/go/logging"
"github.com/blendle/zapdriver"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
"google.golang.org/api/option"
)
const telemetryLogLevel = zap.InfoLevel
@ -24,49 +20,16 @@ type Telemetry struct {
type ExternalLogger interface {
log(time time.Time, message json.RawMessage, level zapcore.Level)
flush() error
close() error
}
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: logLevelSeverity[level],
Labels: logger.labels,
}
// call google cloud logger
logger.Log(entry)
}
func (logger *ExternalLoggerGoogleCloud) flush() error {
return logger.Flush()
}
// guardianTelemetryEncoder is a wrapper around zapcore.jsonEncoder that logs to google cloud logging
// guardianTelemetryEncoder is a wrapper around zapcore.jsonEncoder that logs to cloud based logging
type guardianTelemetryEncoder struct {
zapcore.Encoder // zapcore.jsonEncoder
logger ExternalLogger
skipPrivateLogs bool
}
// 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]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) {
buf, err := enc.Encoder.EncodeEntry(entry, fields)
if err != nil {
@ -111,27 +74,6 @@ func NewExternalLogger(skipPrivateLogs bool, externalLogger ExternalLogger) (*Te
}, nil
}
// 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, 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
}
func (s *Telemetry) WrapLogger(logger *zap.Logger) *zap.Logger {
tc := zapcore.NewCore(
s.encoder,
@ -145,5 +87,5 @@ func (s *Telemetry) WrapLogger(logger *zap.Logger) *zap.Logger {
}
func (s *Telemetry) Close() error {
return s.encoder.logger.flush()
return s.encoder.logger.close()
}

View File

@ -27,7 +27,7 @@ func (logger *externalLoggerMock) log(time time.Time, message json.RawMessage, l
entry := google_cloud_logging.Entry{
Timestamp: time,
Payload: message,
Severity: logLevelSeverity[level],
Severity: googleLogLevelSeverity[level],
}
_, err := google_cloud_logging.ToLogEntry(entry, "someProjectId")
@ -36,7 +36,7 @@ func (logger *externalLoggerMock) log(time time.Time, message json.RawMessage, l
}
}
func (logger *externalLoggerMock) flush() error {
func (logger *externalLoggerMock) close() error {
return nil
}