| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package weblog |
| 4 | |
| 5 | import ( |
| 6 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/oldmetrix" |
| 7 | ) |
| 8 | |
| 9 | func newWebLogSummary() oldmetrix.Summary { |
| 10 | return &weblogSummary{oldmetrix.NewSummary()} |
| 11 | } |
| 12 | |
| 13 | type weblogSummary struct { |
| 14 | oldmetrix.Summary |
| 15 | } |
| 16 | |
| 17 | // WriteTo redefines metrics.Summary.WriteTo |
| 18 | // TODO: temporary workaround? |
| 19 | func (s weblogSummary) WriteTo(rv map[string]int64, key string, mul, div int) { |
| 20 | s.Summary.WriteTo(rv, key, mul, div) |
| 21 | if _, ok := rv[key+"_min"]; !ok { |
| 22 | rv[key+"_min"] = 0 |
| 23 | rv[key+"_max"] = 0 |
| 24 | rv[key+"_avg"] = 0 |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | type ( |
| 29 | metricsData struct { |
| 30 | Requests oldmetrix.Counter `stm:"requests"` |
| 31 | ReqUnmatched oldmetrix.Counter `stm:"req_unmatched"` |
| 32 | |
| 33 | RespCode oldmetrix.CounterVec `stm:"resp_code"` |
| 34 | Resp1xx oldmetrix.Counter `stm:"resp_1xx"` |
| 35 | Resp2xx oldmetrix.Counter `stm:"resp_2xx"` |
| 36 | Resp3xx oldmetrix.Counter `stm:"resp_3xx"` |
| 37 | Resp4xx oldmetrix.Counter `stm:"resp_4xx"` |
| 38 | Resp5xx oldmetrix.Counter `stm:"resp_5xx"` |
| 39 | |
| 40 | ReqSuccess oldmetrix.Counter `stm:"req_type_success"` |
| 41 | ReqRedirect oldmetrix.Counter `stm:"req_type_redirect"` |
| 42 | ReqBad oldmetrix.Counter `stm:"req_type_bad"` |
| 43 | ReqError oldmetrix.Counter `stm:"req_type_error"` |
| 44 | |
| 45 | UniqueIPv4 oldmetrix.UniqueCounter `stm:"uniq_ipv4"` |
| 46 | UniqueIPv6 oldmetrix.UniqueCounter `stm:"uniq_ipv6"` |
| 47 | BytesSent oldmetrix.Counter `stm:"bytes_sent"` |
| 48 | BytesReceived oldmetrix.Counter `stm:"bytes_received"` |
| 49 | ReqProcTime oldmetrix.Summary `stm:"req_proc_time"` |
| 50 | ReqProcTimeHist oldmetrix.Histogram `stm:"req_proc_time_hist"` |
| 51 | UpsRespTime oldmetrix.Summary `stm:"upstream_resp_time"` |
| 52 | UpsRespTimeHist oldmetrix.Histogram `stm:"upstream_resp_time_hist"` |
| 53 | |
| 54 | ReqVhost oldmetrix.CounterVec `stm:"req_vhost"` |
| 55 | ReqPort oldmetrix.CounterVec `stm:"req_port"` |
| 56 | ReqMethod oldmetrix.CounterVec `stm:"req_method"` |
| 57 | ReqURLPattern oldmetrix.CounterVec `stm:"req_url_ptn"` |
| 58 | ReqVersion oldmetrix.CounterVec `stm:"req_version"` |
| 59 | ReqSSLProto oldmetrix.CounterVec `stm:"req_ssl_proto"` |
| 60 | ReqSSLCipherSuite oldmetrix.CounterVec `stm:"req_ssl_cipher_suite"` |
| 61 | ReqHTTPScheme oldmetrix.Counter `stm:"req_http_scheme"` |
| 62 | ReqHTTPSScheme oldmetrix.Counter `stm:"req_https_scheme"` |
| 63 | ReqIPv4 oldmetrix.Counter `stm:"req_ipv4"` |
| 64 | ReqIPv6 oldmetrix.Counter `stm:"req_ipv6"` |
| 65 | |
| 66 | ReqCustomField map[string]oldmetrix.CounterVec `stm:"custom_field"` |
| 67 | URLPatternStats map[string]*patternMetrics `stm:"url_ptn"` |
| 68 | |
| 69 | ReqCustomTimeField map[string]*customTimeFieldMetrics `stm:"custom_time_field"` |
| 70 | ReqCustomNumericField map[string]*customNumericFieldMetrics `stm:"custom_numeric_field"` |
| 71 | } |
| 72 | customTimeFieldMetrics struct { |
| 73 | Time oldmetrix.Summary `stm:"time"` |
| 74 | TimeHist oldmetrix.Histogram `stm:"time_hist"` |
| 75 | } |
| 76 | customNumericFieldMetrics struct { |
| 77 | Summary oldmetrix.Summary `stm:"summary"` |
| 78 | |
| 79 | multiplier int |
| 80 | divisor int |
| 81 | } |
| 82 | patternMetrics struct { |
| 83 | RespCode oldmetrix.CounterVec `stm:"resp_code"` |
| 84 | ReqMethod oldmetrix.CounterVec `stm:"req_method"` |
| 85 | BytesSent oldmetrix.Counter `stm:"bytes_sent"` |
| 86 | BytesReceived oldmetrix.Counter `stm:"bytes_received"` |
| 87 | ReqProcTime oldmetrix.Summary `stm:"req_proc_time"` |
| 88 | } |
| 89 | ) |
| 90 | |
| 91 | func newMetricsData(config Config) *metricsData { |
| 92 | return &metricsData{ |
| 93 | ReqVhost: oldmetrix.NewCounterVec(), |
| 94 | ReqPort: oldmetrix.NewCounterVec(), |
| 95 | ReqMethod: oldmetrix.NewCounterVec(), |
| 96 | ReqVersion: oldmetrix.NewCounterVec(), |
| 97 | RespCode: oldmetrix.NewCounterVec(), |
| 98 | ReqSSLProto: oldmetrix.NewCounterVec(), |
| 99 | ReqSSLCipherSuite: oldmetrix.NewCounterVec(), |
| 100 | ReqProcTime: newWebLogSummary(), |
| 101 | ReqProcTimeHist: oldmetrix.NewHistogram(convHistOptionsToMicroseconds(config.Histogram)), |
| 102 | UpsRespTime: newWebLogSummary(), |
| 103 | UpsRespTimeHist: oldmetrix.NewHistogram(convHistOptionsToMicroseconds(config.Histogram)), |
| 104 | UniqueIPv4: oldmetrix.NewUniqueCounter(true), |
| 105 | UniqueIPv6: oldmetrix.NewUniqueCounter(true), |
| 106 | ReqURLPattern: newCounterVecFromPatterns(config.URLPatterns), |
| 107 | ReqCustomField: newReqCustomField(config.CustomFields), |
| 108 | URLPatternStats: newURLPatternStats(config.URLPatterns), |
| 109 | ReqCustomTimeField: newReqCustomTimeField(config.CustomTimeFields), |
| 110 | ReqCustomNumericField: newReqCustomNumericField(config.CustomNumericFields), |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func (m *metricsData) reset() { |
| 115 | m.UniqueIPv4.Reset() |
| 116 | m.UniqueIPv6.Reset() |
| 117 | m.ReqProcTime.Reset() |
| 118 | m.UpsRespTime.Reset() |
| 119 | for _, v := range m.URLPatternStats { |
| 120 | v.ReqProcTime.Reset() |
| 121 | } |
| 122 | for _, v := range m.ReqCustomTimeField { |
| 123 | v.Time.Reset() |
| 124 | } |
| 125 | for _, v := range m.ReqCustomNumericField { |
| 126 | v.Summary.Reset() |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func newCounterVecFromPatterns(patterns []userPattern) oldmetrix.CounterVec { |
| 131 | c := oldmetrix.NewCounterVec() |
| 132 | for _, p := range patterns { |
| 133 | _, _ = c.GetP(p.Name) |
| 134 | } |
| 135 | return c |
| 136 | } |
| 137 | |
| 138 | func newURLPatternStats(patterns []userPattern) map[string]*patternMetrics { |
| 139 | stats := make(map[string]*patternMetrics) |
| 140 | for _, p := range patterns { |
| 141 | stats[p.Name] = &patternMetrics{ |
| 142 | RespCode: oldmetrix.NewCounterVec(), |
| 143 | ReqMethod: oldmetrix.NewCounterVec(), |
| 144 | ReqProcTime: newWebLogSummary(), |
| 145 | } |
| 146 | } |
| 147 | return stats |
| 148 | } |
| 149 | |
| 150 | func newReqCustomField(fields []customField) map[string]oldmetrix.CounterVec { |
| 151 | cf := make(map[string]oldmetrix.CounterVec) |
| 152 | for _, f := range fields { |
| 153 | cf[f.Name] = newCounterVecFromPatterns(f.Patterns) |
| 154 | } |
| 155 | return cf |
| 156 | } |
| 157 | |
| 158 | func newReqCustomTimeField(fields []customTimeField) map[string]*customTimeFieldMetrics { |
| 159 | cf := make(map[string]*customTimeFieldMetrics) |
| 160 | for _, f := range fields { |
| 161 | cf[f.Name] = &customTimeFieldMetrics{ |
| 162 | Time: newWebLogSummary(), |
| 163 | TimeHist: oldmetrix.NewHistogram(convHistOptionsToMicroseconds(f.Histogram)), |
| 164 | } |
| 165 | } |
| 166 | return cf |
| 167 | } |
| 168 | |
| 169 | func newReqCustomNumericField(fields []customNumericField) map[string]*customNumericFieldMetrics { |
| 170 | rv := make(map[string]*customNumericFieldMetrics) |
| 171 | for _, f := range fields { |
| 172 | rv[f.Name] = &customNumericFieldMetrics{ |
| 173 | Summary: newWebLogSummary(), |
| 174 | multiplier: f.Multiplier, |
| 175 | divisor: f.Divisor, |
| 176 | } |
| 177 | } |
| 178 | return rv |
| 179 | } |
| 180 | |
| 181 | // convert histogram options to microseconds (second => us) |
| 182 | func convHistOptionsToMicroseconds(histogram []float64) []float64 { |
| 183 | var buckets []float64 |
| 184 | for _, value := range histogram { |
| 185 | buckets = append(buckets, value*1e6) |
| 186 | } |
| 187 | return buckets |
| 188 | } |