Skip to content

Commit

Permalink
Run the guacgql HTTP server on only one port (#1805)
Browse files Browse the repository at this point in the history
- Previously guacgql would run http.DefaultServeMux on
  two separate ports. One of them would conflict with
  the port used by Prometheus for guaccollect deps_dev.
  (The second port was used to provide Prometheus's
  /metrics route, but this was already present on the
  initial port, and it still is.)
- Fixes #1676

Signed-off-by: Narsimham Chelluri (Narsa) <narsa@kusari.dev>
  • Loading branch information
nchelluri committed Apr 3, 2024
1 parent 2dc06e2 commit ef4658e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 34 deletions.
4 changes: 2 additions & 2 deletions cmd/guaccollect/cmd/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ you have access to read and write to the respective blob store.`,
viper.GetBool("retrieve-dependencies"),
args,
viper.GetBool("enable-prometheus"),
viper.GetInt("prometheus-addr"),
viper.GetInt("prometheus-port"),
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
Expand Down Expand Up @@ -159,7 +159,7 @@ func validateDepsDevFlags(pubsubAddr string, blobAddr string, csubAddr string, c
}

func init() {
set, err := cli.BuildFlags([]string{"retrieve-dependencies"})
set, err := cli.BuildFlags([]string{"retrieve-dependencies", "prometheus-port"})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/guaccollect/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
func init() {
cobra.OnInitialize(cli.InitConfig)

set, err := cli.BuildFlags([]string{"pubsub-addr", "blob-addr", "csub-addr", "use-csub", "service-poll", "enable-prometheus", "prometheus-addr"})
set, err := cli.BuildFlags([]string{"pubsub-addr", "blob-addr", "csub-addr", "use-csub", "service-poll", "enable-prometheus"})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/guacgql/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,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", "enable-prometheus", "prometheus-addr",
"kv-store", "kv-redis", "kv-tikv", "enable-prometheus",
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
Expand Down
34 changes: 6 additions & 28 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
Expand Down Expand Up @@ -71,39 +70,18 @@ func InitConfig() {
}
}

// SetupPrometheus sets up the prometheus server
// SetupPrometheus sets up the Prometheus server, registering its handler on http.DefaultServeMux
func SetupPrometheus(ctx context.Context, logger *zap.SugaredLogger, name string) (metrics.MetricCollector, error) {
if name == "" {
return nil, 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, 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()
if name == "" {
return nil, errors.New("name cannot be empty")
}

// 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)
}
}()
m := metrics.FromContext(ctx, name)
http.Handle("/metrics", m.MetricsHandler())
return m, nil
}
2 changes: 1 addition & 1 deletion pkg/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func init() {

set.Bool("enable-prometheus", true, "enable prometheus metrics")

set.Int("prometheus-addr", 9091, "port to listen to on prometheus server")
set.Int("prometheus-port", 9091, "port to listen to on prometheus server")

set.StringP("interval", "i", "5m", "if polling set interval, m, h, s, etc.")

Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ To use this metrics package in your application, you need to do the following:
To scrape the metrics from Prometheus, you can use the following example:

```bash
curl http://localhost:9091/metrics
curl http://localhost:8080/metrics
```

This will return a plain text page with a series of lines like this:
Expand Down

0 comments on commit ef4658e

Please sign in to comment.