Skip to content

Commit

Permalink
Included prometheus server for guacql (#1635)
Browse files Browse the repository at this point in the history
- Included prometheus for guacql
- Will include in all other
  #1500 (comment) if
  this is acceptable.

Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan committed Jan 26, 2024
1 parent b1c67c9 commit de3cd11
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
3 changes: 1 addition & 2 deletions cmd/guacgql/cmd/root.go
Expand Up @@ -100,7 +100,6 @@ var rootCmd = &cobra.Command{
flags.kvStore = viper.GetString("kv-store")
flags.kvRedis = viper.GetString("kv-redis")
flags.kvTiKV = viper.GetString("kv-tikv")

startServer(cmd)
},
}
Expand All @@ -114,7 +113,7 @@ func init() {
"neptune-endpoint", "neptune-port", "neptune-region", "neptune-user", "neptune-realm",
"gql-listen-port", "gql-tls-cert-file", "gql-tls-key-file", "gql-debug", "gql-backend", "gql-trace",
"db-address", "db-driver", "db-debug", "db-migrate",
"kv-store", "kv-redis", "kv-tikv",
"kv-store", "kv-redis", "kv-tikv", "enable-prometheus", "prometheus-addr",
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
Expand Down
6 changes: 6 additions & 0 deletions cmd/guacgql/cmd/server.go
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/guacsec/guac/pkg/assembler/graphql/resolvers"
"github.com/guacsec/guac/pkg/assembler/kv"
"github.com/guacsec/guac/pkg/assembler/kv/redis"
"github.com/guacsec/guac/pkg/cli"
"github.com/guacsec/guac/pkg/logging"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -96,6 +97,11 @@ func startServer(cmd *cobra.Command) {
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
logger.Infof("connect to %s://localhost:%d/ for GraphQL playground", proto, flags.port)
}
// Setup Prometheus metrics handler
err = cli.SetupPrometheus(ctx, logger, "guacgql")
if err != nil {
logger.Fatalf("Error setting up Prometheus: %v", err)
}

server := &http.Server{Addr: fmt.Sprintf(":%d", flags.port)}
logger.Info("starting server")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -104,7 +104,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v24.0.4+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
Expand Down Expand Up @@ -179,7 +179,7 @@ require (
github.com/onsi/ginkgo/v2 v2.13.1 // indirect
github.com/onsi/gomega v1.29.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/owenrumney/go-sarif/v2 v2.3.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Expand Up @@ -202,8 +202,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
Expand Down Expand Up @@ -551,8 +551,8 @@ github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc3 h1:fzg1mXZFj8YdPeNkRXMg+zb88BFV0Ys52cJydRwBkb8=
github.com/opencontainers/image-spec v1.1.0-rc3/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
Expand Down
44 changes: 42 additions & 2 deletions pkg/cli/init.go
Expand Up @@ -17,18 +17,21 @@ package cli

import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/guacsec/guac/pkg/logging"

"github.com/guacsec/guac/pkg/metrics"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"go.uber.org/zap"
)

func InitConfig() {

home, err := homedir.Dir()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err)
Expand Down Expand Up @@ -67,3 +70,40 @@ func InitConfig() {
logger.Infof("Using config file: %s", viper.ConfigFileUsed())
}
}

// SetupPrometheus sets up the prometheus server
func SetupPrometheus(ctx context.Context, logger *zap.SugaredLogger, name string) error {
if name == "" {
return errors.New("name cannot be empty")
}
m := metrics.FromContext(ctx, name)
enablePrometheus := viper.GetBool("enable-prometheus")
prometheusPort := viper.GetInt("prometheus-addr")
if !enablePrometheus {
return nil
}

go func() {
http.Handle("/metrics", m.MetricsHandler())
logger.Infof("Prometheus server is listening on: %d", prometheusPort)
server := &http.Server{Addr: fmt.Sprintf(":%d", prometheusPort)}

// Start server in a goroutine so that it doesn't block
go func() {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("Error starting HTTP server: %v", err)
}
}()

// Listen for the cancellation signal
<-ctx.Done()

// Shutdown the server when cancellation signal is received
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Errorf("Error shutting down server: %v", err)
}
}()
return nil
}

0 comments on commit de3cd11

Please sign in to comment.