-
Notifications
You must be signed in to change notification settings - Fork 10
/
scope.go
102 lines (90 loc) · 3.46 KB
/
scope.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package arrow
import (
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
"go.opentelemetry.io/collector/pdata/pcommon"
acommon "github.com/open-telemetry/otel-arrow/pkg/otel/common/schema"
"github.com/open-telemetry/otel-arrow/pkg/otel/common/schema/builder"
"github.com/open-telemetry/otel-arrow/pkg/otel/constants"
"github.com/open-telemetry/otel-arrow/pkg/werror"
)
// ScopeDT is the Arrow Data Type describing a scope.
var (
ScopeDT = arrow.StructOf([]arrow.Field{
{Name: constants.ID, Type: arrow.PrimitiveTypes.Uint16, Metadata: acommon.Metadata(acommon.DeltaEncoding), Nullable: true},
{Name: constants.Name, Type: arrow.BinaryTypes.String, Metadata: acommon.Metadata(acommon.Dictionary8), Nullable: true},
{Name: constants.Version, Type: arrow.BinaryTypes.String, Metadata: acommon.Metadata(acommon.Dictionary8), Nullable: true},
{Name: constants.DroppedAttributesCount, Type: arrow.PrimitiveTypes.Uint32, Nullable: true},
}...)
)
type ScopeBuilder struct {
released bool
builder *builder.StructBuilder
nb *builder.StringBuilder // Name builder
vb *builder.StringBuilder // Version builder
aib *builder.Uint16DeltaBuilder // attributes id builder
dacb *builder.Uint32Builder // Dropped attributes count builder
}
// ScopeBuilderFrom creates a new instrumentation scope array builder from an existing struct builder.
func ScopeBuilderFrom(sb *builder.StructBuilder) *ScopeBuilder {
aib := sb.Uint16DeltaBuilder(constants.ID)
// As the attributes are sorted before insertion, the delta between two
// consecutive attributes ID should always be <=1.
// We are enforcing this constraint to make sure that the delta encoding
// will always be used efficiently.
aib.SetMaxDelta(1)
return &ScopeBuilder{
released: false,
builder: sb,
nb: sb.StringBuilder(constants.Name),
vb: sb.StringBuilder(constants.Version),
aib: aib,
dacb: sb.Uint32Builder(constants.DroppedAttributesCount),
}
}
func (b *ScopeBuilder) Append(scopeID int64, scope pcommon.InstrumentationScope) error {
if b.released {
return werror.Wrap(ErrBuilderAlreadyReleased)
}
return b.builder.Append(scope, func() error {
b.nb.AppendNonEmpty(scope.Name())
b.vb.AppendNonEmpty(scope.Version())
b.aib.Append(uint16(scopeID))
b.dacb.AppendNonZero(scope.DroppedAttributesCount())
return nil
})
}
// Build builds the instrumentation scope array struct.
//
// Once the array is no longer needed, Release() must be called to free the
// memory allocated by the array.
func (b *ScopeBuilder) Build() (*array.Struct, error) {
if b.released {
return nil, werror.Wrap(ErrBuilderAlreadyReleased)
}
defer b.Release()
return b.builder.NewStructArray(), nil
}
// Release releases the memory allocated by the builder.
func (b *ScopeBuilder) Release() {
if !b.released {
b.builder.Release()
b.released = true
}
}