|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package exporterhelperprofiles // import "go.opentelemetry.io/collector/exporter/exporterhelper/exporterhelperprofiles" |
| 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/consumererrorprofiles" |
| 15 | + "go.opentelemetry.io/collector/consumer/consumerprofiles" |
| 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/exporterprofiles" |
| 20 | + "go.opentelemetry.io/collector/exporter/exporterqueue" |
| 21 | + "go.opentelemetry.io/collector/pdata/pprofile" |
| 22 | + "go.opentelemetry.io/collector/pipeline/pipelineprofiles" |
| 23 | +) |
| 24 | + |
| 25 | +var profilesMarshaler = &pprofile.ProtoMarshaler{} |
| 26 | +var profilesUnmarshaler = &pprofile.ProtoUnmarshaler{} |
| 27 | + |
| 28 | +type profilesRequest struct { |
| 29 | + pd pprofile.Profiles |
| 30 | + pusher consumerprofiles.ConsumeProfilesFunc |
| 31 | +} |
| 32 | + |
| 33 | +func newProfilesRequest(pd pprofile.Profiles, pusher consumerprofiles.ConsumeProfilesFunc) exporterhelper.Request { |
| 34 | + return &profilesRequest{ |
| 35 | + pd: pd, |
| 36 | + pusher: pusher, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func newProfileRequestUnmarshalerFunc(pusher consumerprofiles.ConsumeProfilesFunc) exporterqueue.Unmarshaler[exporterhelper.Request] { |
| 41 | + return func(bytes []byte) (exporterhelper.Request, error) { |
| 42 | + profiles, err := profilesUnmarshaler.UnmarshalProfiles(bytes) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + return newProfilesRequest(profiles, pusher), nil |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func profilesRequestMarshaler(req exporterhelper.Request) ([]byte, error) { |
| 51 | + return profilesMarshaler.MarshalProfiles(req.(*profilesRequest).pd) |
| 52 | +} |
| 53 | + |
| 54 | +func (req *profilesRequest) OnError(err error) exporterhelper.Request { |
| 55 | + var profileError consumererrorprofiles.Profiles |
| 56 | + if errors.As(err, &profileError) { |
| 57 | + return newProfilesRequest(profileError.Data(), req.pusher) |
| 58 | + } |
| 59 | + return req |
| 60 | +} |
| 61 | + |
| 62 | +func (req *profilesRequest) Export(ctx context.Context) error { |
| 63 | + return req.pusher(ctx, req.pd) |
| 64 | +} |
| 65 | + |
| 66 | +func (req *profilesRequest) ItemsCount() int { |
| 67 | + return req.pd.SampleCount() |
| 68 | +} |
| 69 | + |
| 70 | +type profileExporter struct { |
| 71 | + *internal.BaseExporter |
| 72 | + consumerprofiles.Profiles |
| 73 | +} |
| 74 | + |
| 75 | +// NewProfilesExporter creates an exporterprofiles.Profiles that records observability metrics and wraps every request with a Span. |
| 76 | +func NewProfilesExporter( |
| 77 | + ctx context.Context, |
| 78 | + set exporter.Settings, |
| 79 | + cfg component.Config, |
| 80 | + pusher consumerprofiles.ConsumeProfilesFunc, |
| 81 | + options ...exporterhelper.Option, |
| 82 | +) (exporterprofiles.Profiles, error) { |
| 83 | + if cfg == nil { |
| 84 | + return nil, errNilConfig |
| 85 | + } |
| 86 | + if pusher == nil { |
| 87 | + return nil, errNilPushProfileData |
| 88 | + } |
| 89 | + profilesOpts := []exporterhelper.Option{ |
| 90 | + internal.WithMarshaler(profilesRequestMarshaler), internal.WithUnmarshaler(newProfileRequestUnmarshalerFunc(pusher)), |
| 91 | + internal.WithBatchFuncs(mergeProfiles, mergeSplitProfiles), |
| 92 | + } |
| 93 | + return NewProfilesRequestExporter(ctx, set, requestFromProfiles(pusher), append(profilesOpts, options...)...) |
| 94 | +} |
| 95 | + |
| 96 | +// RequestFromProfilesFunc converts pprofile.Profiles into a user-defined Request. |
| 97 | +// Experimental: This API is at the early stage of development and may change without backward compatibility |
| 98 | +// until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved. |
| 99 | +type RequestFromProfilesFunc func(context.Context, pprofile.Profiles) (exporterhelper.Request, error) |
| 100 | + |
| 101 | +// requestFromProfiles returns a RequestFromProfilesFunc that converts pprofile.Profiles into a Request. |
| 102 | +func requestFromProfiles(pusher consumerprofiles.ConsumeProfilesFunc) RequestFromProfilesFunc { |
| 103 | + return func(_ context.Context, profiles pprofile.Profiles) (exporterhelper.Request, error) { |
| 104 | + return newProfilesRequest(profiles, pusher), nil |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// NewProfilesRequestExporter creates a new profiles exporter based on a custom ProfilesConverter and RequestSender. |
| 109 | +// Experimental: This API is at the early stage of development and may change without backward compatibility |
| 110 | +// until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved. |
| 111 | +func NewProfilesRequestExporter( |
| 112 | + _ context.Context, |
| 113 | + set exporter.Settings, |
| 114 | + converter RequestFromProfilesFunc, |
| 115 | + options ...exporterhelper.Option, |
| 116 | +) (exporterprofiles.Profiles, error) { |
| 117 | + if set.Logger == nil { |
| 118 | + return nil, errNilLogger |
| 119 | + } |
| 120 | + |
| 121 | + if converter == nil { |
| 122 | + return nil, errNilProfilesConverter |
| 123 | + } |
| 124 | + |
| 125 | + be, err := internal.NewBaseExporter(set, pipelineprofiles.SignalProfiles, newProfilesExporterWithObservability, options...) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + tc, err := consumerprofiles.NewProfiles(func(ctx context.Context, pd pprofile.Profiles) error { |
| 131 | + req, cErr := converter(ctx, pd) |
| 132 | + if cErr != nil { |
| 133 | + set.Logger.Error("Failed to convert profiles. Dropping data.", |
| 134 | + zap.Int("dropped_samples", pd.SampleCount()), |
| 135 | + zap.Error(err)) |
| 136 | + return consumererror.NewPermanent(cErr) |
| 137 | + } |
| 138 | + return be.Send(ctx, req) |
| 139 | + }, be.ConsumerOptions...) |
| 140 | + |
| 141 | + return &profileExporter{ |
| 142 | + BaseExporter: be, |
| 143 | + Profiles: tc, |
| 144 | + }, err |
| 145 | +} |
| 146 | + |
| 147 | +type profilesExporterWithObservability struct { |
| 148 | + internal.BaseRequestSender |
| 149 | + obsrep *internal.ObsReport |
| 150 | +} |
| 151 | + |
| 152 | +func newProfilesExporterWithObservability(obsrep *internal.ObsReport) internal.RequestSender { |
| 153 | + return &profilesExporterWithObservability{obsrep: obsrep} |
| 154 | +} |
| 155 | + |
| 156 | +func (tewo *profilesExporterWithObservability) Send(ctx context.Context, req exporterhelper.Request) error { |
| 157 | + c := tewo.obsrep.StartProfilesOp(ctx) |
| 158 | + numSamples := req.ItemsCount() |
| 159 | + // Forward the data to the next consumer (this pusher is the next). |
| 160 | + err := tewo.NextSender.Send(c, req) |
| 161 | + tewo.obsrep.EndProfilesOp(c, numSamples, err) |
| 162 | + return err |
| 163 | +} |
0 commit comments