Skip to content

Commit

Permalink
feat: add gcloud-logging driver (#538)
Browse files Browse the repository at this point in the history
The gcloud-logging driver enables workflows to send logs to GCP natively using
a driver without shell out to a gcloud command.
  • Loading branch information
Charles546 committed May 24, 2023
1 parent dfd9478 commit 6b173f6
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 138 deletions.
74 changes: 74 additions & 0 deletions drivers/cmd/gcloud-logging/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 PayPal Inc.

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this file,
// you can obtain one at https://mit-license.org/.

// Package gcloud-logging enables Honeydipper to send logs to GCP natively.
package main

import (
"context"
"flag"
"fmt"
"os"

"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
"github.com/honeydipper/honeydipper/pkg/dipper"
"google.golang.org/api/option"
)

func initFlags() {
flag.Usage = func() {
fmt.Printf("%s [ -h ] <service name>\n", os.Args[0])
fmt.Printf(" This driver supports the operator service.")
fmt.Printf(" This program provides honeydipper with capability of sending logs to GCP using logging API.")
}
}

var driver *dipper.Driver

func main() {
initFlags()
flag.Parse()

driver = dipper.NewDriver(os.Args[1], "google-logging")
if driver.Service == "operator" {
driver.Reload = func(*dipper.Message) {}
driver.Commands["log"] = sendLog
driver.Run()
}
}

func sendLog(m *dipper.Message) {
m = dipper.DeserializePayload(m)
severity, _ := dipper.GetMapDataStr(m.Payload, "severity")
logger := getGCPLogger()
logger.Log(logging.Entry{Severity: logging.ParseSeverity(severity), Payload: dipper.MustGetMapData(m.Payload, "payload")})
m.Reply <- dipper.Message{}
}

func getGCPLogger() *logging.Logger {
l, ok := driver.GetOption("_runtime.logger")
if !ok {
ln, _ := driver.GetOptionStr("logger_name")
if ln == "" {
ln = "honeydipper"
}
parent, _ := driver.GetOptionStr("parent")
if parent == "" {
parent, _ = metadata.ProjectID()
}
options := []option.ClientOption{}
serviceAccount, _ := driver.GetOptionStr("service_account")
if serviceAccount != "" {
options = append(options, option.WithCredentialsJSON([]byte(serviceAccount)))
}
client := dipper.Must(logging.NewClient(context.Background(), parent, options...)).(*logging.Client)
l := dipper.Must(client.Logger(ln))
driver.Options.(map[string]interface{})["_runtime"] = map[string]interface{}{"logger": l}
}

return l.(*logging.Logger)
}
32 changes: 17 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/honeydipper/honeydipper
go 1.18

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go v0.110.2 // indirect
cloud.google.com/go/pubsub v1.30.0
cloud.google.com/go/spanner v1.45.0
cloud.google.com/go/storage v1.28.1
cloud.google.com/go/storage v1.29.0
github.com/DataDog/datadog-go v4.8.3+incompatible
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/casbin/casbin/v2 v2.41.0
Expand All @@ -21,7 +21,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0
github.com/googleapis/gax-go/v2 v2.7.1
github.com/googleapis/gax-go/v2 v2.8.0
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -33,12 +33,12 @@ require (
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/qiangmzsx/string-adapter/v2 v2.1.0
github.com/stretchr/testify v1.8.1
golang.org/x/crypto v0.6.0
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0
golang.org/x/term v0.6.0
golang.org/x/text v0.8.0 // indirect
google.golang.org/api v0.114.0
golang.org/x/crypto v0.9.0
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0
golang.org/x/term v0.8.0
golang.org/x/text v0.9.0 // indirect
google.golang.org/api v0.123.0
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
gopkg.in/h2non/gock.v1 v1.0.15
gopkg.in/src-d/go-git.v4 v4.13.1
Expand Down Expand Up @@ -94,7 +94,7 @@ require (
go.opentelemetry.io/otel/trace v1.3.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
Expand All @@ -109,27 +109,29 @@ require (
)

require (
cloud.google.com/go/compute/metadata v0.2.3
cloud.google.com/go/logging v1.7.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/go-redis/redismock/v8 v8.0.6
)

require (
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
cloud.google.com/go/longrunning v0.4.2 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
)
Loading

0 comments on commit 6b173f6

Please sign in to comment.