Skip to content

Commit

Permalink
feat(BRIDGE-122): added relevant fake server methods to mock observab…
Browse files Browse the repository at this point in the history
…ility service
  • Loading branch information
ElectroNafta committed Aug 27, 2024
1 parent 7a61908 commit 7109637
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
23 changes: 13 additions & 10 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type unsafeBackend struct {
csTicket []string

featureFlags []proton.FeatureToggle

observabilityStatistics ObservabilityStatistics
}

func readBackendRet[T any](b *Backend, f func(b *unsafeBackend) T) T {
Expand Down Expand Up @@ -91,16 +93,17 @@ func New(authLife time.Duration, domain string, enableDedup bool) *Backend {
return &Backend{
domain: domain,
safeBackend: &unsafeBackend{
accounts: make(map[string]*account),
attachments: make(map[string]*attachment),
attData: make(map[string][]byte),
messages: make(map[string]*message),
labels: make(map[string]*label),
updates: make(map[ID]update),
maxUpdatesPerEvent: 0,
srp: make(map[string]*srp.Server),
authLife: authLife,
enableDedup: enableDedup,
accounts: make(map[string]*account),
attachments: make(map[string]*attachment),
attData: make(map[string][]byte),
messages: make(map[string]*message),
labels: make(map[string]*label),
updates: make(map[ID]update),
maxUpdatesPerEvent: 0,
srp: make(map[string]*srp.Server),
authLife: authLife,
enableDedup: enableDedup,
observabilityStatistics: NewObservabilityStatistics(),
},
}
}
Expand Down
32 changes: 32 additions & 0 deletions server/backend/observability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package backend

import (
"time"

"github.com/ProtonMail/go-proton-api"
)

type ObservabilityStatistics struct {
Metrics []proton.ObservabilityMetric
RequestTime []time.Time
}

func NewObservabilityStatistics() ObservabilityStatistics {
return ObservabilityStatistics{
Metrics: make([]proton.ObservabilityMetric, 0),
RequestTime: make([]time.Time, 0),
}
}

func (b *Backend) PushObservabilityMetrics(metrics []proton.ObservabilityMetric) {
writeBackend(b, func(b *unsafeBackend) {
b.observabilityStatistics.Metrics = append(b.observabilityStatistics.Metrics, metrics...)
b.observabilityStatistics.RequestTime = append(b.observabilityStatistics.RequestTime, time.Now())
})
}

func (b *Backend) GetObservabilityStatistics() ObservabilityStatistics {
return readBackendRet(b, func(b *unsafeBackend) ObservabilityStatistics {
return b.observabilityStatistics
})
}
19 changes: 18 additions & 1 deletion server/data.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package server

import (
"net/http"

"github.com/ProtonMail/go-proton-api"
"github.com/gin-gonic/gin"
"net/http"
)

func (s *Server) handlePostDataStats() gin.HandlerFunc {
Expand Down Expand Up @@ -51,3 +52,19 @@ func (s *Server) handlePostDataStatsMultiple() gin.HandlerFunc {
func validateSendStatReq(req *proton.SendStatsReq) bool {
return req.MeasurementGroup != ""
}

func (s *Server) handleObservabilityPost() gin.HandlerFunc {
return func(c *gin.Context) {
var req proton.ObservabilityBatch
if err := c.BindJSON(&req); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

s.b.PushObservabilityMetrics(req.Metrics)

c.JSON(http.StatusOK, gin.H{
"Code": proton.SuccessCode,
})
}
}
2 changes: 2 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func initRouter(s *Server) {
stats.POST("", s.handlePostDataStats())
stats.POST("/multiple", s.handlePostDataStatsMultiple())
}
// Observability endpoint
data.POST("/metrics", s.handleObservabilityPost())
}

// Top level auth routes don't need authentication.
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,7 @@ func (s *Server) PushFeatureFlag(flagName string) {
func (s *Server) DeleteFeatureFlags() {
s.b.DeleteFeatureFlags()
}

func (s *Server) GetObservabilityStatistics() backend.ObservabilityStatistics {
return s.b.GetObservabilityStatistics()
}

0 comments on commit 7109637

Please sign in to comment.