|
| 1 | +// Copyright 2024 Google LLC |
| 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 promapi |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net/http" |
| 20 | + "os" |
| 21 | + "runtime" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-kit/log" |
| 25 | + "github.com/go-kit/log/level" |
| 26 | + promapiv1 "github.com/prometheus/prometheus/web/api/v1" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + timestampFormat = "20060102-15:04:05" |
| 31 | + buildinfoPath = "/api/v1/status/buildinfo" |
| 32 | + exposedVersion = "2.55.0" // We support APIs similar to the last pre 3.0 Prometheus version. |
| 33 | +) |
| 34 | + |
| 35 | +// BuildinfoHandlerFunc simulates the /api/v1/status/buildinfo prometheus endpoint. |
| 36 | +// It is used by Grafana to determine the Prometheus flavor, e.g. to check whether the ruler-api is enabled. |
| 37 | +// binary: e.g. "frontend" or "rule-evaluator". |
| 38 | +func BuildinfoHandlerFunc(logger log.Logger, binaryName, binaryVersion string) func(http.ResponseWriter, *http.Request) { |
| 39 | + return func(w http.ResponseWriter, _ *http.Request) { |
| 40 | + // TODO(yama6a): Populate buildinfo at build time, analogous to: https://github.com/prometheus/common/blob/v0.60.0/version/info.go |
| 41 | + response := promapiv1.PrometheusVersion{ |
| 42 | + Version: exposedVersion, |
| 43 | + Revision: fmt.Sprintf("gmp/%s-%s", binaryName, binaryVersion), |
| 44 | + Branch: "HEAD", |
| 45 | + BuildUser: "gmp@localhost", |
| 46 | + BuildDate: getBinaryCreatedTimestamp(logger), |
| 47 | + GoVersion: runtime.Version(), |
| 48 | + } |
| 49 | + WriteSuccessResponse(logger, w, http.StatusOK, buildinfoPath, response) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func getBinaryCreatedTimestamp(logger log.Logger) string { |
| 54 | + fileInfo, err := os.Stat(os.Args[0]) |
| 55 | + if err != nil { |
| 56 | + _ = level.Error(logger).Log("msg", "Failed to get binary creation timestamp, usinng now()", "err", err) |
| 57 | + return time.Now().Format(timestampFormat) |
| 58 | + } |
| 59 | + |
| 60 | + return fileInfo.ModTime().Format(timestampFormat) |
| 61 | +} |
0 commit comments