From b9f7171bf3c4f34723af3a8aeb910ad9a8471d10 Mon Sep 17 00:00:00 2001 From: Anders Liu Date: Sun, 18 Mar 2018 12:36:16 -0700 Subject: [PATCH 1/2] Support log metadata --- log2oms.go | 25 ++++++++++++++++++++++++- logclient/logclient.go | 22 +++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/log2oms.go b/log2oms.go index 97ef693..4be715b 100644 --- a/log2oms.go +++ b/log2oms.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strings" "time" "github.com/hpcloud/tail" @@ -14,6 +15,7 @@ const ( envLogType = "LOG2OMS_LOG_TYPE" envWorkspaceID = "LOG2OMS_WORKSPACE_ID" envWorkspaceSecret = "LOG2OMS_WORKSPACE_SECRET" + envMetadataPrefix = "LOG2OMS_META_" ) var ( @@ -28,6 +30,20 @@ func logLines(client *logclient.LogClient, lines []string) { } } +func metadata() map[string]string { + metadata := make(map[string]string) + for _, e := range os.Environ() { + pair := strings.Split(e, "=") + + if strings.HasPrefix(pair[0], envMetadataPrefix) { + key := strings.TrimPrefix(pair[0], envMetadataPrefix) + metadata[key] = pair[1] + } + } + + return metadata +} + func main() { workspaceID, workspaceSecret := os.Getenv(envWorkspaceID), os.Getenv(envWorkspaceSecret) if workspaceID == "" || workspaceSecret == "" { @@ -50,9 +66,16 @@ func main() { logType = "container_logs" } + metadata := metadata() + metadata["Hostname"], _ = os.Hostname() + + for m := range metadata { + fmt.Printf("[LOG2OMS][%s] %s = %s\n", time.Now().UTC().Format(time.RFC3339), m, metadata[m]) + } + fmt.Printf("[LOG2OMS][%s] Start tail logs from: %s\n", time.Now().UTC().Format(time.RFC3339), logfile) - client := logclient.NewLogClient(workspaceID, workspaceSecret, logType) + client := logclient.NewLogClient(workspaceID, workspaceSecret, logType, metadata) t, err := tail.TailFile(logfile, tail.Config{ReOpen: true, Follow: true}) if err != nil { diff --git a/logclient/logclient.go b/logclient/logclient.go index 0f55e97..2947631 100644 --- a/logclient/logclient.go +++ b/logclient/logclient.go @@ -23,18 +23,20 @@ type LogClient struct { httpClient *http.Client signingKey []byte apiLogsURL string -} - -type log struct { - Data string `json:"data"` + metadata map[string]string } // NewLogClient creates a log client -func NewLogClient(workspaceID, workspaceSecret, logType string) LogClient { +func NewLogClient(workspaceID, workspaceSecret, logType string, metadata map[string]string) LogClient { client := LogClient{ workspaceID: workspaceID, workspaceSecret: workspaceSecret, logType: logType, + metadata: metadata, + } + + if client.metadata == nil { + client.metadata = map[string]string{} } client.httpClient = &http.Client{Timeout: time.Second * 30} @@ -51,9 +53,15 @@ func (c *LogClient) PostMessage(message string, timestamp time.Time) error { // PostMessages logs an array of messages to log analytics service func (c *LogClient) PostMessages(messages []string, timestamp time.Time) error { - var logs []log + var logs []map[string]string for _, m := range messages { - logs = append(logs, log{Data: m}) + log := make(map[string]string, len(c.metadata)+1) + for item := range c.metadata { + log[item] = c.metadata[item] + } + log["message"] = m + + logs = append(logs, log) } if timestamp.IsZero() { From 13a69832532d1209947ba1b5c8eabe49f4627e0b Mon Sep 17 00:00:00 2001 From: Anders Liu Date: Sun, 18 Mar 2018 12:38:10 -0700 Subject: [PATCH 2/2] minor fix --- log2oms.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/log2oms.go b/log2oms.go index 4be715b..518267e 100644 --- a/log2oms.go +++ b/log2oms.go @@ -32,6 +32,8 @@ func logLines(client *logclient.LogClient, lines []string) { func metadata() map[string]string { metadata := make(map[string]string) + metadata["Hostname"], _ = os.Hostname() + for _, e := range os.Environ() { pair := strings.Split(e, "=") @@ -67,8 +69,6 @@ func main() { } metadata := metadata() - metadata["Hostname"], _ = os.Hostname() - for m := range metadata { fmt.Printf("[LOG2OMS][%s] %s = %s\n", time.Now().UTC().Format(time.RFC3339), m, metadata[m]) }