Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Jun 27, 2024
1 parent 8d04213 commit 3a28511
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion agents/agents/executor/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion agents/agents/guard/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion agents/agents/notary/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion contrib/promexporter/exporters/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
2 changes: 1 addition & 1 deletion contrib/promexporter/internal/gql/explorer/contrib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion contrib/promexporter/internal/gql/util/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions core/ginhelper/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,35 @@ 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
// - request-ids (used for stack tracing)
// - 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))
Expand Down Expand Up @@ -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,
}))

Expand All @@ -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",
})
})
Expand Down
4 changes: 4 additions & 0 deletions core/metrics/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions core/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions core/metrics/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type nullHandler struct {
tracer trace.Tracer
propagator nullPropogator
meter Meter
name string
}

func (n nullHandler) ExperimentalLogger() experimentalLogger.ExperimentalLogger {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 3a28511

Please sign in to comment.