Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add metric for OpenAPI (#1724)

ecmadao81ea53de3a35
diff --git a/metric/metric.go b/metric/metric.go
index 4cc8730212..75c495184f 100644
--- a/metric/metric.go
+++ b/metric/metric.go
@@ -23,6 +23,10 @@ const (
SheetCountMetricName metric.Name = "bb.sheet.count"
// MemberCountMetricName is the metric name for member count
MemberCountMetricName metric.Name = "bb.member.count"
+ // OpenAPIMetricName is the metric name for OpenAPI
+ OpenAPIMetricName metric.Name = "bb.api.call"
+ // SQLAdviseAPIMetricName is the metric name for SQL check API
+ SQLAdviseAPIMetricName metric.Name = "bb.api.sql.advise"
)
// InstanceCountMetric is the API message for bb.instance.count
diff --git a/server/metric_reporter.go b/server/metric_reporter.go
index df85f8909c..32496a525b 100644
--- a/server/metric_reporter.go
+++ b/server/metric_reporter.go
@@ -90,13 +90,7 @@ func (m *MetricReporter) Run(ctx context.Context, wg *sync.WaitGroup) {
}
for _, metric := range metricList {
- if err := m.reporter.Report(metric); err != nil {
- log.Error(
- "Failed to report metric",
- zap.String("metric", string(metric.Name)),
- zap.Error(err),
- )
- }
+ m.report(metric)
}
}
}()
@@ -136,3 +130,13 @@ func (m *MetricReporter) identify() {
log.Debug("reporter identify failed", zap.Error(err))
}
}
+
+func (m *MetricReporter) report(metric *metric.Metric) {
+ if err := m.reporter.Report(metric); err != nil {
+ log.Error(
+ "Failed to report metric",
+ zap.String("metric", string(metric.Name)),
+ zap.Error(err),
+ )
+ }
+}
diff --git a/server/openapi.go b/server/openapi.go
index 1b132b5cb5..afb7e6be1c 100644
--- a/server/openapi.go
+++ b/server/openapi.go
@@ -7,8 +7,10 @@ import (
"strings"
"github.com/bytebase/bytebase/api"
+ metricAPI "github.com/bytebase/bytebase/metric"
"github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/metric"
"github.com/labstack/echo/v4"
)
@@ -66,7 +68,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Failed to find environment %s", envName)).SetInternal(err)
}
if len(envList) != 1 {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Invalid environment %s", envName))
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid environment %s", envName))
}
_, adviceList, err := s.sqlCheck(
@@ -82,5 +84,16 @@ func (s *Server) sqlCheckController(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to run sql check").SetInternal(err)
}
+ if s.MetricReporter != nil {
+ s.MetricReporter.report(&metric.Metric{
+ Name: metricAPI.SQLAdviseAPIMetricName,
+ Value: 1,
+ Labels: map[string]string{
+ "database_type": dbType,
+ "environment": envName,
+ },
+ })
+ }
+
return c.JSON(http.StatusOK, adviceList)
}
diff --git a/server/openapi_metric.go b/server/openapi_metric.go
new file mode 100644
index 0000000000..a7fe966190
--- /dev/null
+++ b/server/openapi_metric.go
@@ -0,0 +1,41 @@
+package server
+
+import (
+ "strconv"
+ "time"
+
+ metricAPI "github.com/bytebase/bytebase/metric"
+ "github.com/bytebase/bytebase/plugin/metric"
+ "github.com/labstack/echo/v4"
+)
+
+func openAPIMetricMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) (err error) {
+ start := time.Now()
+
+ defer func() {
+ duration := time.Since(start)
+ requestMethod := c.Request().Method
+ requestPath := c.Path()
+ responseCode := c.Response().Status
+
+ if s.MetricReporter != nil {
+ s.MetricReporter.report(&metric.Metric{
+ Name: metricAPI.OpenAPIMetricName,
+ Value: 1,
+ Labels: map[string]string{
+ "latency_ns": strconv.FormatInt(duration.Nanoseconds(), 10),
+ "request_method": requestMethod,
+ "request_path": requestPath,
+ "response_code": strconv.Itoa(responseCode),
+ },
+ })
+ }
+ }()
+
+ if err = next(c); err != nil {
+ c.Error(err)
+ }
+ return
+ }
+}
diff --git a/server/server.go b/server/server.go
index 7b6732d451..195744d922 100644
--- a/server/server.go
+++ b/server/server.go
@@ -292,6 +292,9 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
apiGroup := e.Group("/api")
openAPIGroup := e.Group(openAPIPrefix)
+ openAPIGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
+ return openAPIMetricMiddleware(s, next)
+ })
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return JWTMiddleware(s.store, next, prof.Mode, config.secret)