metrics
This commit is contained in:
+29
-7
@@ -12,6 +12,12 @@ import (
|
||||
|
||||
type StateStorage struct {
|
||||
registry MutationRegistry
|
||||
// metrics is set by the owning DiskStorage via SetMetrics. Nil is fine —
|
||||
// every method that touches it nil-checks first. The pointer is
|
||||
// intentionally unexported: callers set it through DiskStorage.SetMetrics
|
||||
// so the embedded StateStorage always tracks the parent disk storage's
|
||||
// metrics, even if the embedded value is replaced.
|
||||
metrics *Metrics
|
||||
}
|
||||
|
||||
type StorageEvent struct {
|
||||
@@ -50,10 +56,20 @@ func (s *StateStorage) Load(r io.Reader, onMessage func(msg proto.Message, timeS
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *StateStorage) Append(io io.Writer, mutation proto.Message, timeStamp time.Time) error {
|
||||
// Append serialises a mutation as a StorageEvent and writes it (plus a
|
||||
// trailing newline) to io. The first return value is the number of
|
||||
// bytes written, which the caller (DiskStorage) adds to the
|
||||
// event_log_bytes_written_total counter; the second is the underlying
|
||||
// write error, if any. ErrUnknownType is returned (with 0 bytes) when
|
||||
// the mutation's type is not registered, and is also counted in
|
||||
// event_log_unknown_types_total.
|
||||
func (s *StateStorage) Append(io io.Writer, mutation proto.Message, timeStamp time.Time) (int, error) {
|
||||
typeName, ok := s.registry.GetTypeName(mutation)
|
||||
if !ok {
|
||||
return ErrUnknownType
|
||||
if s.metrics != nil {
|
||||
s.metrics.EventLogUnknownTypes.Inc()
|
||||
}
|
||||
return 0, ErrUnknownType
|
||||
}
|
||||
event := &StorageEvent{
|
||||
Type: typeName,
|
||||
@@ -62,13 +78,16 @@ func (s *StateStorage) Append(io io.Writer, mutation proto.Message, timeStamp ti
|
||||
}
|
||||
jsonBytes, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
if _, err := io.Write(jsonBytes); err != nil {
|
||||
return err
|
||||
n, err := io.Write(jsonBytes)
|
||||
total := n
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
io.Write([]byte("\n"))
|
||||
return nil
|
||||
n, err = io.Write([]byte("\n"))
|
||||
total += n
|
||||
return total, err
|
||||
}
|
||||
|
||||
func (s *StateStorage) Read(r *bufio.Scanner) (*StorageEvent, error) {
|
||||
@@ -83,6 +102,9 @@ func (s *StateStorage) Read(r *bufio.Scanner) (*StorageEvent, error) {
|
||||
typeName := event.Type
|
||||
mutation, ok := s.registry.Create(typeName)
|
||||
if !ok {
|
||||
if s.metrics != nil {
|
||||
s.metrics.EventLogUnknownTypes.Inc()
|
||||
}
|
||||
return nil, ErrUnknownType
|
||||
}
|
||||
if err := json.Unmarshal(event.Mutation, mutation); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user