diff --git a/internal/transport/middleware/permission_middleware.go b/internal/transport/middleware/permission_middleware.go index e8a2d3f..1f06861 100644 --- a/internal/transport/middleware/permission_middleware.go +++ b/internal/transport/middleware/permission_middleware.go @@ -3,9 +3,9 @@ package middleware import ( "cybertron/constants" "cybertron/service" - "net/http" - "github.com/gin-gonic/gin" + "net/http" + "strings" ) type UserInfo struct { @@ -27,6 +27,18 @@ type UserInfo struct { func PermissionMiddleware(authService *service.AuthService) gin.HandlerFunc { return func(c *gin.Context) { + excludedPublicUrls := []string{ + "/envelope", + } + + for _, url := range excludedPublicUrls { + if strings.Contains(c.Request.RequestURI, url) { + c.Next() + return + } + } + + //println("%s", path.Base(c.Request.URL.Path)) sessionToken := c.GetHeader(constants.SESSION_HEADER_NAME) userEmail := c.GetHeader(constants.EMAIL_HEADER_NAME) diff --git a/internal/transport/server.go b/internal/transport/server.go index 469c0f2..b7ddebc 100644 --- a/internal/transport/server.go +++ b/internal/transport/server.go @@ -1,7 +1,9 @@ package transport import ( + "cybertron/internal/transport/middleware" "cybertron/internal/transport/router" + "cybertron/pkg/metrics" "fmt" "github.com/gin-contrib/cors" "os" @@ -48,8 +50,10 @@ func (s *Server) Start() { AllowCredentials: true, MaxAge: 24 * time.Hour, })) + s.gin.Use(middleware.MetricMiddleware()) //s.gin.Use(middleware.PermissionMiddleware(s.dependencies.Service.AuthService)) s.router() + metrics.AdminHandler() port := configs.GetPort() s.dependencies.Logger.Info("Starting server", zap.Int("port", port)) diff --git a/models/instrumentation/performance_metrics.go b/models/instrumentation/performance_metrics.go index 333752f..4637675 100644 --- a/models/instrumentation/performance_metrics.go +++ b/models/instrumentation/performance_metrics.go @@ -28,6 +28,10 @@ type ClientHttpCallMetric struct { DurationInMs int64 `json:"duration_in_ms,omitempty"` } +type ErrorConsumptionMetric struct { + ProjectId string `json:"project_id,omitempty"` +} + type MetricAttributes struct { ApiMetric ApiMetric ClientHttpCallMetric ClientHttpCallMetric diff --git a/pkg/metrics/metric_publisher.go b/pkg/metrics/metric_publisher.go index 75f4c0f..5eb3968 100644 --- a/pkg/metrics/metric_publisher.go +++ b/pkg/metrics/metric_publisher.go @@ -36,6 +36,10 @@ func publishApiMetric(apiMetrics instrumentation.ApiMetric) { ApiRequestLatencyHistogram.WithLabelValues(apiMetrics.Url, status).Observe(duration) } +func PublishErrorConsumptionMetric(errorConsumptionMetrics instrumentation.ErrorConsumptionMetric) { + ErrorConsumerCounter.WithLabelValues(errorConsumptionMetrics.ProjectId).Inc() +} + func publishClientHttpCallMetric(clientHttpCallMetric instrumentation.ClientHttpCallMetric) { status := strconv.Itoa(clientHttpCallMetric.ResponseCode) duration := float64(clientHttpCallMetric.DurationInMs) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 9b86372..4b0a09c 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -15,6 +15,13 @@ var ApiRequestCounter = promauto.NewCounterVec( []string{"url", "response_code"}, ) +var ErrorConsumerCounter = promauto.NewCounterVec( + prometheus.CounterOpts{ + Name: "cybertron_error_consumption_counter_total", + Help: "counter for number of error consumed by cybertron", + }, + []string{"project_id"}, +) var ApiRequestLatencyHistogram = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "cybertron_api_request_latency_histogram", diff --git a/scripts/run-local b/scripts/run-local index 22fa932..34463d9 100644 --- a/scripts/run-local +++ b/scripts/run-local @@ -9,3 +9,7 @@ while read -r line; do echo "Building cybertron locally" eval "make build-cybertron $ENVS out/cybertron" + + + + diff --git a/service/ExceptionService.go b/service/ExceptionService.go index 0262f32..a97bb45 100644 --- a/service/ExceptionService.go +++ b/service/ExceptionService.go @@ -2,9 +2,11 @@ package service import ( "bufio" + "cybertron/models/instrumentation" "cybertron/pkg/encoder" "cybertron/pkg/kafka/producer" "cybertron/pkg/log" + "cybertron/pkg/metrics" "encoding/json" "fmt" "github.com/gin-gonic/gin" @@ -37,7 +39,7 @@ type ExceptionValue struct { ProjectId string `json:"project_id,omitempty"` ReleaseId string `json:"release_id,omitempty"` Breadcrumbs interface{} `json:"breadcrumbs,omitempty"` - Extras interface{} `json:"extras,omitempty"` + Extra interface{} `json:"extra,omitempty"` Request interface{} `json:"request,omitempty"` } @@ -63,6 +65,8 @@ func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) { scanner := bufio.NewScanner(c.Request.Body) var lines []string + //error metric firing + for scanner.Scan() { lines = append(lines, scanner.Text()) } @@ -76,12 +80,14 @@ func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) { return } + metrics.PublishErrorConsumptionMetric(instrumentation.ErrorConsumptionMetric{ProjectId: projectID}) + for _, errorItem := range jsonData.Exception.Values { errorItem.ProjectId = projectID //todo update release id errorItem.ReleaseId = "release-1" errorItem.Breadcrumbs = jsonData.Breadcrumbs - errorItem.Extras = jsonData.Extra + errorItem.Extra = jsonData.Extra errorItem.Request = jsonData.Request err := exceptionService.kafkaProducer.PublishEvent(errorItem, "kafka-stream", "", nil, encoder.JsonEncoderInstance) diff --git a/service/searchService.go b/service/searchService.go index 29b3e03..03a9862 100644 --- a/service/searchService.go +++ b/service/searchService.go @@ -69,6 +69,7 @@ func (s *SearchService) GetErrorDetails(c *gin.Context) { if err != nil { utils.ErrorResponse(c, "Failed to search please try again later") + return } c.JSON(http.StatusOK, gin.H{ "results": response,