Skip to content

Commit 471361a

Browse files
authored
[chore] Fix small nits in persistent queue metadata proto (#13125)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent c4c0814 commit 471361a

File tree

5 files changed

+166
-75
lines changed

5 files changed

+166
-75
lines changed

exporter/exporterhelper/internal/queuebatch/internal/persistentqueue/meta.pb.go

Lines changed: 102 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/exporterhelper/internal/queuebatch/internal/persistentqueue/meta.proto

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ package opentelemetry.collector.exporter.exporterhelper.internal.queuebatch.inte
44

55
option go_package = "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch/internal/persistentqueue";
66

7+
// Sizer type configuration
8+
enum SizerType {
9+
REQUESTS = 0;
10+
ITEMS = 1;
11+
BYTES = 2;
12+
}
13+
714
// QueueMetadata holds all persistent metadata for the queue.
815
message QueueMetadata{
9-
// Sizer type configuration (bytes=0, items=1, requests=2)
10-
int32 sizer_type_value = 1;
16+
// Sizer type configuration.
17+
SizerType sizer_type = 1;
1118

12-
// Index of the next item to be read from the queue
13-
uint64 read_index = 2;
19+
// Current total size of the queue (in bytes, items, or requests).
20+
sfixed64 queue_size = 2;
1421

15-
// Index where the next item will be written to the queue
16-
uint64 write_index = 3;
22+
// Index of the next item to be read from the queue.
23+
fixed64 read_index = 3;
1724

18-
// Current total size of the queue (in bytes, items, or requests)
19-
sfixed64 queue_size = 4;
25+
// Index where the next item will be written to the queue.
26+
fixed64 write_index = 4;
2027

21-
// List of item indices currently being processed by consumers
28+
// List of item indices currently being processed by consumers.
2229
repeated fixed64 currently_dispatched_items = 5;
2330
}

exporter/exporterhelper/internal/queuebatch/internal/persistentqueue/sizer_conversion.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,29 @@ import (
99
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
1010
)
1111

12-
// Numeric constants for protobuf serialization
13-
const (
14-
SizerTypeBytes int32 = iota // 0
15-
SizerTypeItems // 1
16-
SizerTypeRequests // 2
17-
)
18-
1912
// SizerTypeToProto converts SizerType to int32 for protobuf serialization
20-
func SizerTypeToProto(sizerType request.SizerType) (int32, error) {
13+
func SizerTypeToProto(sizerType request.SizerType) (SizerType, error) {
2114
switch sizerType {
22-
case request.SizerTypeBytes:
23-
return SizerTypeBytes, nil
24-
case request.SizerTypeItems:
25-
return SizerTypeItems, nil
2615
case request.SizerTypeRequests:
27-
return SizerTypeRequests, nil
16+
return SizerType_REQUESTS, nil
17+
case request.SizerTypeItems:
18+
return SizerType_ITEMS, nil
19+
case request.SizerTypeBytes:
20+
return SizerType_BYTES, nil
2821
default:
2922
return -1, fmt.Errorf("invalid sizer type: %v", sizerType)
3023
}
3124
}
3225

3326
// SizerTypeFromProto creates SizerType from int32 representation
34-
func SizerTypeFromProto(value int32) (request.SizerType, error) {
27+
func SizerTypeFromProto(value SizerType) (request.SizerType, error) {
3528
switch value {
36-
case SizerTypeBytes:
37-
return request.SizerTypeBytes, nil
38-
case SizerTypeItems:
39-
return request.SizerTypeItems, nil
40-
case SizerTypeRequests:
29+
case SizerType_REQUESTS:
4130
return request.SizerTypeRequests, nil
31+
case SizerType_ITEMS:
32+
return request.SizerTypeItems, nil
33+
case SizerType_BYTES:
34+
return request.SizerTypeBytes, nil
4235
default:
4336
return request.SizerType{}, fmt.Errorf("invalid sizer type value: %d", value)
4437
}

exporter/exporterhelper/internal/queuebatch/internal/persistentqueue/sizer_conversion_test.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ func TestSizerTypeToProto(t *testing.T) {
1616
tests := []struct {
1717
name string
1818
sizerType request.SizerType
19-
protoSizerType int32
19+
protoSizerType SizerType
2020
wantErr bool
2121
}{
2222
{
2323
name: "requests",
2424
sizerType: request.SizerTypeRequests,
25-
protoSizerType: SizerTypeRequests,
25+
protoSizerType: SizerType_REQUESTS,
2626
},
2727
{
2828
name: "items",
2929
sizerType: request.SizerTypeItems,
30-
protoSizerType: SizerTypeItems,
30+
protoSizerType: SizerType_ITEMS,
3131
},
3232
{
3333
name: "bytes",
3434
sizerType: request.SizerTypeBytes,
35-
protoSizerType: SizerTypeBytes,
35+
protoSizerType: SizerType_BYTES,
3636
},
3737
{
3838
name: "invalid",
@@ -58,23 +58,23 @@ func TestSizerTypeFromProto(t *testing.T) {
5858
tests := []struct {
5959
name string
6060
sizerType request.SizerType
61-
protoSizerType int32
61+
protoSizerType SizerType
6262
wantErr bool
6363
}{
6464
{
6565
name: "requests",
6666
sizerType: request.SizerTypeRequests,
67-
protoSizerType: SizerTypeRequests,
67+
protoSizerType: SizerType_REQUESTS,
6868
},
6969
{
7070
name: "items",
7171
sizerType: request.SizerTypeItems,
72-
protoSizerType: SizerTypeItems,
72+
protoSizerType: SizerType_ITEMS,
7373
},
7474
{
7575
name: "bytes",
7676
sizerType: request.SizerTypeBytes,
77-
protoSizerType: SizerTypeBytes,
77+
protoSizerType: SizerType_BYTES,
7878
},
7979
{
8080
name: "invalid",
@@ -95,3 +95,27 @@ func TestSizerTypeFromProto(t *testing.T) {
9595
})
9696
}
9797
}
98+
99+
func TestSizerTypeRoundTrip(t *testing.T) {
100+
// Test that converting to int32 and back results in the same value
101+
sizerTypes := []request.SizerType{
102+
request.SizerTypeBytes,
103+
request.SizerTypeItems,
104+
request.SizerTypeRequests,
105+
}
106+
107+
for _, originalType := range sizerTypes {
108+
t.Run(originalType.String(), func(t *testing.T) {
109+
// Convert to int32
110+
protoVal, err := SizerTypeToProto(originalType)
111+
require.NoError(t, err)
112+
113+
// Convert back to SizerType
114+
convertedType, err := SizerTypeFromProto(protoVal)
115+
require.NoError(t, err)
116+
117+
// Should be the same as original
118+
assert.Equal(t, originalType, convertedType)
119+
})
120+
}
121+
}

exporter/exporterhelper/internal/request/sizer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func (s *SizerType) MarshalText() ([]byte, error) {
5050
return []byte(s.val), nil
5151
}
5252

53+
func (s *SizerType) String() string {
54+
return s.val
55+
}
56+
5357
// Sizer is an interface that returns the size of the given element.
5458
type Sizer[T any] interface {
5559
Sizeof(T) int64

0 commit comments

Comments
 (0)