-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gcloud-logging driver (#538)
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
1 parent
dfd9478
commit 6b173f6
Showing
3 changed files
with
140 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.