summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/clickhouse/collect_system_events.go
blob: de3c33a1e7875e80be68d01d03a99bd2aec13568 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package clickhouse

import (
	"errors"
	"strconv"

	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
)

const querySystemEvents = `
SELECT
    event,
    value 
FROM
    system.events FORMAT CSVWithNames
`

func (c *ClickHouse) collectSystemEvents(mx map[string]int64) error {
	req, _ := web.NewHTTPRequest(c.Request)
	req.URL.RawQuery = makeURLQuery(querySystemEvents)

	px := "events_"
	var event string
	var n int

	err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) {
		switch column {
		case "event":
			event = value
		case "value":
			if !wantSystemEvents[event] {
				return
			}
			n++
			if v, err := strconv.ParseInt(value, 10, 64); err == nil {
				mx[px+event] = v
			}
		}
	})
	if err != nil {
		return err
	}
	if n == 0 {
		return errors.New("no system events data returned")
	}

	// CH doesn't expose events with 0 values
	for k := range wantSystemEvents {
		k = px + k
		if _, ok := mx[k]; !ok {
			mx[k] = 0
		}
	}

	mx["events_SuccessfulQuery"] = mx["events_Query"] - mx["events_FailedQuery"]
	mx["events_SuccessfulSelectQuery"] = mx["events_SelectQuery"] - mx["events_FailedSelectQuery"]
	mx["events_SuccessfulInsertQuery"] = mx["events_InsertQuery"] - mx["events_FailedInsertQuery"]

	return nil
}

var wantSystemEvents = map[string]bool{
	"SlowRead":                                 true,
	"ReadBackoff":                              true,
	"Query":                                    true,
	"FailedQuery":                              true,
	"QueryTimeMicroseconds":                    true,
	"SelectQuery":                              true,
	"FailedSelectQuery":                        true,
	"SelectQueryTimeMicroseconds":              true,
	"InsertQuery":                              true,
	"FailedInsertQuery":                        true,
	"InsertQueryTimeMicroseconds":              true,
	"QueryPreempted":                           true,
	"QueryMemoryLimitExceeded":                 true,
	"InsertedRows":                             true,
	"InsertedBytes":                            true,
	"DelayedInserts":                           true,
	"DelayedInsertsMilliseconds":               true,
	"RejectedInserts":                          true,
	"SelectedRows":                             true,
	"SelectedBytes":                            true,
	"SelectedParts":                            true,
	"SelectedRanges":                           true,
	"SelectedMarks":                            true,
	"Merge":                                    true,
	"MergedRows":                               true,
	"MergedUncompressedBytes":                  true,
	"MergesTimeMilliseconds":                   true,
	"MergeTreeDataWriterRows":                  true,
	"MergeTreeDataWriterUncompressedBytes":     true,
	"MergeTreeDataWriterCompressedBytes":       true,
	"UncompressedCacheHits":                    true,
	"UncompressedCacheMisses":                  true,
	"MarkCacheHits":                            true,
	"MarkCacheMisses":                          true,
	"Seek":                                     true,
	"FileOpen":                                 true,
	"ReadBufferFromFileDescriptorReadBytes":    true,
	"WriteBufferFromFileDescriptorWriteBytes":  true,
	"ReadBufferFromFileDescriptorRead":         true,
	"WriteBufferFromFileDescriptorWrite":       true,
	"ReadBufferFromFileDescriptorReadFailed":   true,
	"WriteBufferFromFileDescriptorWriteFailed": true,
	"DistributedConnectionTries":               true,
	"DistributedConnectionFailTry":             true,
	"DistributedConnectionFailAtAll":           true,
	"DistributedRejectedInserts":               true,
	"DistributedDelayedInserts":                true,
	"DistributedDelayedInsertsMilliseconds":    true,
	"DistributedSyncInsertionTimeoutExceeded":  true,
	"DistributedAsyncInsertionFailures":        true,
	"ReplicatedDataLoss":                       true,
	"ReplicatedPartFetches":                    true,
	"ReplicatedPartFailedFetches":              true,
	"ReplicatedPartMerges":                     true,
	"ReplicatedPartFetchesOfMerged":            true,
}