From 3a2851156854aaefc092b51112853cac7ef0f53c Mon Sep 17 00:00:00 2001 From: Trajan0x Date: Thu, 27 Jun 2024 11:28:32 -0400 Subject: [PATCH] first pass --- agents/agents/executor/api/server.go | 2 +- agents/agents/guard/api/server.go | 2 +- agents/agents/notary/api/server.go | 2 +- contrib/promexporter/exporters/exporter.go | 2 +- .../internal/gql/explorer/contrib/main.go | 2 +- .../internal/gql/util/server_test.go | 2 +- contrib/screener-api/screener/screener.go | 2 +- core/ginhelper/server.go | 27 ++++++++++++++++--- core/metrics/base.go | 4 +++ core/metrics/metrics.go | 6 +++++ core/metrics/null.go | 5 ++++ 11 files changed, 46 insertions(+), 10 deletions(-) diff --git a/agents/agents/executor/api/server.go b/agents/agents/executor/api/server.go index 17dc544dc0..bb2b0769eb 100644 --- a/agents/agents/executor/api/server.go +++ b/agents/agents/executor/api/server.go @@ -13,7 +13,7 @@ var logger = log.Logger("executor-api") // Start starts the api server. func Start(ctx context.Context, metricsPort uint16) error { - router := ginhelper.New(logger) + router := ginhelper.New(logger, ginhelper.EmptyHandler("executor-api")) g, ctx := errgroup.WithContext(ctx) diff --git a/agents/agents/guard/api/server.go b/agents/agents/guard/api/server.go index a7caa95156..663554cf5f 100644 --- a/agents/agents/guard/api/server.go +++ b/agents/agents/guard/api/server.go @@ -13,7 +13,7 @@ var logger = log.Logger("guard-api") // Start starts the api server. func Start(ctx context.Context, metricsPort uint16) error { - router := ginhelper.New(logger) + router := ginhelper.New(logger, ginhelper.EmptyHandler("guard-api")) g, ctx := errgroup.WithContext(ctx) diff --git a/agents/agents/notary/api/server.go b/agents/agents/notary/api/server.go index 3fd2b15fc1..1903333ee2 100644 --- a/agents/agents/notary/api/server.go +++ b/agents/agents/notary/api/server.go @@ -13,7 +13,7 @@ var logger = log.Logger("notary-api") // Start starts the api server. func Start(ctx context.Context, metricsPort uint16) error { - router := ginhelper.New(logger) + router := ginhelper.New(logger, ginhelper.EmptyHandler("notary-api")) g, ctx := errgroup.WithContext(ctx) diff --git a/contrib/promexporter/exporters/exporter.go b/contrib/promexporter/exporters/exporter.go index c645dffea5..dc3aaf81de 100644 --- a/contrib/promexporter/exporters/exporter.go +++ b/contrib/promexporter/exporters/exporter.go @@ -59,7 +59,7 @@ func StartExporterServer(ctx context.Context, handler metrics.Handler, cfg confi // the main server serves metrics since this is only a prom exporter _ = os.Setenv(metrics.MetricsPortEnabledEnv, "false") - router := ginhelper.New(logger) + router := ginhelper.New(logger, handler) router.Use(handler.Gin()) router.GET(metrics.MetricsPathDefault, gin.WrapH(handler.Handler())) diff --git a/contrib/promexporter/internal/gql/explorer/contrib/main.go b/contrib/promexporter/internal/gql/explorer/contrib/main.go index dc7174f63c..106e7b2864 100644 --- a/contrib/promexporter/internal/gql/explorer/contrib/main.go +++ b/contrib/promexporter/internal/gql/explorer/contrib/main.go @@ -33,7 +33,7 @@ func main() { defer cancel() // prepare the server - router := ginhelper.New(logger) + router := ginhelper.New(logger, ginhelper.EmptyHandler("explorer-api")) nullHandler, err := metrics.NewByType(ctx, metadata.BuildInfo(), metrics.Null) if err != nil { diff --git a/contrib/promexporter/internal/gql/util/server_test.go b/contrib/promexporter/internal/gql/util/server_test.go index 8999fbf2e0..6649c1c583 100644 --- a/contrib/promexporter/internal/gql/util/server_test.go +++ b/contrib/promexporter/internal/gql/util/server_test.go @@ -45,7 +45,7 @@ func (t *UtilSuite) TestWaitForStartSucceed() { tmpPort, err := freeport.GetFreePort() t.Require().NoError(err) - router := ginhelper.New(testLogger) + router := ginhelper.New(testLogger, ginhelper.EmptyHandler("test")) // start a server go func() { diff --git a/contrib/screener-api/screener/screener.go b/contrib/screener-api/screener/screener.go index 55e06614a6..7921527b39 100644 --- a/contrib/screener-api/screener/screener.go +++ b/contrib/screener-api/screener/screener.go @@ -85,7 +85,7 @@ func NewScreener(ctx context.Context, cfg config.Config, metricHandler metrics.H return nil, fmt.Errorf("could not connect to rules db: %w", err) } - screener.router = ginhelper.New(logger) + screener.router = ginhelper.New(logger, metricHandler.Gin()) screener.router.Use(screener.metrics.Gin()) // Blacklist route diff --git a/core/ginhelper/server.go b/core/ginhelper/server.go index f9c3b5c725..fd09a3ccb2 100644 --- a/core/ginhelper/server.go +++ b/core/ginhelper/server.go @@ -23,6 +23,27 @@ import ( //go:embed robots.txt var robots []byte +// IMetricsHandler is an interface for metrics handlers. +type IMetricsHandler interface { + // Name returns the name of the service + Name() string +} + +// EmptyHandler returns an empty service. +func EmptyHandler(serviceName string) IMetricsHandler { + return emptyService{name: serviceName} +} + +type emptyService struct { + name string +} + +func (m emptyService) Name() string { + return "metrics-server" +} + +var _ IMetricsHandler = (*emptyService)(nil) + // New creates a new gin server with some sensible defaults. // these include: // - helmet-default handlers @@ -30,7 +51,7 @@ var robots []byte // - cors (used for requests from the frontend) // - health-checks // - restrictive robots.txt. -func New(logger *log.ZapEventLogger) *gin.Engine { +func New(logger *log.ZapEventLogger, handler IMetricsHandler) *gin.Engine { server := newBase() server.Use(ginzap.RecoveryWithZap(logger.Desugar(), true)) @@ -92,7 +113,7 @@ func newBase() *gin.Engine { server.Use(cors.New(cors.Config{ AllowAllOrigins: true, AllowHeaders: []string{"*"}, - AllowMethods: []string{"GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"}, + AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodPatch, http.MethodDelete, http.MethodOptions}, MaxAge: 12 * time.Hour, })) @@ -111,7 +132,7 @@ func newBase() *gin.Engine { }) server.GET(HealthCheck, func(c *gin.Context) { - c.JSON(200, gin.H{ + c.JSON(http.StatusOK, gin.H{ "status": "UP", }) }) diff --git a/core/metrics/base.go b/core/metrics/base.go index 91d2cadae2..27ed9f08a0 100644 --- a/core/metrics/base.go +++ b/core/metrics/base.go @@ -48,6 +48,10 @@ type baseHandler struct { experimentalLogger experimentalLogger.ExperimentalLogger } +func (b *baseHandler) Name() string { + return b.name +} + func (b *baseHandler) Handler() http.Handler { return b.handler } diff --git a/core/metrics/metrics.go b/core/metrics/metrics.go index 6f030985d7..c6ab506179 100644 --- a/core/metrics/metrics.go +++ b/core/metrics/metrics.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/synapsecns/sanguine/core/config" + "github.com/synapsecns/sanguine/core/ginhelper" experimentalLogger "github.com/synapsecns/sanguine/core/metrics/logger" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel/metric" @@ -43,8 +44,13 @@ type Handler interface { Handler() http.Handler // ExperimentalLogger returns an experimental logger. ExperimentalLogger() experimentalLogger.ExperimentalLogger + // Name returns the name of the service + Name() string } +// Static check +var _ ginhelper.IMetricsHandler = (*Handler)(nil) + // HandlerType is the handler type to use // //go:generate go run golang.org/x/tools/cmd/stringer -type=HandlerType -linecomment diff --git a/core/metrics/null.go b/core/metrics/null.go index e79ecd4330..eefcbe6ec9 100644 --- a/core/metrics/null.go +++ b/core/metrics/null.go @@ -19,6 +19,7 @@ type nullHandler struct { tracer trace.Tracer propagator nullPropogator meter Meter + name string } func (n nullHandler) ExperimentalLogger() experimentalLogger.ExperimentalLogger { @@ -63,6 +64,10 @@ func (n nullHandler) Gin() gin.HandlerFunc { } } +func (n nullHandler) Name() string { + return "" +} + func (n nullHandler) Start(_ context.Context) error { return nil }