-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
persister.go
42 lines (35 loc) · 1.34 KB
/
persister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver"
import (
"context"
"fmt"
"github.com/Azure/azure-event-hubs-go/v3/persist"
jsoniter "github.com/json-iterator/go"
"go.opentelemetry.io/collector/extension/experimental/storage"
)
const (
storageKeyFormat = "%s/%s/%s/%s"
)
type storageCheckpointPersister struct {
storageClient storage.Client
}
func (s *storageCheckpointPersister) Write(namespace, name, consumerGroup, partitionID string, checkpoint persist.Checkpoint) error {
b, err := jsoniter.Marshal(checkpoint)
if err != nil {
return err
}
return s.storageClient.Set(context.Background(), fmt.Sprintf(storageKeyFormat, namespace, name, consumerGroup, partitionID), b)
}
func (s *storageCheckpointPersister) Read(namespace, name, consumerGroup, partitionID string) (persist.Checkpoint, error) {
var checkpoint persist.Checkpoint
bytes, err := s.storageClient.Get(context.Background(), fmt.Sprintf(storageKeyFormat, namespace, name, consumerGroup, partitionID))
if err != nil {
return persist.NewCheckpointFromStartOfStream(), err
}
if len(bytes) == 0 {
return persist.NewCheckpointFromStartOfStream(), err
}
err = jsoniter.Unmarshal(bytes, &checkpoint)
return checkpoint, err
}