summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/weblog/weblog.go
blob: b9259a9b0782a885f01ee8ad543a87783892b6fc (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
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
// SPDX-License-Identifier: GPL-3.0-or-later

package weblog

import (
	_ "embed"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/logs"
)

//go:embed "config_schema.json"
var configSchema string

func init() {
	module.Register("web_log", module.Creator{
		JobConfigSchema: configSchema,
		Create:          func() module.Module { return New() },
	})
}

func New() *WebLog {
	return &WebLog{
		Config: Config{
			ExcludePath:    "*.gz",
			GroupRespCodes: true,
			ParserConfig: logs.ParserConfig{
				LogType: typeAuto,
				CSV: logs.CSVConfig{
					FieldsPerRecord:  -1,
					Delimiter:        " ",
					TrimLeadingSpace: false,
					CheckField:       checkCSVFormatField,
				},
				LTSV: logs.LTSVConfig{
					FieldDelimiter: "\t",
					ValueDelimiter: ":",
				},
				RegExp: logs.RegExpConfig{},
				JSON:   logs.JSONConfig{},
			},
		},
	}
}

type (
	Config struct {
		logs.ParserConfig   `yaml:",inline" json:""`
		UpdateEvery         int                  `yaml:"update_every" json:"update_every"`
		Path                string               `yaml:"path" json:"path"`
		ExcludePath         string               `yaml:"exclude_path" json:"exclude_path"`
		URLPatterns         []userPattern        `yaml:"url_patterns" json:"url_patterns"`
		CustomFields        []customField        `yaml:"custom_fields" json:"custom_fields"`
		CustomTimeFields    []customTimeField    `yaml:"custom_time_fields" json:"custom_time_fields"`
		CustomNumericFields []customNumericField `yaml:"custom_numeric_fields" json:"custom_numeric_fields"`
		Histogram           []float64            `yaml:"histogram" json:"histogram"`
		GroupRespCodes      bool                 `yaml:"group_response_codes" json:"group_response_codes"`
	}
	userPattern struct {
		Name  string `yaml:"name" json:"name"`
		Match string `yaml:"match" json:"match"`
	}
	customField struct {
		Name     string        `yaml:"name" json:"name"`
		Patterns []userPattern `yaml:"patterns" json:"patterns"`
	}
	customTimeField struct {
		Name      string    `yaml:"name" json:"name"`
		Histogram []float64 `yaml:"histogram" json:"histogram"`
	}
	customNumericField struct {
		Name       string `yaml:"name" json:"name"`
		Units      string `yaml:"units" json:"units"`
		Multiplier int    `yaml:"multiplier" json:"multiplier"`
		Divisor    int    `yaml:"divisor" json:"divisor"`
	}
)

type WebLog struct {
	module.Base
	Config `yaml:",inline" json:""`

	charts *module.Charts

	file   *logs.Reader
	parser logs.Parser
	line   *logLine

	urlPatterns         []*pattern
	customFields        map[string][]*pattern
	customTimeFields    map[string][]float64
	customNumericFields map[string]bool

	mx *metricsData
}

func (w *WebLog) Configuration() any {
	return w.Config
}

func (w *WebLog) Init() error {
	if err := w.createURLPatterns(); err != nil {
		w.Errorf("init failed: %v", err)
		return err
	}

	if err := w.createCustomFields(); err != nil {
		w.Errorf("init failed: %v", err)
		return err
	}

	if err := w.createCustomTimeFields(); err != nil {
		w.Errorf("init failed: %v", err)
		return err
	}

	if err := w.createCustomNumericFields(); err != nil {
		w.Errorf("init failed: %v", err)
	}

	w.createLogLine()
	w.mx = newMetricsData(w.Config)

	return nil
}

func (w *WebLog) Check() error {
	// Note: these inits are here to make auto-detection retry working
	if err := w.createLogReader(); err != nil {
		w.Warning("check failed: ", err)
		return err
	}

	if err := w.createParser(); err != nil {
		w.Warning("check failed: ", err)
		return err
	}

	if err := w.createCharts(w.line); err != nil {
		w.Warning("check failed: ", err)
		return err
	}

	return nil
}

func (w *WebLog) Charts() *module.Charts {
	return w.charts
}

func (w *WebLog) Collect() map[string]int64 {
	mx, err := w.collect()
	if err != nil {
		w.Error(err)
	}

	if len(mx) == 0 {
		return nil
	}
	return mx
}

func (w *WebLog) Cleanup() {
	if w.file != nil {
		_ = w.file.Close()
	}
}