Dragonfly2
68 строк · 2.2 Кб
1/*
2* Copyright 2020 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package metrics
18
19import (
20"net/http"
21
22grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
23"github.com/prometheus/client_golang/prometheus"
24"github.com/prometheus/client_golang/prometheus/promauto"
25"github.com/prometheus/client_golang/prometheus/promhttp"
26"google.golang.org/grpc"
27
28"d7y.io/dragonfly/v2/pkg/types"
29"d7y.io/dragonfly/v2/trainer/config"
30"d7y.io/dragonfly/v2/version"
31)
32
33// Variables declared for metrics.
34var (
35TrainCount = promauto.NewCounter(prometheus.CounterOpts{
36Namespace: types.MetricsNamespace,
37Subsystem: types.TrainerMetricsName,
38Name: "training_total",
39Help: "Counter of the number of the training.",
40})
41
42TrainFailureCount = promauto.NewCounter(prometheus.CounterOpts{
43Namespace: types.MetricsNamespace,
44Subsystem: types.TrainerMetricsName,
45Name: "training_failure_total",
46Help: "Counter of the number of failed of the training.",
47})
48
49VersionGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
50Namespace: types.MetricsNamespace,
51Subsystem: types.TrainerMetricsName,
52Name: "version",
53Help: "Version info of the service.",
54}, []string{"major", "minor", "git_version", "git_commit", "platform", "build_time", "go_version", "go_tags", "go_gcflags"})
55)
56
57func New(cfg *config.MetricsConfig, svr *grpc.Server) *http.Server {
58grpc_prometheus.Register(svr)
59
60mux := http.NewServeMux()
61mux.Handle("/metrics", promhttp.Handler())
62
63VersionGauge.WithLabelValues(version.Major, version.Minor, version.GitVersion, version.GitCommit, version.Platform, version.BuildTime, version.GoVersion, version.Gotags, version.Gogcflags).Set(1)
64return &http.Server{
65Addr: cfg.Addr,
66Handler: mux,
67}
68}
69