-
Notifications
You must be signed in to change notification settings - Fork 21
/
identity.go
124 lines (107 loc) · 3.57 KB
/
identity.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
122
123
124
// Copyright observIQ, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package observiq
import (
"runtime"
ios "github.com/observiq/bindplane-agent/internal/os"
"github.com/observiq/bindplane-agent/opamp"
"github.com/open-telemetry/opamp-go/protobufs"
"go.uber.org/zap"
)
// identity contains identifying information about the Collector
type identity struct {
agentID string
agentName *string
serviceName string
version string
labels *string
oSArch string
oSDetails string
oSFamily string
hostname string
mac string
}
// newIdentity constructs a new identity for this collector
func newIdentity(logger *zap.Logger, config opamp.Config, version string) *identity {
// Grab various fields from OS
hostname, err := ios.Hostname()
if err != nil {
logger.Warn("Failed to retrieve hostname for collector. Creating partial identity", zap.Error(err))
}
name, err := ios.Name()
if err != nil {
logger.Warn("Failed to retrieve host details on collector. Creating partial identity", zap.Error(err))
}
return &identity{
agentID: config.AgentID,
agentName: config.AgentName,
serviceName: "com.observiq.collector", // Hardcoded defines this type of agent to the server
version: version,
labels: config.Labels,
oSArch: runtime.GOARCH,
oSDetails: name,
oSFamily: runtime.GOOS,
hostname: hostname,
mac: ios.MACAddress(),
}
}
// Copy creates a deep copy of this identity
func (i identity) Copy() *identity {
identCpy := &identity{
agentID: i.agentID,
serviceName: i.serviceName,
version: i.version,
oSArch: i.oSArch,
oSDetails: i.oSDetails,
oSFamily: i.oSFamily,
hostname: i.hostname,
mac: i.mac,
}
if i.agentName != nil {
identCpy.agentName = new(string)
*identCpy.agentName = *i.agentName
}
if i.labels != nil {
identCpy.labels = new(string)
*identCpy.labels = *i.labels
}
return identCpy
}
func (i *identity) ToAgentDescription() *protobufs.AgentDescription {
identifyingAttributes := []*protobufs.KeyValue{
opamp.StringKeyValue("service.instance.id", i.agentID),
opamp.StringKeyValue("service.name", i.serviceName),
opamp.StringKeyValue("service.version", i.version),
}
if i.agentName != nil {
identifyingAttributes = append(identifyingAttributes, opamp.StringKeyValue("service.instance.name", *i.agentName))
} else {
identifyingAttributes = append(identifyingAttributes, opamp.StringKeyValue("service.instance.name", i.hostname))
}
nonIdentifyingAttributes := []*protobufs.KeyValue{
opamp.StringKeyValue("os.arch", i.oSArch),
opamp.StringKeyValue("os.details", i.oSDetails),
opamp.StringKeyValue("os.family", i.oSFamily),
opamp.StringKeyValue("host.name", i.hostname),
opamp.StringKeyValue("host.mac_address", i.mac),
}
if i.labels != nil {
nonIdentifyingAttributes = append(nonIdentifyingAttributes, opamp.StringKeyValue("service.labels", *i.labels))
}
agentDesc := &protobufs.AgentDescription{
IdentifyingAttributes: identifyingAttributes,
NonIdentifyingAttributes: nonIdentifyingAttributes,
}
return agentDesc
}