| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package xexporterhelper |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
|
| 10 |
"go.uber.org/zap" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/component" |
| 13 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 14 |
"go.opentelemetry.io/collector/consumer/consumererror/xconsumererror" |
| 15 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 16 |
"go.opentelemetry.io/collector/exporter" |
| 17 |
"go.opentelemetry.io/collector/exporter/exporterhelper" |
| 18 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal" |
| 19 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/queue" |
| 20 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" |
| 21 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer" |
| 22 |
"go.opentelemetry.io/collector/exporter/xexporter" |
| 23 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 24 |
"go.opentelemetry.io/collector/pdata/xpdata/pref" |
| 25 |
pdatareq "go.opentelemetry.io/collector/pdata/xpdata/request" |
| 26 |
"go.opentelemetry.io/collector/pipeline/xpipeline" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
profilesMarshaler = &pprofile.ProtoMarshaler{} |
| 31 |
profilesUnmarshaler = &pprofile.ProtoUnmarshaler{} |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func NewProfilesQueueBatchSettings() QueueBatchSettings { |
| 38 |
return QueueBatchSettings{ |
| 39 |
ReferenceCounter: profilesReferenceCounter{}, |
| 40 |
Encoding: profilesEncoding{}, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
var ( |
| 45 |
_ request.Request = (*profilesRequest)(nil) |
| 46 |
_ request.ErrorHandler = (*profilesRequest)(nil) |
| 47 |
) |
| 48 |
|
| 49 |
type profilesRequest struct { |
| 50 |
pd pprofile.Profiles |
| 51 |
cachedSize int |
| 52 |
} |
| 53 |
|
| 54 |
func newProfilesRequest(pd pprofile.Profiles) Request { |
| 55 |
return &profilesRequest{ |
| 56 |
pd: pd, |
| 57 |
cachedSize: -1, |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
type profilesEncoding struct{} |
| 62 |
|
| 63 |
var _ exporterhelper.QueueBatchEncoding[request.Request] = profilesEncoding{} |
| 64 |
|
| 65 |
func (profilesEncoding) Unmarshal(bytes []byte) (context.Context, request.Request, error) { |
| 66 |
if queue.PersistRequestContextOnRead() { |
| 67 |
ctx, profiles, err := pdatareq.UnmarshalProfiles(bytes) |
| 68 |
if errors.Is(err, pdatareq.ErrInvalidFormat) { |
| 69 |
|
| 70 |
profiles, err = profilesUnmarshaler.UnmarshalProfiles(bytes) |
| 71 |
} |
| 72 |
return ctx, newProfilesRequest(profiles), err |
| 73 |
} |
| 74 |
profiles, err := profilesUnmarshaler.UnmarshalProfiles(bytes) |
| 75 |
if err != nil { |
| 76 |
var req request.Request |
| 77 |
return context.Background(), req, err |
| 78 |
} |
| 79 |
return context.Background(), newProfilesRequest(profiles), nil |
| 80 |
} |
| 81 |
|
| 82 |
func (profilesEncoding) Marshal(ctx context.Context, req request.Request) ([]byte, error) { |
| 83 |
profiles := req.(*profilesRequest).pd |
| 84 |
if queue.PersistRequestContextOnWrite() { |
| 85 |
return pdatareq.MarshalProfiles(ctx, profiles) |
| 86 |
} |
| 87 |
return profilesMarshaler.MarshalProfiles(profiles) |
| 88 |
} |
| 89 |
|
| 90 |
var _ queue.ReferenceCounter[request.Request] = profilesReferenceCounter{} |
| 91 |
|
| 92 |
type profilesReferenceCounter struct{} |
| 93 |
|
| 94 |
func (profilesReferenceCounter) Ref(req request.Request) { |
| 95 |
pref.RefProfiles(req.(*profilesRequest).pd) |
| 96 |
} |
| 97 |
|
| 98 |
func (profilesReferenceCounter) Unref(req request.Request) { |
| 99 |
pref.UnrefProfiles(req.(*profilesRequest).pd) |
| 100 |
} |
| 101 |
|
| 102 |
func (req *profilesRequest) OnError(err error) Request { |
| 103 |
var profileError xconsumererror.Profiles |
| 104 |
if errors.As(err, &profileError) { |
| 105 |
|
| 106 |
return newProfilesRequest(profileError.Data()) |
| 107 |
} |
| 108 |
return req |
| 109 |
} |
| 110 |
|
| 111 |
func (req *profilesRequest) ItemsCount() int { |
| 112 |
return req.pd.SampleCount() |
| 113 |
} |
| 114 |
|
| 115 |
func (req *profilesRequest) size(sizer sizer.ProfilesSizer) int { |
| 116 |
if req.cachedSize == -1 { |
| 117 |
req.cachedSize = sizer.ProfilesSize(req.pd) |
| 118 |
} |
| 119 |
return req.cachedSize |
| 120 |
} |
| 121 |
|
| 122 |
func (req *profilesRequest) setCachedSize(size int) { |
| 123 |
req.cachedSize = size |
| 124 |
} |
| 125 |
|
| 126 |
func (req *profilesRequest) BytesSize() int { |
| 127 |
return profilesMarshaler.ProfilesSize(req.pd) |
| 128 |
} |
| 129 |
|
| 130 |
type profileExporter struct { |
| 131 |
*internal.BaseExporter |
| 132 |
xconsumer.Profiles |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func NewProfiles( |
| 137 |
ctx context.Context, |
| 138 |
set exporter.Settings, |
| 139 |
cfg component.Config, |
| 140 |
pusher xconsumer.ConsumeProfilesFunc, |
| 141 |
options ...exporterhelper.Option, |
| 142 |
) (xexporter.Profiles, error) { |
| 143 |
if cfg == nil { |
| 144 |
return nil, errNilConfig |
| 145 |
} |
| 146 |
if pusher == nil { |
| 147 |
return nil, errNilPushProfileData |
| 148 |
} |
| 149 |
return NewProfilesRequest(ctx, set, requestFromProfiles(), requestConsumeFromProfiles(pusher), |
| 150 |
append([]exporterhelper.Option{internal.WithQueueBatchSettings(NewProfilesQueueBatchSettings())}, options...)...) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func requestConsumeFromProfiles(pusher xconsumer.ConsumeProfilesFunc) RequestConsumeFunc { |
| 155 |
return func(ctx context.Context, request Request) error { |
| 156 |
return pusher.ConsumeProfiles(ctx, request.(*profilesRequest).pd) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
func requestFromProfiles() RequestConverterFunc[pprofile.Profiles] { |
| 162 |
return func(_ context.Context, profiles pprofile.Profiles) (Request, error) { |
| 163 |
return newProfilesRequest(profiles), nil |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func NewProfilesRequest( |
| 171 |
_ context.Context, |
| 172 |
set exporter.Settings, |
| 173 |
converter RequestConverterFunc[pprofile.Profiles], |
| 174 |
pusher RequestConsumeFunc, |
| 175 |
options ...exporterhelper.Option, |
| 176 |
) (xexporter.Profiles, error) { |
| 177 |
if set.Logger == nil { |
| 178 |
return nil, errNilLogger |
| 179 |
} |
| 180 |
|
| 181 |
if converter == nil { |
| 182 |
return nil, errNilProfilesConverter |
| 183 |
} |
| 184 |
|
| 185 |
if pusher == nil { |
| 186 |
return nil, errNilConsumeRequest |
| 187 |
} |
| 188 |
|
| 189 |
be, err := internal.NewBaseExporter(set, xpipeline.SignalProfiles, pusher, options...) |
| 190 |
if err != nil { |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
|
| 194 |
tc, err := xconsumer.NewProfiles(newConsumeProfiles(converter, be, set.Logger), be.ConsumerOptions...) |
| 195 |
if err != nil { |
| 196 |
return nil, err |
| 197 |
} |
| 198 |
|
| 199 |
return &profileExporter{BaseExporter: be, Profiles: tc}, nil |
| 200 |
} |
| 201 |
|
| 202 |
func newConsumeProfiles(converter RequestConverterFunc[pprofile.Profiles], be *internal.BaseExporter, logger *zap.Logger) xconsumer.ConsumeProfilesFunc { |
| 203 |
return func(ctx context.Context, pd pprofile.Profiles) error { |
| 204 |
req, err := converter(ctx, pd) |
| 205 |
if err != nil { |
| 206 |
logger.Error("Failed to convert profiles. Dropping data.", |
| 207 |
zap.Int("dropped_samples", pd.SampleCount()), |
| 208 |
zap.Error(err)) |
| 209 |
return consumererror.NewPermanent(err) |
| 210 |
} |
| 211 |
return be.Send(ctx, req) |
| 212 |
} |
| 213 |
} |