-
Notifications
You must be signed in to change notification settings - Fork 323
/
trace.go
302 lines (251 loc) · 7.27 KB
/
trace.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.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 trace
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/aws/amazon-ssm-agent/agent/framework/processor/executer/iohandler"
"github.com/aws/amazon-ssm-agent/agent/log"
)
// NanoTime is helper interface for mocking time
type NanoTime interface {
NowUnixNano() int64
}
type TimeImpl struct {
}
func (t *TimeImpl) NowUnixNano() int64 {
return time.Now().UnixNano()
}
type Trace struct {
Tracer Tracer `json:"-"`
Logger log.T `json:"-"`
Operation string
// results
Exitcode int64
Error string `json:",omitempty"`
// timing
Start int64
Stop int64 `json:",omitempty"`
// output
InfoOut bytes.Buffer `json:"-"`
ErrorOut bytes.Buffer `json:"-"`
}
// Tracer is used for collecting traces during a package installation
type Tracer interface {
BeginSection(message string) *Trace
EndSection(trace *Trace) error
AddTrace(trace *Trace)
Traces() []*Trace
PrependTraces([]*Trace)
CurrentTrace() *Trace
ToPluginOutput() iohandler.IOHandler
}
// TracerImpl implements the Tracer interface for collecting traces
type TracerImpl struct {
timeProvider NanoTime
traces []*Trace
tracestack []*Trace
logger log.T
}
func NewTracer(logger log.T) Tracer {
return &TracerImpl{
timeProvider: &TimeImpl{},
logger: logger,
}
}
func NewTracerCustomTime(timeProvider NanoTime, logger log.T) Tracer {
return &TracerImpl{
timeProvider: timeProvider,
logger: logger,
}
}
// BeginSection will create a new trace and registers with the tracer
func (t *TracerImpl) BeginSection(message string) *Trace {
t.logger.Debugf("starting with %s", message)
trace := &Trace{
Tracer: t,
Logger: t.logger,
Operation: message,
Start: t.timeProvider.NowUnixNano(),
}
t.tracestack = append(t.tracestack, trace)
return trace
}
func logTraceDone(logger log.T, trace *Trace) {
if trace.Error != "" {
logger.Errorf("done with %s - error: %s", trace.Operation, trace.Error)
} else if trace.Exitcode != 0 {
logger.Errorf("done with %s - exitcode: %d", trace.Operation, trace.Exitcode)
} else {
logger.Debugf("done with %s", trace.Operation)
}
}
func containsTrace(traces []*Trace, trace *Trace) bool {
for _, x := range traces {
if x == trace {
return true
}
}
return false
}
// EndSection will close the trace provided in the parameter.
// If the provided trace is not the upper one on the stack it will close all
// traces in between.
func (t *TracerImpl) EndSection(trace *Trace) error {
if trace.Start == 0 {
return errors.New("Trying to end section without start time")
}
if !containsTrace(t.tracestack, trace) {
return errors.New("Provided trace not found")
}
logTraceDone(t.logger, trace)
trace.Stop = t.timeProvider.NowUnixNano()
l := len(t.tracestack)
for t.tracestack[l-1] != trace {
var x *Trace
x, t.tracestack = t.tracestack[l-1], t.tracestack[:l-1]
l = len(t.tracestack)
// Trace not closed correctly - closing now
x.Stop = t.timeProvider.NowUnixNano()
t.logger.Tracef("closing skipped trace: %s", x.Operation)
t.traces = append(t.traces, x)
}
// only keep remaining traces
t.tracestack = t.tracestack[:l-1]
// appending traces on end should ensure they are sorted by Stop time
t.traces = append(t.traces, trace)
return nil
}
// AddTrace takes a one time trace without tracking a duration
func (t *TracerImpl) AddTrace(trace *Trace) {
logTraceDone(t.logger, trace)
if trace.Start == 0 {
trace.Start = t.timeProvider.NowUnixNano()
}
t.traces = append(t.traces, trace)
}
// Traces will return all closed traces
func (t *TracerImpl) Traces() []*Trace {
return t.traces
}
// PrependTraces takes existing traces and add them at the beginning
// while also setting their Tracer and Logger
func (t *TracerImpl) PrependTraces(traces []*Trace) {
for _, trace := range traces {
trace.Tracer = t
trace.Logger = t.logger
}
t.traces = append(traces, t.traces...)
}
// CurrentTrace will return the last unclosed trace
// If no trace is open it will return nil
func (t *TracerImpl) CurrentTrace() *Trace {
if len(t.tracestack) > 0 {
return t.tracestack[len(t.tracestack)-1]
} else {
return nil
}
}
// ToPluginOutput will convert info and error output into a IOHandler struct
// It will sort the output by trace end time
func (t *TracerImpl) ToPluginOutput() iohandler.IOHandler {
var out iohandler.DefaultIOHandler
var infoOut bytes.Buffer
var errorOut bytes.Buffer
for _, trace := range t.Traces() {
infoOut.Write(trace.InfoOut.Bytes())
errorOut.Write(trace.ErrorOut.Bytes())
if trace.Error != "" {
errorOut.WriteString(trace.Error)
errorOut.WriteString("\n")
}
}
out.SetStdout(infoOut.String())
out.SetStderr(errorOut.String())
return &out
}
// Trace
// WithExitcode sets the exitcode of the trace
func (t *Trace) WithExitcode(exitcode int64) *Trace {
t.Exitcode = exitcode
return t
}
// WithError sets the error of the trace
func (t *Trace) WithError(err error) *Trace {
if err != nil {
t.Logger.Error(err)
t.Error = err.Error()
} else {
t.Error = ""
}
return t
}
// End will close the trace. Afterwards no other operation should be called.
func (t *Trace) End() error {
return t.Tracer.EndSection(t)
}
// EndWithError just combines two commonly used methods to be able to use it in
// combination with defer
//
// func asdf(tracer Tracer) {
// var err error
// defer tracer.BeginSection("testtracemsg").EndWithError(err)
// ...
// }
func (t *Trace) EndWithError(err *error) *Trace {
t.WithError(*err)
t.End()
return t
}
// PluginOutput
// AppendInfo adds info to PluginOutput StandardOut.
func (t *Trace) AppendInfo(message string) *Trace {
t.Logger.Info(message)
t.InfoOut.WriteString(message)
t.InfoOut.WriteString("\n")
return t
}
// AppendInfof adds info to PluginOutput StandardOut with formatting parameters.
func (t *Trace) AppendInfof(format string, params ...interface{}) *Trace {
return t.AppendInfo(fmt.Sprintf(format, params...))
}
// AppendDebug adds debug info to the trace
func (t *Trace) AppendDebug(message string) *Trace {
t.Logger.Debugf(message)
return t
}
// AppendDebugf adds debug info to the trace
func (t *Trace) AppendDebugf(format string, params ...interface{}) *Trace {
t.Logger.Debugf(format, params...)
return t
}
// AppendError adds errors to PluginOutput StandardErr.
func (t *Trace) AppendError(message string) *Trace {
t.Logger.Error(message)
t.ErrorOut.WriteString(message)
t.ErrorOut.WriteString("\n")
return t
}
// AppendErrorf adds errors to PluginOutput StandardErr with formatting parameters.
func (t *Trace) AppendErrorf(format string, params ...interface{}) *Trace {
t.AppendError(fmt.Sprintf(format, params...))
return t
}
// subtraces
func (t *Trace) AppendWithSubtraces(message string) *Trace {
// TODO: detect subtraces
return t.AppendInfo(message)
}