Skip to content

Commit 82fe9aa

Browse files
authored
Generate internal/transform in otlploggrpc (#5553)
Part of #5056 It abstracts the `transform` package from `otlploghttp` and makes it a shared template. Then, it generates the abstracted `transform` package into `otlploggrpc`. For full usage of this transform package, check #5522
1 parent f3a2d96 commit 82fe9aa

File tree

15 files changed

+2009
-3
lines changed

15 files changed

+2009
-3
lines changed

exporters/otlp/otlplog/otlploggrpc/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ require (
66
github.com/cenkalti/backoff/v4 v4.3.0
77
github.com/stretchr/testify v1.9.0
88
go.opentelemetry.io/otel v1.27.0
9+
go.opentelemetry.io/otel/log v0.3.0
10+
go.opentelemetry.io/otel/sdk v1.27.0
911
go.opentelemetry.io/otel/sdk/log v0.3.0
12+
go.opentelemetry.io/otel/trace v1.27.0
1013
go.opentelemetry.io/proto/otlp v1.3.1
1114
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d
1215
google.golang.org/grpc v1.64.0
@@ -22,10 +25,7 @@ require (
2225
github.com/kr/text v0.2.0 // indirect
2326
github.com/pmezard/go-difflib v1.0.0 // indirect
2427
github.com/rogpeppe/go-internal v1.12.0 // indirect
25-
go.opentelemetry.io/otel/log v0.3.0 // indirect
2628
go.opentelemetry.io/otel/metric v1.27.0 // indirect
27-
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
28-
go.opentelemetry.io/otel/trace v1.27.0 // indirect
2929
golang.org/x/net v0.26.0 // indirect
3030
golang.org/x/sys v0.21.0 // indirect
3131
golang.org/x/text v0.16.0 // indirect

exporters/otlp/otlplog/otlploggrpc/internal/gen.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ package internal // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlp
55

66
//go:generate gotmpl --body=../../../../../internal/shared/otlp/retry/retry.go.tmpl "--data={}" --out=retry/retry.go
77
//go:generate gotmpl --body=../../../../../internal/shared/otlp/retry/retry_test.go.tmpl "--data={}" --out=retry/retry_test.go
8+
9+
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlplog/transform/attr_test.go.tmpl "--data={}" --out=transform/attr_test.go
10+
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlplog/transform/log.go.tmpl "--data={}" --out=transform/log.go
11+
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlplog/transform/log_attr_test.go.tmpl "--data={}" --out=transform/log_attr_test.go
12+
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlplog/transform/log_test.go.tmpl "--data={}" --out=transform/log_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Code created by gotmpl. DO NOT MODIFY.
2+
// source: internal/shared/otlp/otlplog/transform/attr_test.go.tmpl
3+
4+
// Copyright The OpenTelemetry Authors
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
package transform
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
14+
"go.opentelemetry.io/otel/attribute"
15+
cpb "go.opentelemetry.io/proto/otlp/common/v1"
16+
)
17+
18+
var (
19+
attrBool = attribute.Bool("bool", true)
20+
attrBoolSlice = attribute.BoolSlice("bool slice", []bool{true, false})
21+
attrInt = attribute.Int("int", 1)
22+
attrIntSlice = attribute.IntSlice("int slice", []int{-1, 1})
23+
attrInt64 = attribute.Int64("int64", 1)
24+
attrInt64Slice = attribute.Int64Slice("int64 slice", []int64{-1, 1})
25+
attrFloat64 = attribute.Float64("float64", 1)
26+
attrFloat64Slice = attribute.Float64Slice("float64 slice", []float64{-1, 1})
27+
attrString = attribute.String("string", "o")
28+
attrStringSlice = attribute.StringSlice("string slice", []string{"o", "n"})
29+
attrInvalid = attribute.KeyValue{
30+
Key: attribute.Key("invalid"),
31+
Value: attribute.Value{},
32+
}
33+
34+
valBoolTrue = &cpb.AnyValue{Value: &cpb.AnyValue_BoolValue{BoolValue: true}}
35+
valBoolFalse = &cpb.AnyValue{Value: &cpb.AnyValue_BoolValue{BoolValue: false}}
36+
valBoolSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{
37+
ArrayValue: &cpb.ArrayValue{
38+
Values: []*cpb.AnyValue{valBoolTrue, valBoolFalse},
39+
},
40+
}}
41+
valIntOne = &cpb.AnyValue{Value: &cpb.AnyValue_IntValue{IntValue: 1}}
42+
valIntNOne = &cpb.AnyValue{Value: &cpb.AnyValue_IntValue{IntValue: -1}}
43+
valIntSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{
44+
ArrayValue: &cpb.ArrayValue{
45+
Values: []*cpb.AnyValue{valIntNOne, valIntOne},
46+
},
47+
}}
48+
valDblOne = &cpb.AnyValue{Value: &cpb.AnyValue_DoubleValue{DoubleValue: 1}}
49+
valDblNOne = &cpb.AnyValue{Value: &cpb.AnyValue_DoubleValue{DoubleValue: -1}}
50+
valDblSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{
51+
ArrayValue: &cpb.ArrayValue{
52+
Values: []*cpb.AnyValue{valDblNOne, valDblOne},
53+
},
54+
}}
55+
valStrO = &cpb.AnyValue{Value: &cpb.AnyValue_StringValue{StringValue: "o"}}
56+
valStrN = &cpb.AnyValue{Value: &cpb.AnyValue_StringValue{StringValue: "n"}}
57+
valStrSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{
58+
ArrayValue: &cpb.ArrayValue{
59+
Values: []*cpb.AnyValue{valStrO, valStrN},
60+
},
61+
}}
62+
63+
kvBool = &cpb.KeyValue{Key: "bool", Value: valBoolTrue}
64+
kvBoolSlice = &cpb.KeyValue{Key: "bool slice", Value: valBoolSlice}
65+
kvInt = &cpb.KeyValue{Key: "int", Value: valIntOne}
66+
kvIntSlice = &cpb.KeyValue{Key: "int slice", Value: valIntSlice}
67+
kvInt64 = &cpb.KeyValue{Key: "int64", Value: valIntOne}
68+
kvInt64Slice = &cpb.KeyValue{Key: "int64 slice", Value: valIntSlice}
69+
kvFloat64 = &cpb.KeyValue{Key: "float64", Value: valDblOne}
70+
kvFloat64Slice = &cpb.KeyValue{Key: "float64 slice", Value: valDblSlice}
71+
kvString = &cpb.KeyValue{Key: "string", Value: valStrO}
72+
kvStringSlice = &cpb.KeyValue{Key: "string slice", Value: valStrSlice}
73+
kvInvalid = &cpb.KeyValue{
74+
Key: "invalid",
75+
Value: &cpb.AnyValue{
76+
Value: &cpb.AnyValue_StringValue{StringValue: "INVALID"},
77+
},
78+
}
79+
)
80+
81+
func TestAttrTransforms(t *testing.T) {
82+
type attrTest struct {
83+
name string
84+
in []attribute.KeyValue
85+
want []*cpb.KeyValue
86+
}
87+
88+
for _, test := range []attrTest{
89+
{"nil", nil, nil},
90+
{"empty", []attribute.KeyValue{}, nil},
91+
{
92+
"invalid",
93+
[]attribute.KeyValue{attrInvalid},
94+
[]*cpb.KeyValue{kvInvalid},
95+
},
96+
{
97+
"bool",
98+
[]attribute.KeyValue{attrBool},
99+
[]*cpb.KeyValue{kvBool},
100+
},
101+
{
102+
"bool slice",
103+
[]attribute.KeyValue{attrBoolSlice},
104+
[]*cpb.KeyValue{kvBoolSlice},
105+
},
106+
{
107+
"int",
108+
[]attribute.KeyValue{attrInt},
109+
[]*cpb.KeyValue{kvInt},
110+
},
111+
{
112+
"int slice",
113+
[]attribute.KeyValue{attrIntSlice},
114+
[]*cpb.KeyValue{kvIntSlice},
115+
},
116+
{
117+
"int64",
118+
[]attribute.KeyValue{attrInt64},
119+
[]*cpb.KeyValue{kvInt64},
120+
},
121+
{
122+
"int64 slice",
123+
[]attribute.KeyValue{attrInt64Slice},
124+
[]*cpb.KeyValue{kvInt64Slice},
125+
},
126+
{
127+
"float64",
128+
[]attribute.KeyValue{attrFloat64},
129+
[]*cpb.KeyValue{kvFloat64},
130+
},
131+
{
132+
"float64 slice",
133+
[]attribute.KeyValue{attrFloat64Slice},
134+
[]*cpb.KeyValue{kvFloat64Slice},
135+
},
136+
{
137+
"string",
138+
[]attribute.KeyValue{attrString},
139+
[]*cpb.KeyValue{kvString},
140+
},
141+
{
142+
"string slice",
143+
[]attribute.KeyValue{attrStringSlice},
144+
[]*cpb.KeyValue{kvStringSlice},
145+
},
146+
{
147+
"all",
148+
[]attribute.KeyValue{
149+
attrBool,
150+
attrBoolSlice,
151+
attrInt,
152+
attrIntSlice,
153+
attrInt64,
154+
attrInt64Slice,
155+
attrFloat64,
156+
attrFloat64Slice,
157+
attrString,
158+
attrStringSlice,
159+
attrInvalid,
160+
},
161+
[]*cpb.KeyValue{
162+
kvBool,
163+
kvBoolSlice,
164+
kvInt,
165+
kvIntSlice,
166+
kvInt64,
167+
kvInt64Slice,
168+
kvFloat64,
169+
kvFloat64Slice,
170+
kvString,
171+
kvStringSlice,
172+
kvInvalid,
173+
},
174+
},
175+
} {
176+
t.Run(test.name, func(t *testing.T) {
177+
t.Run("Attrs", func(t *testing.T) {
178+
assert.ElementsMatch(t, test.want, Attrs(test.in))
179+
})
180+
t.Run("AttrIter", func(t *testing.T) {
181+
s := attribute.NewSet(test.in...)
182+
assert.ElementsMatch(t, test.want, AttrIter(s.Iter()))
183+
})
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)