/
origin.go
121 lines (99 loc) · 3.07 KB
/
origin.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 (
"strings"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
)
// Origin represents the Origin of a message
type Origin struct {
Identifier string
LogSource *sources.LogSource
Offset string
service string
source string
tags []string
}
// NewOrigin returns a new Origin
func NewOrigin(source *sources.LogSource) *Origin {
return &Origin{
LogSource: source,
}
}
// Tags returns the tags of the origin.
//
// The returned slice must not be modified by the caller.
func (o *Origin) Tags(processingTags []string) []string {
return o.tagsToStringArray(processingTags)
}
// TagsPayload returns the raw tag payload of the origin.
func (o *Origin) TagsPayload(processingTags []string) []byte {
var tagsPayload []byte
source := o.Source()
if source != "" {
tagsPayload = append(tagsPayload, []byte("[dd ddsource=\""+source+"\"]")...)
}
sourceCategory := o.LogSource.Config.SourceCategory
if sourceCategory != "" {
tagsPayload = append(tagsPayload, []byte("[dd ddsourcecategory=\""+sourceCategory+"\"]")...)
}
var tags []string
tags = append(tags, o.LogSource.Config.Tags...)
tags = append(tags, o.tags...)
tags = append(tags, processingTags...)
if len(tags) > 0 {
tagsPayload = append(tagsPayload, []byte("[dd ddtags=\""+strings.Join(tags, ",")+"\"]")...)
}
if len(tagsPayload) == 0 {
tagsPayload = []byte{}
}
return tagsPayload
}
// TagsToString encodes tags to a single string, in a comma separated format
func (o *Origin) TagsToString(processingTags []string) string {
tags := o.tagsToStringArray(processingTags)
if tags == nil {
return ""
}
return strings.Join(tags, ",")
}
func (o *Origin) tagsToStringArray(processingTags []string) []string {
tags := o.tags
sourceCategory := o.LogSource.Config.SourceCategory
if sourceCategory != "" {
tags = append(tags, "sourcecategory"+":"+sourceCategory)
}
tags = append(tags, o.LogSource.Config.Tags...)
tags = append(tags, processingTags...)
return tags
}
// SetTags sets the tags of the origin.
func (o *Origin) SetTags(tags []string) {
o.tags = tags
}
// SetSource sets the source of the origin.
func (o *Origin) SetSource(source string) {
o.source = source
}
// Source returns the source of the configuration if set or the source of the message,
// if none are defined, returns an empty string by default.
func (o *Origin) Source() string {
if o.LogSource.Config.Source != "" {
return o.LogSource.Config.Source
}
return o.source
}
// SetService sets the service of the origin.
func (o *Origin) SetService(service string) {
o.service = service
}
// Service returns the service of the configuration if set or the service of the message,
// if none are defined, returns an empty string by default.
func (o *Origin) Service() string {
if o.LogSource.Config.Service != "" {
return o.LogSource.Config.Service
}
return o.service
}