1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
// Package metrics provides a metric logging wrappers for Prometheus server.
package metrics
import (
"context"
"errors"
"os"
"strconv"
"time"
)
var (
// ErrMetricServiceAlreadyRunning Generated when we call Start and the metric service is already running
ErrMetricServiceAlreadyRunning = errors.New("MetricService is already running")
// ErrMetricServiceNotRunning is not currently running
ErrMetricServiceNotRunning = errors.New("MetricService not running")
// ErrMetricUnableToRegister unable to register
ErrMetricUnableToRegister = errors.New("Unable to register metric")
)
var (
// the duration of which we'll keep a metric in-memory and keep reporting it.
// when a metric time expires, it would get removed.
// TODO: implement or remove
maxMetricRetensionDuration = time.Duration(5) * time.Minute
)
// MakeMetricService creates a new metrics server at the given endpoint.
func MakeMetricService(config *ServiceConfig) *MetricService {
server := &MetricService{
config: *config,
done: make(chan struct{}, 1),
}
if _, hasPid := server.config.Labels["pid"]; !hasPid {
pid := os.Getpid()
server.config.Labels["pid"] = strconv.FormatInt(int64(pid), 10)
}
if _, hasHost := server.config.Labels["host"]; !hasHost {
if hostname, err := os.Hostname(); err == nil && len(hostname) > 0 {
server.config.Labels["host"] = hostname
}
}
return server
}
func (server *MetricService) startAsync(ctx context.Context) {
defer close(server.done)
metricsReporter := MakeMetricReporter(server.config)
metricsReporter.ReporterLoop(ctx)
}
// Start starts the metric server
func (server *MetricService) Start(ctx context.Context) error {
server.runningMu.Lock()
defer server.runningMu.Unlock()
if server.running {
return ErrMetricServiceAlreadyRunning
}
var runContext context.Context
runContext, server.cancel = context.WithCancel(ctx)
go server.startAsync(runContext)
server.running = true
return nil
}
// Shutdown the running server
func (server *MetricService) Shutdown() error {
// check if the service is running.
server.runningMu.Lock()
defer server.runningMu.Unlock()
if !server.running {
return ErrMetricServiceNotRunning
}
server.cancel()
server.cancel = nil
<-server.done
server.running = false
return nil
}
|