Skip to content
Snippets Groups Projects
Commit a7aa86cc authored by Gospodin Bodurov's avatar Gospodin Bodurov
Browse files

Merge branch 'prometheus-integration' into 'master'

Prometheus integration

See merge request !62
parents 90e38c53 2a306e1c
No related branches found
No related tags found
1 merge request!62Prometheus integration
Pipeline #32111 failed with stages
in 2 seconds
......@@ -37,6 +37,9 @@ var EntitiesManagerUrl string
var CertDir string
var GlobalLogLevel string
var PrometeusListenAddress string
var MetricEnvPrefix string
func SetConfigValues(configFile, etcdURL string) {
// Set Default Values For Config Variables
......@@ -141,12 +144,17 @@ func SetConfigValues(configFile, etcdURL string) {
MaxMessageSize = viper.GetInt("maxMessageSize")
PrometeusListenAddress = viper.GetString("prometeusListenAddress")
MetricEnvPrefix = viper.GetString("metricEnvPrefix")
GlobalLogLevel = viper.GetString("globalLogLevel")
CertificatePEM = GetCertificatePEM()
PrivateKeyPEM = GetPrivateKeyPEM()
CaCertificatePEM = GetCaCertificatePEM()
VereignCaCertificatePEM = GetVereignCaCertificatePEM()
VereignCaKeyPEM = GetVereignCaKeyPEM()
GlobalLogLevel = viper.GetString("globalLogLevel")
}
func GetCertificatePEM() []byte {
......
......@@ -70,6 +70,13 @@ func main() {
}
}()
go func() {
err := server.StartPrometheusServer()
if err != nil {
log.Printf("failed to start prometheus server: %v", err)
}
}()
// infinite loop
log.Printf("Entering infinite loop")
select {}
......
......@@ -18,13 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"code.vereign.com/code/viam-apis/errors"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"code.vereign.com/code/viam-apis/errors"
"code.vereign.com/code/viam-apis/log"
......@@ -41,6 +46,9 @@ import (
"google.golang.org/grpc/metadata"
)
var mutex sync.RWMutex
var summaries = make(map[string]prometheus.Summary)
// private type for Context keys
type contextKey int
......@@ -112,6 +120,40 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ
return res, err
}
func createQueryTime(funcName string) prometheus.Summary {
if config.PrometeusListenAddress != "" {
endPointName := strings.Replace(funcName, "/", "_", -1)
endPointName = strings.Replace(endPointName, ".", "_", -1)
metricName := endPointName + "_grpc_request_duration_seconds"
if config.MetricEnvPrefix != "" {
metricName = config.MetricEnvPrefix + "_" + metricName
}
mutex.Lock()
defer mutex.Unlock()
queryTime, ok := summaries[metricName]
if ok == false {
queryTime = prometheus.NewSummary(prometheus.SummaryOpts{
Name: metricName,
Help: "grpc request duration seconds of /" + funcName + " for " + config.MetricEnvPrefix + " env",
})
// init metrics
prometheus.MustRegister(queryTime)
summaries[metricName] = queryTime
}
return queryTime
}
return nil
}
func StartGRPCServer(address string, certPEM, privateKeyPEM, caCertPEM, vereignCertPEM, vereignPrivateKeyPEM []byte, dataStorageAddress string, maxMessageSize int) error {
pkgCertPEM = certPEM
pkgKeyPEM = privateKeyPEM
......@@ -217,3 +259,19 @@ func StartRESTServer(address, grpcAddress string, certPEM []byte) error {
return nil
}
func StartPrometheusServer() error {
if config.PrometeusListenAddress != "" {
// start prometheus
promHandler := http.NewServeMux()
promHandler.Handle("/metrics", promhttp.Handler())
log.Println("Starting prometheus...")
err := http.ListenAndServe(config.PrometeusListenAddress, promHandler)
if err != nil {
return err
}
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment