Skip to content

Commit

Permalink
Merge pull request #3 from yangl900/metadata
Browse files Browse the repository at this point in the history
Support sending metadata from environment variables
  • Loading branch information
yangl900 authored Mar 19, 2018
2 parents c03368f + 13a6983 commit e266927
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
25 changes: 24 additions & 1 deletion log2oms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"
"time"

"github.com/hpcloud/tail"
Expand All @@ -14,6 +15,7 @@ const (
envLogType = "LOG2OMS_LOG_TYPE"
envWorkspaceID = "LOG2OMS_WORKSPACE_ID"
envWorkspaceSecret = "LOG2OMS_WORKSPACE_SECRET"
envMetadataPrefix = "LOG2OMS_META_"
)

var (
Expand All @@ -28,6 +30,22 @@ 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, "=")

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 == "" {
Expand All @@ -50,9 +68,14 @@ func main() {
logType = "container_logs"
}

metadata := metadata()
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 {
Expand Down
22 changes: 15 additions & 7 deletions logclient/logclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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() {
Expand Down

0 comments on commit e266927

Please sign in to comment.