Andrea Maria Piana 9a59d6a459 feat(metrics)_: add centralized metrics
This commit adds support for centralized metrics.
There are two providers as of now, and we haven't quite decided which
one to go for, so for the time being both are supported.
It also introduces a new endpoint InitializeApplication that replaces
OpenAccounts
2024-07-11 10:05:31 +01:00

49 lines
1.0 KiB
Go

package common
import (
"errors"
"github.com/google/uuid"
)
var (
ErrInvalidEventName = errors.New("centralized-metric: invalid-event-name")
ErrInvalidPlatform = errors.New("centralized-metric: invalid-platform")
ErrInvalidVersion = errors.New("centralized-metric: invalid-version")
)
type Metric struct {
ID string `json:"id"`
UserID string `json:"userId"`
EventName string `json:"eventName"`
EventValue map[string]any `json:"eventValue"`
Timestamp int64 `json:"timestamp"`
Platform string `json:"platform"`
AppVersion string `json:"appVersion"`
}
type MetricProcessor interface {
Process(metrics []Metric) error
}
func (m *Metric) Validate() error {
if len(m.EventName) == 0 {
return ErrInvalidEventName
}
if len(m.Platform) == 0 {
return ErrInvalidPlatform
}
if len(m.AppVersion) == 0 {
return ErrInvalidVersion
}
return nil
}
func (m *Metric) EnsureID() {
if len(m.ID) != 0 {
return
}
m.ID = uuid.New().String()
}