|
| 1 | +// Copyright 2023 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +package toolkit |
| 14 | + |
| 15 | +import ( |
| 16 | + "errors" |
| 17 | + "net/http" |
| 18 | + "os" |
| 19 | + |
| 20 | + "github.com/alecthomas/kingpin/v2" |
| 21 | + "github.com/go-kit/log" |
| 22 | + "github.com/go-kit/log/level" |
| 23 | + "github.com/prometheus/common/promlog" |
| 24 | + promlogflag "github.com/prometheus/common/promlog/flag" |
| 25 | + "github.com/prometheus/common/version" |
| 26 | + "github.com/prometheus/exporter-toolkit/web" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + ErrNoFlagConfig = errors.New("Missing FlagConfig") |
| 31 | + ErrNoHandler = errors.New("Missing one of MetricsHandler or MetricsHandlerFunc") |
| 32 | + ErrOneHandler = errors.New("Only one of MetricsHandler or MetricsHandlerFunc allowed") |
| 33 | +) |
| 34 | + |
| 35 | +type Config struct { |
| 36 | + Name string |
| 37 | + Description string |
| 38 | + DefaultAddress string |
| 39 | + Logger log.Logger |
| 40 | + MetricsHandler http.Handler |
| 41 | + MetricsHandlerFunc *func(http.ResponseWriter, *http.Request) |
| 42 | +} |
| 43 | + |
| 44 | +type Toolkit struct { |
| 45 | + Logger log.Logger |
| 46 | + |
| 47 | + flagConfig *web.FlagConfig |
| 48 | + landingConfig web.LandingConfig |
| 49 | + metricsHandler http.Handler |
| 50 | + metricsHandlerFunc *func(http.ResponseWriter, *http.Request) |
| 51 | +} |
| 52 | + |
| 53 | +func New(c Config) *Toolkit { |
| 54 | + t := Toolkit{ |
| 55 | + flagConfig: AddFlags(kingpin.CommandLine, c.DefaultAddress), |
| 56 | + landingConfig: web.LandingConfig{ |
| 57 | + Name: c.Name, |
| 58 | + Description: c.Description, |
| 59 | + Version: version.Info(), |
| 60 | + }, |
| 61 | + metricsHandler: c.MetricsHandler, |
| 62 | + metricsHandlerFunc: c.MetricsHandlerFunc, |
| 63 | + } |
| 64 | + |
| 65 | + promlogConfig := &promlog.Config{} |
| 66 | + promlogflag.AddFlags(kingpin.CommandLine, promlogConfig) |
| 67 | + |
| 68 | + kingpin.Version(version.Print(c.Name)) |
| 69 | + kingpin.HelpFlag.Short('h') |
| 70 | + kingpin.Parse() |
| 71 | + |
| 72 | + t.Logger = promlog.New(promlogConfig) |
| 73 | + |
| 74 | + return &t |
| 75 | +} |
| 76 | + |
| 77 | +func (t *Toolkit) SetMetricsHandler(h http.Handler) { |
| 78 | + t.metricsHandler = h |
| 79 | +} |
| 80 | + |
| 81 | +func (t *Toolkit) SetMetricsHandlerFunc(h *func(http.ResponseWriter, *http.Request)) { |
| 82 | + t.metricsHandlerFunc = h |
| 83 | +} |
| 84 | + |
| 85 | +func (t *Toolkit) Run() error { |
| 86 | + if t.flagConfig == nil { |
| 87 | + return ErrNoFlagConfig |
| 88 | + } |
| 89 | + err := t.flagConfig.CheckFlags() |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + if t.metricsHandler == nil && t.metricsHandlerFunc == nil { |
| 94 | + return ErrNoHandler |
| 95 | + } |
| 96 | + if t.metricsHandler != nil && t.metricsHandlerFunc != nil { |
| 97 | + return ErrOneHandler |
| 98 | + } |
| 99 | + if *t.flagConfig.MetricsPath != "" && t.metricsHandler != nil { |
| 100 | + http.Handle(*t.flagConfig.MetricsPath, t.metricsHandler) |
| 101 | + } |
| 102 | + if *t.flagConfig.MetricsPath != "" && t.metricsHandlerFunc != nil { |
| 103 | + http.HandleFunc(*t.flagConfig.MetricsPath, *t.metricsHandlerFunc) |
| 104 | + } |
| 105 | + if *t.flagConfig.MetricsPath != "/" && *t.flagConfig.MetricsPath != "" { |
| 106 | + t.landingConfig.Links = append(t.landingConfig.Links, |
| 107 | + web.LandingLinks{ |
| 108 | + Address: *t.flagConfig.MetricsPath, |
| 109 | + Text: "Metrics", |
| 110 | + }, |
| 111 | + ) |
| 112 | + landingPage, err := web.NewLandingPage(t.landingConfig) |
| 113 | + if err != nil { |
| 114 | + level.Error(t.Logger).Log("err", err) |
| 115 | + os.Exit(1) |
| 116 | + } |
| 117 | + http.Handle("/", landingPage) |
| 118 | + } |
| 119 | + |
| 120 | + level.Info(t.Logger).Log("msg", "Starting "+t.landingConfig.Name, "version", version.Info()) |
| 121 | + level.Info(t.Logger).Log("msg", "Build context", "build_context", version.BuildContext()) |
| 122 | + |
| 123 | + srv := &http.Server{} |
| 124 | + return web.ListenAndServe(srv, t.flagConfig, t.Logger) |
| 125 | +} |
0 commit comments