-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a top level package to make it easy to bootstrap an exporter. * Move the flag package to the top level. * Add support for `--web.telemetry-path` flag. Defaults to`/metrics`. * Add a self-check function to the web FlagConfig. Signed-off-by: SuperQ <[email protected]>
- Loading branch information
Showing
3 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package toolkit | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/exporter-toolkit/web" | ||
) | ||
|
||
var ( | ||
ErrNoFlagConfig = errors.New("Missing FlagConfig") | ||
ErrNoHandler = errors.New("Missing one of MetricsHandler or MetricsHandlerFunc") | ||
ErrOneHandler = errors.New("Only one of MetricsHandler or MetricsHandlerFunc allowed") | ||
) | ||
|
||
type Config struct { | ||
MetricsHandler http.Handler | ||
MetricsHandlerFunc *func(http.ResponseWriter, *http.Request) | ||
FlagConfig *web.FlagConfig | ||
LandingConfig web.LandingConfig | ||
Logger log.Logger | ||
} | ||
|
||
func Run(c Config) error { | ||
if c.FlagConfig == nil { | ||
return ErrNoFlagConfig | ||
} | ||
err := c.FlagConfig.CheckFlags() | ||
if err != nil { | ||
return err | ||
} | ||
if c.MetricsHandler == nil && c.MetricsHandlerFunc == nil { | ||
return ErrNoHandler | ||
} | ||
if c.MetricsHandler != nil && c.MetricsHandlerFunc != nil { | ||
return ErrOneHandler | ||
} | ||
if *c.FlagConfig.MetricsPath != "" && c.MetricsHandler != nil { | ||
http.Handle(*c.FlagConfig.MetricsPath, c.MetricsHandler) | ||
} | ||
if *c.FlagConfig.MetricsPath != "" && c.MetricsHandlerFunc != nil { | ||
http.HandleFunc(*c.FlagConfig.MetricsPath, *c.MetricsHandlerFunc) | ||
} | ||
if *c.FlagConfig.MetricsPath != "/" && *c.FlagConfig.MetricsPath != "" { | ||
c.LandingConfig.Links = append(c.LandingConfig.Links, | ||
web.LandingLinks{ | ||
Address: *c.FlagConfig.MetricsPath, | ||
Text: "Metrics", | ||
}, | ||
) | ||
landingPage, err := web.NewLandingPage(c.LandingConfig) | ||
if err != nil { | ||
level.Error(c.Logger).Log("err", err) | ||
os.Exit(1) | ||
} | ||
http.Handle("/", landingPage) | ||
} | ||
|
||
srv := &http.Server{} | ||
return web.ListenAndServe(srv, c.FlagConfig, c.Logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters