@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 73 lines 2.12 KB
Raw
1 // Copyright 2020 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package metrics
16
17 import (
18 "errors"
19 "net/http"
20
21 "github.com/prometheus/client_golang/prometheus"
22
23 "github.com/caddyserver/caddy/v2"
24 )
25
26 func init() {
27 caddy.RegisterModule(AdminMetrics{})
28 }
29
30 // AdminMetrics is a module that serves a metrics endpoint so that any gathered
31 // metrics can be exposed for scraping. This module is not configurable, and
32 // is permanently mounted to the admin API endpoint at "/metrics".
33 // See the Metrics module for a configurable endpoint that is usable if the
34 // Admin API is disabled.
35 type AdminMetrics struct {
36 registry *prometheus.Registry
37
38 metricsHandler http.Handler
39 }
40
41 // CaddyModule returns the Caddy module information.
42 func (AdminMetrics) CaddyModule() caddy.ModuleInfo {
43 return caddy.ModuleInfo{
44 ID: "admin.api.metrics",
45 New: func() caddy.Module { return new(AdminMetrics) },
46 }
47 }
48
49 // Provision -
50 func (m *AdminMetrics) Provision(ctx caddy.Context) error {
51 m.registry = ctx.GetMetricsRegistry()
52 if m.registry == nil {
53 return errors.New("no metrics registry found")
54 }
55 m.metricsHandler = createMetricsHandler(nil, false, m.registry)
56 return nil
57 }
58
59 // Routes returns a route for the /metrics endpoint.
60 func (m *AdminMetrics) Routes() []caddy.AdminRoute {
61 return []caddy.AdminRoute{{Pattern: "/metrics", Handler: caddy.AdminHandlerFunc(m.serveHTTP)}}
62 }
63
64 func (m *AdminMetrics) serveHTTP(w http.ResponseWriter, r *http.Request) error {
65 m.metricsHandler.ServeHTTP(w, r)
66 return nil
67 }
68
69 // Interface guards
70 var (
71 _ caddy.Provisioner = (*AdminMetrics)(nil)
72 _ caddy.AdminRouter = (*AdminMetrics)(nil)
73 )