Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sending metadata from environment variables #3

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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