diff --git a/config/configs.go b/config/configs.go index b7ad2d1b278459df7b572ccaee34a0945c29dbb0..2836c0a7b195d769ee1c47e0264dd79ac7de2210 100644 --- a/config/configs.go +++ b/config/configs.go @@ -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,18 @@ 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 { diff --git a/server/server.go b/server/server.go index 044a242fcfae533cb3e0158d5368abd834d44663..5b5e8407c18faa2cec8a807bfee5ce32d75b1bf4 100644 --- a/server/server.go +++ b/server/server.go @@ -18,13 +18,17 @@ 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" + + "code.vereign.com/code/viam-apis/errors" "code.vereign.com/code/viam-apis/log" @@ -41,6 +45,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 +119,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