Skip to content

Commit 6a2fb2d

Browse files
authored
Merge pull request #243 from Notifiarr/dn2_more3
Remove expvar and improve prometheus configs.
2 parents 23c5b9f + cb83a0a commit 6a2fb2d

12 files changed

Lines changed: 116 additions & 153 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ linters:
1111
- godot
1212
exclusions:
1313
rules:
14-
- path: docs/api_docs.go|pkg/exp/exp.go
14+
- path: docs/api_docs.go
1515
linters:
1616
- gochecknoinits
1717
- gochecknoglobals

docs/api_docs.go

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ const docTemplateapi = `{
175175
"responses": {
176176
"200": {
177177
"description": "Auth Proxy Prometheus metrics",
178-
"schema": {
179-
"type": "object"
180-
}
178+
"schema": {}
181179
}
182180
}
183181
}
@@ -208,26 +206,6 @@ const docTemplateapi = `{
208206
}
209207
}
210208
},
211-
"/stats": {
212-
"get": {
213-
"description": "Retrieve internal application statistics.",
214-
"produces": [
215-
"application/json"
216-
],
217-
"tags": [
218-
"stats"
219-
],
220-
"summary": "Return auth proxy stats",
221-
"responses": {
222-
"200": {
223-
"description": "Auth Proxy Stats",
224-
"schema": {
225-
"type": "object"
226-
}
227-
}
228-
}
229-
}
230-
},
231209
"/stats/config": {
232210
"get": {
233211
"description": "Retrieve auth proxy configuration, minus passwords.",
@@ -401,7 +379,7 @@ const docTemplateapi = `{
401379
"summary": "Return all cached servers",
402380
"responses": {
403381
"200": {
404-
"description": "List of cached servers. The map key is the server ID.",
382+
"description": "Cached servers. Mp key is server ID.",
405383
"schema": {
406384
"type": "object",
407385
"additionalProperties": {
@@ -447,6 +425,30 @@ const docTemplateapi = `{
447425
}
448426
}
449427
},
428+
"time.Duration": {
429+
"type": "integer",
430+
"format": "int64",
431+
"enum": [
432+
-9223372036854775808,
433+
9223372036854775807,
434+
1,
435+
1000,
436+
1000000,
437+
1000000000,
438+
60000000000,
439+
3600000000000
440+
],
441+
"x-enum-varnames": [
442+
"minDuration",
443+
"maxDuration",
444+
"Nanosecond",
445+
"Microsecond",
446+
"Millisecond",
447+
"Second",
448+
"Minute",
449+
"Hour"
450+
]
451+
},
450452
"userinfo.UserInfo": {
451453
"type": "object",
452454
"properties": {
@@ -467,6 +469,12 @@ const docTemplateapi = `{
467469
"webserver.Config": {
468470
"type": "object",
469471
"properties": {
472+
"connMaxIdleTime": {
473+
"$ref": "#/definitions/time.Duration"
474+
},
475+
"connMaxLifetime": {
476+
"$ref": "#/definitions/time.Duration"
477+
},
470478
"errorFile": {
471479
"type": "string"
472480
},
@@ -479,6 +487,13 @@ const docTemplateapi = `{
479487
"logFile": {
480488
"type": "string"
481489
},
490+
"maxIdleConns": {
491+
"type": "integer"
492+
},
493+
"maxOpenConns": {
494+
"description": "Pool tuning (optional). Zero values use defaults suitable for high-throughput auth lookups.",
495+
"type": "integer"
496+
},
482497
"name": {
483498
"type": "string"
484499
},

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.26.1
55
require (
66
github.com/go-sql-driver/mysql v1.9.3
77
github.com/gorilla/mux v1.8.1
8-
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
98
github.com/lestrrat-go/apache-logformat/v2 v2.0.6
109
github.com/prometheus/client_golang v1.23.2
1110
github.com/swaggo/swag v1.16.6

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4747
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
4848
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
4949
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
50-
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4=
51-
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0=
5250
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
5351
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
5452
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

pkg/exp/exp.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

pkg/exp/metrics.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
// Package exp provides Prometheus metrics registration for the auth proxy.
12
package exp
23

34
import (
5+
"net/http"
6+
"strconv"
47
"time"
58

69
"github.com/prometheus/client_golang/prometheus"
@@ -41,13 +44,23 @@ func (c *CacheCollector) Collect(metrics chan<- prometheus.Metric) {
4144
}
4245
}
4346

47+
// HTTP request event labels for authproxy_http_requests_total (see warmHTTPMetrics).
48+
const (
49+
HTTPEventTotal = "total"
50+
HTTPEventDelete = "delete"
51+
HTTPEventXServer = "x_server"
52+
HTTPEventInvalidKey = "invalid_key"
53+
)
54+
4455
// Metrics contains the exported prometheus metrics used by the application.
4556
type Metrics struct {
4657
QueryErrors *prometheus.CounterVec
4758
QueryMissing *prometheus.CounterVec
4859
QueryTime *prometheus.HistogramVec
4960
ReqTime *prometheus.HistogramVec
5061
Uptime prometheus.CounterFunc
62+
HTTPRequests *prometheus.CounterVec
63+
HTTPResponse *prometheus.CounterVec
5164
}
5265

5366
// GetMetrics sets up metrics on startup.
@@ -63,7 +76,7 @@ func GetMetrics(collector *CacheCollector) *Metrics {
6376
collector.gauge = prometheus.NewDesc("authproxy_cache_gauges", "All cache gauges", []string{"cache", "gauge"}, nil)
6477
prometheus.MustRegister(collector)
6578

66-
return &Metrics{
79+
metrics := &Metrics{
6780
QueryErrors: promauto.NewCounterVec(prometheus.CounterOpts{
6881
Name: "authproxy_db_query_errors_total",
6982
Help: "The total number of DB query errors",
@@ -86,5 +99,47 @@ func GetMetrics(collector *CacheCollector) *Metrics {
8699
Name: "authproxy_uptime_seconds_total",
87100
Help: "Seconds the auth proxy has been running",
88101
}, func() float64 { return time.Since(start).Seconds() }),
102+
HTTPRequests: promauto.NewCounterVec(prometheus.CounterOpts{
103+
Name: "authproxy_http_requests_total",
104+
Help: "HTTP request counts by event type",
105+
}, []string{"event"}),
106+
HTTPResponse: promauto.NewCounterVec(prometheus.CounterOpts{
107+
Name: "authproxy_http_responses_total",
108+
Help: "HTTP responses by status code",
109+
}, []string{"status_code"}),
110+
}
111+
112+
warmHTTPMetrics(metrics)
113+
114+
return metrics
115+
}
116+
117+
// warmHTTPMetrics registers label combinations up front to reduce allocations on the hot path.
118+
func warmHTTPMetrics(metrics *Metrics) {
119+
for _, cache := range []string{"users", "servers"} {
120+
metrics.QueryErrors.WithLabelValues(cache)
121+
metrics.QueryMissing.WithLabelValues(cache)
122+
metrics.QueryTime.WithLabelValues(cache)
123+
metrics.ReqTime.WithLabelValues(cache)
124+
}
125+
126+
for _, event := range []string{
127+
HTTPEventTotal,
128+
HTTPEventDelete,
129+
HTTPEventXServer,
130+
HTTPEventInvalidKey,
131+
} {
132+
metrics.HTTPRequests.WithLabelValues(event)
133+
}
134+
135+
for _, code := range []int{
136+
http.StatusOK,
137+
http.StatusUnauthorized,
138+
http.StatusNotFound,
139+
http.StatusInternalServerError,
140+
http.StatusBadRequest,
141+
http.StatusMethodNotAllowed,
142+
} {
143+
metrics.HTTPResponse.WithLabelValues(strconv.Itoa(code))
89144
}
90145
}

pkg/userinfo/servers.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,22 @@ package userinfo
33

44
import (
55
"context"
6-
"expvar"
76
"fmt"
87
"time"
9-
10-
"github.com/Notifiarr/mysql-auth-proxy/pkg/exp"
11-
"github.com/prometheus/client_golang/prometheus"
128
)
139

1410
const getServerQuery = "SELECT `apikey`,`developmentEnv`,`environment`,`name`,`id`,`discordServer` " +
1511
"FROM `users` WHERE `discordServer` = ?"
1612

1713
// GetServer retrieves a Discord server's information from the database.
1814
func (u *UI) GetServer(ctx context.Context, serverID string) (*UserInfo, error) {
19-
u.exp.Add("Server Queries", 1)
20-
u.exp.Set("Last Server", expvar.Func((&exp.Time{Time: time.Now()}).Since))
15+
start := time.Now()
2116

22-
timer := prometheus.NewTimer(u.metrics.QueryTime.WithLabelValues("servers"))
2317
rows, err := u.dbase.QueryContext(ctx, getServerQuery, serverID)
24-
timer.ObserveDuration()
18+
u.metrics.QueryTime.WithLabelValues("servers").Observe(time.Since(start).Seconds())
2519

2620
if err != nil {
27-
u.exp.Add("Server Errors", 1)
2821
u.metrics.QueryErrors.WithLabelValues("servers").Inc()
29-
3022
return nil, fmt.Errorf("querying database: %w", err)
3123
}
3224

@@ -39,7 +31,6 @@ func (u *UI) GetServer(ctx context.Context, serverID string) (*UserInfo, error)
3931

4032
err := rows.Scan(&user.APIKey, &devAllowed, &user.Environment, &user.Username, &user.UserID, &discord)
4133
if err != nil {
42-
u.exp.Add("Server Errors", 1)
4334
u.Printf("[ERROR] scanning mysql rows: %v", err)
4435
u.metrics.QueryErrors.WithLabelValues("servers").Inc()
4536

@@ -55,14 +46,11 @@ func (u *UI) GetServer(ctx context.Context, serverID string) (*UserInfo, error)
5546

5647
err = rows.Err()
5748
if err != nil {
58-
u.exp.Add("Server Errors", 1)
5949
u.metrics.QueryErrors.WithLabelValues("servers").Inc()
60-
6150
return nil, fmt.Errorf("iterating database rows: %w", err)
6251
}
6352

6453
u.metrics.QueryMissing.WithLabelValues("servers").Inc()
65-
u.exp.Add("Missing Servers", 1)
6654

6755
return DefaultUser(), nil
6856
}

pkg/userinfo/userinfo.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package userinfo
33
import (
44
"database/sql"
55
"errors"
6-
"expvar"
76
"fmt"
87
"log"
98
"os"
@@ -42,7 +41,6 @@ type UI struct {
4241

4342
config *Config
4443
dbase *sql.DB
45-
exp *expvar.Map
4644
metrics *exp.Metrics
4745
}
4846

@@ -69,7 +67,6 @@ func New(config *Config, metrics *exp.Metrics) (*UI, error) {
6967
info := &UI{
7068
metrics: metrics,
7169
config: config,
72-
exp: exp.GetMap("Outgoing MySQL Requests").Init(),
7370
Logger: config.Logger,
7471
}
7572

0 commit comments

Comments
 (0)