forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemultiplexer_agent_printer.go
187 lines (163 loc) · 5.59 KB
/
demultiplexer_agent_printer.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
// 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 aggregator
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/DataDog/datadog-agent/pkg/collector/check/stats"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
// AgentDemultiplexerPrinter is used to output series, sketches, service checks
// and events.
// Today, this is only used by the `agent check` command.
type AgentDemultiplexerPrinter struct {
DemultiplexerWithAggregator
}
type eventPlatformDebugEvent struct {
RawEvent string `json:",omitempty"`
EventType string
UnmarshalledEvent map[string]interface{} `json:",omitempty"`
}
// PrintMetrics prints metrics aggregator in the Demultiplexer's check samplers (series and sketches),
// service checks buffer, events buffers.
func (p AgentDemultiplexerPrinter) PrintMetrics(checkFileOutput *bytes.Buffer, formatTable bool) {
series, sketches := p.Aggregator().GetSeriesAndSketches(time.Now())
if len(series) != 0 {
fmt.Fprintf(color.Output, "=== %s ===\n", color.BlueString("Series"))
if formatTable {
headers, data := series.MarshalStrings()
var buffer bytes.Buffer
// plain table with no borders
table := tablewriter.NewWriter(&buffer)
table.SetHeader(headers)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.AppendBulk(data)
table.Render()
fmt.Println(buffer.String())
checkFileOutput.WriteString(buffer.String() + "\n")
} else {
j, _ := json.MarshalIndent(series, "", " ")
fmt.Println(string(j))
checkFileOutput.WriteString(string(j) + "\n")
}
}
if len(sketches) != 0 {
fmt.Fprintf(color.Output, "=== %s ===\n", color.BlueString("Sketches"))
j, _ := json.MarshalIndent(sketches, "", " ")
fmt.Println(string(j))
checkFileOutput.WriteString(string(j) + "\n")
}
serviceChecks := p.Aggregator().GetServiceChecks()
if len(serviceChecks) != 0 {
fmt.Fprintf(color.Output, "=== %s ===\n", color.BlueString("Service Checks"))
if formatTable {
headers, data := serviceChecks.MarshalStrings()
var buffer bytes.Buffer
// plain table with no borders
table := tablewriter.NewWriter(&buffer)
table.SetHeader(headers)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.AppendBulk(data)
table.Render()
fmt.Println(buffer.String())
checkFileOutput.WriteString(buffer.String() + "\n")
} else {
j, _ := json.MarshalIndent(serviceChecks, "", " ")
fmt.Println(string(j))
checkFileOutput.WriteString(string(j) + "\n")
}
}
events := p.Aggregator().GetEvents()
if len(events) != 0 {
fmt.Fprintf(color.Output, "=== %s ===\n", color.BlueString("Events"))
checkFileOutput.WriteString("=== Events ===\n")
j, _ := json.MarshalIndent(events, "", " ")
fmt.Println(string(j))
checkFileOutput.WriteString(string(j) + "\n")
}
for k, v := range p.toDebugEpEvents() {
if len(v) > 0 {
if translated, ok := stats.EventPlatformNameTranslations[k]; ok {
k = translated
}
fmt.Fprintf(color.Output, "=== %s ===\n", color.BlueString(k))
checkFileOutput.WriteString(fmt.Sprintf("=== %s ===\n", k))
j, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(j))
checkFileOutput.WriteString(string(j) + "\n")
}
}
}
// toDebugEpEvents transforms the raw event platform messages to eventPlatformDebugEvents which are better for json formatting
func (p AgentDemultiplexerPrinter) toDebugEpEvents() map[string][]eventPlatformDebugEvent {
events := p.Aggregator().GetEventPlatformEvents()
result := make(map[string][]eventPlatformDebugEvent)
for eventType, messages := range events {
var events []eventPlatformDebugEvent
for _, m := range messages {
e := eventPlatformDebugEvent{EventType: eventType, RawEvent: string(m.GetContent())}
err := json.Unmarshal([]byte(e.RawEvent), &e.UnmarshalledEvent)
if err == nil {
e.RawEvent = ""
}
events = append(events, e)
}
result[eventType] = events
}
return result
}
// GetMetricsDataForPrint returns metrics data for series and sketches for printing purpose.
func (p AgentDemultiplexerPrinter) GetMetricsDataForPrint() map[string]interface{} {
aggData := make(map[string]interface{})
agg := p.Aggregator()
series, sketches := agg.GetSeriesAndSketches(time.Now())
if len(series) != 0 {
metrics := make([]interface{}, len(series))
// Workaround to get the sequence of metrics as plain interface{}
for i, serie := range series {
serie.PopulateDeviceField()
serie.PopulateResources()
sj, _ := json.Marshal(serie)
json.Unmarshal(sj, &metrics[i]) //nolint:errcheck
}
aggData["metrics"] = metrics
}
if len(sketches) != 0 {
aggData["sketches"] = sketches
}
serviceChecks := agg.GetServiceChecks()
if len(serviceChecks) != 0 {
aggData["service_checks"] = serviceChecks
}
events := agg.GetEvents()
if len(events) != 0 {
aggData["events"] = events
}
for k, v := range p.toDebugEpEvents() {
aggData[k] = v
}
return aggData
}