-
Notifications
You must be signed in to change notification settings - Fork 487
/
gogocodec.go
78 lines (65 loc) · 2.73 KB
/
gogocodec.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
// This is copied over from Jaeger and modified to work for Tempo
// Upgrading to grpc 1.38.0 broke compatibility with gogoproto.customtype. (https://github.com/grpc/grpc-go/issues/4192)
// We use a customtype in the ingesters to pre-allocate byte slices that are reused for requests.
// Similarly Jaeger and Cortex use gogo's custom types for efficiency.
// gogoproto codec is needed only if a custom type (for ex: PreallocBytes) is used directly in a request-response object.
// The codec defined in this package allows us to choose gogo marshalling/unmarshalling for specific structs (Tempo/Jaeger/Cortex) only.
package gogocodec
import (
"reflect"
"strings"
gogoproto "github.com/gogo/protobuf/proto"
"google.golang.org/grpc/encoding"
"google.golang.org/protobuf/proto"
)
const (
frontendProtoGenPkgPath = "github.com/grafana/tempo/modules/frontend"
tempoProtoGenPkgPath = "github.com/grafana/tempo/pkg/tempopb"
jaegerProtoGenPkgPath = "github.com/jaegertracing/jaeger/proto-gen"
jaegerModelPkgPath = "github.com/jaegertracing/jaeger/model"
otelProtoPkgPath = "go.opentelemetry.io/collector"
// etcd path can be removed once upgrade to grpc >v1.38 is released (tentatively next release from v3.5.1)
etcdAPIProtoPkgPath = "go.etcd.io/etcd/api/v3"
)
func init() {
encoding.RegisterCodec(newCodec())
}
// gogoCodec forces the use of gogo proto marshalling/unmarshalling for Tempo/Cortex/Jaeger/etcd structs
type gogoCodec struct {
}
var _ encoding.Codec = (*gogoCodec)(nil)
func newCodec() *gogoCodec {
return &gogoCodec{}
}
// Name implements encoding.Codec
func (c *gogoCodec) Name() string {
return "proto"
}
// Marshal implements encoding.Codec
func (c *gogoCodec) Marshal(v interface{}) ([]byte, error) {
t := reflect.TypeOf(v)
elem := t.Elem()
// use gogo proto only for Tempo/Cortex/Jaeger/etcd types
if useGogo(elem) {
return gogoproto.Marshal(v.(gogoproto.Message))
}
return proto.Marshal(v.(proto.Message))
}
// Unmarshal implements encoding.Codec
func (c *gogoCodec) Unmarshal(data []byte, v interface{}) error {
t := reflect.TypeOf(v)
elem := t.Elem()
// use gogo proto only for Tempo/Cortex/Jaeger/etcd types
if useGogo(elem) {
return gogoproto.Unmarshal(data, v.(gogoproto.Message))
}
return proto.Unmarshal(data, v.(proto.Message))
}
// useGogo checks if the element belongs to Tempo/Cortex/Jaeger/etcd packages
func useGogo(t reflect.Type) bool {
if t == nil {
return false
}
pkgPath := t.PkgPath()
return strings.HasPrefix(pkgPath, frontendProtoGenPkgPath) || strings.HasPrefix(pkgPath, tempoProtoGenPkgPath) || strings.HasPrefix(pkgPath, jaegerProtoGenPkgPath) || strings.HasPrefix(pkgPath, jaegerModelPkgPath) || strings.HasPrefix(pkgPath, otelProtoPkgPath) || strings.HasPrefix(pkgPath, etcdAPIProtoPkgPath)
}