-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathevent.go
More file actions
123 lines (109 loc) · 3.63 KB
/
event.go
File metadata and controls
123 lines (109 loc) · 3.63 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package bootstrap
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/telemetry"
)
const (
performSecureTLSBootstrappingGuestAgentEventName = "AKS.Bootstrap.SecureTLSBootstrapping"
)
func getGuestAgentEventsPath() string {
if isWindows() {
return guestAgentEventsPathWindows
}
return guestAgentEventsPathLinux
}
func getEventVersion() string {
if isWindows() {
// corresponds to Microsoft.Compute.CustomScriptExtension-1.10
return "1.10"
}
// corresponds to Microsoft.Azure.Extensions.CustomScript-1.23
return "1.23"
}
type Status string
const (
StatusSuccess Status = "Success"
StatusFailure Status = "Failure"
)
type Result struct {
// Status is the terminal status of the bootstrapping event.
Status Status `json:"Status"`
// ElapsedMilliseconds measures how long the bootstrapping event took to execute, in milliseconds.
ElapsedMilliseconds int64 `json:"ElapsedMilliseconds"`
// Trace maps individual span names to their respective durations.
Trace telemetry.Trace `json:"Trace,omitempty"`
// FinalErrorType indicates the type of error last encountered before the bootstrapping event entered a failed terminal state.
FinalErrorType ErrorType `json:"FinalErrorType,omitempty"`
// FinalError stores the last encountered error before the bootstrapping event entered a failed terminal state.
FinalError string `json:"FinalError,omitempty"`
// ClientVersion stores the client version string.
ClientVersion string `json:"ClientVersion,omitempty"`
}
type Event struct {
Level string
Message string
Start time.Time
End time.Time
}
var _ json.Marshaler = (*Event)(nil)
// Event instances are marshaled according to the GuestAgentGenericLogsSchema object used
// by the azure guest agent (WALinuxAgent).
// For details, see: https://github.com/Azure/WALinuxAgent/blob/master/azurelinuxagent/common/telemetryevent.py#L49
func (e *Event) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
TaskName string `json:"TaskName"`
OperationID string `json:"OperationId"`
Timestamp string `json:"Timestamp"`
Version string `json:"Version"`
EventLevel string `json:"EventLevel"`
Message string `json:"Message"`
EventPID string `json:"EventPid"`
EventTID string `json:"EventTid"`
}{
TaskName: performSecureTLSBootstrappingGuestAgentEventName,
Timestamp: e.Start.Format("2006-01-02 15:04:05.000"),
OperationID: e.End.Format("2006-01-02 15:04:05.000"),
Message: e.Message,
Version: getEventVersion(),
EventLevel: e.Level,
EventPID: "0",
EventTID: "0",
})
}
func (e *Event) WriteWithResult(result *Result) (string, error) {
e.Level = "Informational"
if result.Status == StatusFailure {
e.Level = "Error"
}
resultBytes, err := json.Marshal(result)
if err != nil {
e.Message = "Completed"
} else {
e.Message = string(resultBytes)
}
return e.write()
}
func (e *Event) write() (string, error) {
guestAgentEventsPath := getGuestAgentEventsPath()
if _, err := os.Stat(guestAgentEventsPath); err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("stating guest agent event path %s: %w", guestAgentEventsPath, err)
}
eventFilePath := filepath.Join(guestAgentEventsPath, fmt.Sprintf("%d.json", e.Start.UnixNano()))
eventBytes, err := json.Marshal(e)
if err != nil {
return "", fmt.Errorf("marshalling bootstrap event data: %w", err)
}
if err := os.WriteFile(eventFilePath, eventBytes, 0600); err != nil {
return "", fmt.Errorf("writing bootstrap event data to disk: %w", err)
}
return eventFilePath, nil
}