-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmetrics.go
110 lines (96 loc) · 3.46 KB
/
metrics.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
//
// Author:: Darren Murray (<[email protected]>)
// Copyright:: Copyright 2024, Lacework Inc.
// License:: Apache License, Version 2.0
//
// 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 api
import (
"os"
"runtime"
)
const DisableTelemetry = "LW_TELEMETRY_DISABLE"
// MetricsService is a service that sends events to Lacework APIv2 Server metrics endpoint
type MetricsService struct {
client *Client
}
func (svc *MetricsService) Send(event Honeyvent) (response HoneyEventResponse, err error) {
if disabled := os.Getenv(DisableTelemetry); disabled != "" {
return HoneyEventResponse{Data: []Honeyvent{{TraceID: "Telemetry Disabled"}}}, nil
}
event.setAccountDetails(*svc.client)
err = svc.client.RequestEncoderDecoder("POST", apiV2HoneyMetrics, event, &response)
return
}
func NewHoneyvent(version, feature, dataset string) Honeyvent {
event := Honeyvent{
Os: runtime.GOOS,
Arch: runtime.GOARCH,
TraceID: newID(),
Version: version,
Dataset: dataset,
Feature: feature,
}
return event
}
func (h *Honeyvent) setAccountDetails(client Client) {
if h.Account == "" {
h.Account = client.account
}
if h.Subaccount == "" {
h.Subaccount = client.subaccount
}
}
// Honeyvent defines what a Honeycomb event looks like for the Lacework CLI
type Honeyvent struct {
Version string `json:"version"`
CfgVersion int `json:"config_version"`
Os string `json:"os"`
Arch string `json:"arch"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Flags []string `json:"flags,omitempty"`
Account string `json:"account,omitempty"`
Subaccount string `json:"subaccount,omitempty"`
Profile string `json:"profile,omitempty"`
ApiKey string `json:"api_key,omitempty"`
Feature string `json:"feature,omitempty"`
FeatureData interface{} `json:"feature.data,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
InstallMethod string `json:"install_method,omitempty"`
Component string `json:"component,omitempty"`
Dataset string `json:"dataset,omitempty"`
// tracing data for multiple events, this is useful for specific features
// within the Lacework CLI such as daily version check, polling mechanism, etc.
TraceID string `json:"trace.trace_id,omitempty"`
SpanID string `json:"trace.span_id,omitempty"`
ParentID string `json:"trace.parent_id,omitempty"`
ContextID string `json:"trace.context_id,omitempty"`
}
type HoneyEventResponse struct {
Data []Honeyvent `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}
func (e *Honeyvent) AddFeatureField(key string, value interface{}) {
if e.FeatureData == nil {
e.FeatureData = map[string]interface{}{key: value}
return
}
if v, ok := e.FeatureData.(map[string]interface{}); ok {
v[key] = value
e.FeatureData = v
}
}