windows_exporter/internal/httphandler/version.go

46 lines
1.2 KiB
Go
Raw Normal View History

2024-09-10 22:34:10 +00:00
package httphandler
import (
"encoding/json"
"fmt"
"net/http"
"github.com/prometheus/common/version"
)
type VersionHandler struct{}
// Same struct prometheus uses for their /version endpoint.
// Separate copy to avoid pulling all of prometheus as a dependency.
type prometheusVersion struct {
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
BuildUser string `json:"buildUser"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
}
// Interface guard.
var _ http.Handler = (*VersionHandler)(nil)
func NewVersionHandler() VersionHandler {
return VersionHandler{}
}
func (h VersionHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
// we can't use "version" directly as it is a package, and not an object that
// can be serialized.
err := json.NewEncoder(w).Encode(prometheusVersion{
Version: version.Version,
Revision: version.Revision,
Branch: version.Branch,
BuildUser: version.BuildUser,
BuildDate: version.BuildDate,
GoVersion: version.GoVersion,
})
if err != nil {
http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError)
}
}