forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
92 lines (81 loc) · 2.77 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package message
import (
"context"
"time"
"github.com/StackVista/stackstate-agent/pkg/logs/config"
"github.com/StackVista/stackstate-agent/pkg/util"
)
// Message represents a log line sent to datadog, with its metadata
type Message struct {
Content []byte
Origin *Origin
status string
IngestionTimestamp int64
// Optional. Must be UTC. If not provided, time.Now().UTC() will be used
// Used in the Serverless Agent
Timestamp time.Time
// Optional.
// Used in the Serverless Agent
Lambda *Lambda
}
// Lambda is a struct storing information about the Lambda function and function execution.
type Lambda struct {
ARN string
RequestID string
}
// NewMessageWithSource constructs message with content, status and log source.
func NewMessageWithSource(content []byte, status string, source *config.LogSource, ingestionTimestamp int64) *Message {
return NewMessage(content, NewOrigin(source), status, ingestionTimestamp)
}
// NewMessage constructs message with content, status, origin and the ingestion timestamp.
func NewMessage(content []byte, origin *Origin, status string, ingestionTimestamp int64) *Message {
return &Message{
Content: content,
Origin: origin,
status: status,
IngestionTimestamp: ingestionTimestamp,
}
}
// NewMessageFromLambda construts a message with content, status, origin and with the given timestamp and Lambda metadata
func NewMessageFromLambda(content []byte, origin *Origin, status string, utcTime time.Time, ARN, reqID string, ingestionTimestamp int64) *Message {
return &Message{
Content: content,
Origin: origin,
status: status,
IngestionTimestamp: ingestionTimestamp,
Timestamp: utcTime,
Lambda: &Lambda{
ARN: ARN,
RequestID: reqID,
},
}
}
// GetStatus gets the status of the message.
// if status is not set, StatusInfo will be returned.
func (m *Message) GetStatus() string {
if m.status == "" {
m.status = StatusInfo
}
return m.status
}
// GetLatency returns the latency delta from ingestion time until now
func (m *Message) GetLatency() int64 {
return time.Now().UnixNano() - m.IngestionTimestamp
}
// GetHostname returns the hostname to applied the given log message
func (m *Message) GetHostname() string {
if m.Lambda != nil {
return m.Lambda.ARN
}
hostname, err := util.GetHostname(context.TODO())
if err != nil {
// this scenario is not likely to happen since
// the agent cannot start without a hostname
hostname = "unknown"
}
return hostname
}