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
...@@ -37,6 +37,9 @@ var EntitiesManagerUrl string ...@@ -37,6 +37,9 @@ var EntitiesManagerUrl string
var CertDir string var CertDir string
var GlobalLogLevel string var GlobalLogLevel string
var PrometeusListenAddress string
var MetricEnvPrefix string
func SetConfigValues(configFile, etcdURL string) { func SetConfigValues(configFile, etcdURL string) {
// Set Default Values For Config Variables // Set Default Values For Config Variables
...@@ -141,12 +144,17 @@ func SetConfigValues(configFile, etcdURL string) { ...@@ -141,12 +144,17 @@ func SetConfigValues(configFile, etcdURL string) {
MaxMessageSize = viper.GetInt("maxMessageSize") MaxMessageSize = viper.GetInt("maxMessageSize")
PrometeusListenAddress = viper.GetString("prometeusListenAddress")
MetricEnvPrefix = viper.GetString("metricEnvPrefix")
GlobalLogLevel = viper.GetString("globalLogLevel")
CertificatePEM = GetCertificatePEM() CertificatePEM = GetCertificatePEM()
PrivateKeyPEM = GetPrivateKeyPEM() PrivateKeyPEM = GetPrivateKeyPEM()
CaCertificatePEM = GetCaCertificatePEM() CaCertificatePEM = GetCaCertificatePEM()
VereignCaCertificatePEM = GetVereignCaCertificatePEM() VereignCaCertificatePEM = GetVereignCaCertificatePEM()
VereignCaKeyPEM = GetVereignCaKeyPEM() VereignCaKeyPEM = GetVereignCaKeyPEM()
GlobalLogLevel = viper.GetString("globalLogLevel")
} }
func GetCertificatePEM() []byte { func GetCertificatePEM() []byte {
......
...@@ -70,6 +70,13 @@ func main() { ...@@ -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 // infinite loop
log.Printf("Entering infinite loop") log.Printf("Entering infinite loop")
select {} select {}
......
...@@ -18,13 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -18,13 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import ( import (
"code.vereign.com/code/viam-apis/errors"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"strings" "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" "code.vereign.com/code/viam-apis/log"
...@@ -41,6 +46,9 @@ import ( ...@@ -41,6 +46,9 @@ import (
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
var mutex sync.RWMutex
var summaries = make(map[string]prometheus.Summary)
// private type for Context keys // private type for Context keys
type contextKey int type contextKey int
...@@ -112,6 +120,40 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ ...@@ -112,6 +120,40 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ
return res, err 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 { func StartGRPCServer(address string, certPEM, privateKeyPEM, caCertPEM, vereignCertPEM, vereignPrivateKeyPEM []byte, dataStorageAddress string, maxMessageSize int) error {
pkgCertPEM = certPEM pkgCertPEM = certPEM
pkgKeyPEM = privateKeyPEM pkgKeyPEM = privateKeyPEM
...@@ -217,3 +259,19 @@ func StartRESTServer(address, grpcAddress string, certPEM []byte) error { ...@@ -217,3 +259,19 @@ func StartRESTServer(address, grpcAddress string, certPEM []byte) error {
return nil 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.
Please register or to comment