Skip to content

Commit f0279bb

Browse files
committed
Use wrapper function instead of defer
1 parent 1edb224 commit f0279bb

File tree

4 files changed

+93
-49
lines changed

4 files changed

+93
-49
lines changed

exporter/otlpexporter/factory.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createTraces(
5959
oce := newExporter(cfg, set)
6060
oCfg := cfg.(*Config)
6161
return exporterhelper.NewTraces(ctx, set, cfg,
62-
oce.pushTraces,
62+
oce.pushTracesWithStatus,
6363
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
6464
exporterhelper.WithTimeout(oCfg.TimeoutConfig),
6565
exporterhelper.WithRetry(oCfg.RetryConfig),
@@ -78,7 +78,7 @@ func createMetrics(
7878
oce := newExporter(cfg, set)
7979
oCfg := cfg.(*Config)
8080
return exporterhelper.NewMetrics(ctx, set, cfg,
81-
oce.pushMetrics,
81+
oce.pushMetricsWithStatus,
8282
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
8383
exporterhelper.WithTimeout(oCfg.TimeoutConfig),
8484
exporterhelper.WithRetry(oCfg.RetryConfig),
@@ -97,7 +97,7 @@ func createLogs(
9797
oce := newExporter(cfg, set)
9898
oCfg := cfg.(*Config)
9999
return exporterhelper.NewLogs(ctx, set, cfg,
100-
oce.pushLogs,
100+
oce.pushLogsWithStatus,
101101
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
102102
exporterhelper.WithTimeout(oCfg.TimeoutConfig),
103103
exporterhelper.WithRetry(oCfg.RetryConfig),
@@ -116,7 +116,7 @@ func createProfilesExporter(
116116
oce := newExporter(cfg, set)
117117
oCfg := cfg.(*Config)
118118
return exporterhelperprofiles.NewProfilesExporter(ctx, set, cfg,
119-
oce.pushProfiles,
119+
oce.pushProfilesWithStatus,
120120
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
121121
exporterhelper.WithTimeout(oCfg.TimeoutConfig),
122122
exporterhelper.WithRetry(oCfg.RetryConfig),

exporter/otlpexporter/otlp.go

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,11 @@ func (e *baseExporter) shutdown(context.Context) error {
9292
return nil
9393
}
9494

95-
func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) (err error) {
96-
defer func() {
97-
e.reportStatusFromError(err)
98-
}()
95+
func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) error {
9996
req := ptraceotlp.NewExportRequestFromTraces(td)
10097
resp, respErr := e.traceExporter.Export(e.enhanceContext(ctx), req, e.callOptions...)
101-
if err = processError(respErr); err != nil {
102-
return
98+
if err := processError(respErr); err != nil {
99+
return err
103100
}
104101
partialSuccess := resp.PartialSuccess()
105102
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedSpans() == 0) {
@@ -108,17 +105,23 @@ func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) (err er
108105
zap.Int64("dropped_spans", resp.PartialSuccess().RejectedSpans()),
109106
)
110107
}
111-
return
108+
return nil
109+
}
110+
111+
func (e *baseExporter) pushTracesWithStatus(ctx context.Context, td ptrace.Traces) error {
112+
if err := e.pushTraces(ctx, td); err != nil {
113+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
114+
return err
115+
}
116+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
117+
return nil
112118
}
113119

114-
func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) (err error) {
115-
defer func() {
116-
e.reportStatusFromError(err)
117-
}()
120+
func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error {
118121
req := pmetricotlp.NewExportRequestFromMetrics(md)
119122
resp, respErr := e.metricExporter.Export(e.enhanceContext(ctx), req, e.callOptions...)
120-
if err = processError(respErr); err != nil {
121-
return
123+
if err := processError(respErr); err != nil {
124+
return err
122125
}
123126
partialSuccess := resp.PartialSuccess()
124127
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedDataPoints() == 0) {
@@ -127,17 +130,23 @@ func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) (err
127130
zap.Int64("dropped_data_points", resp.PartialSuccess().RejectedDataPoints()),
128131
)
129132
}
130-
return
133+
return nil
131134
}
132135

133-
func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) (err error) {
134-
defer func() {
135-
e.reportStatusFromError(err)
136-
}()
136+
func (e *baseExporter) pushMetricsWithStatus(ctx context.Context, md pmetric.Metrics) error {
137+
if err := e.pushMetrics(ctx, md); err != nil {
138+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
139+
return err
140+
}
141+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
142+
return nil
143+
}
144+
145+
func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) error {
137146
req := plogotlp.NewExportRequestFromLogs(ld)
138147
resp, respErr := e.logExporter.Export(e.enhanceContext(ctx), req, e.callOptions...)
139-
if err = processError(respErr); err != nil {
140-
return
148+
if err := processError(respErr); err != nil {
149+
return err
141150
}
142151
partialSuccess := resp.PartialSuccess()
143152
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedLogRecords() == 0) {
@@ -146,7 +155,16 @@ func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) (err error) {
146155
zap.Int64("dropped_log_records", resp.PartialSuccess().RejectedLogRecords()),
147156
)
148157
}
149-
return
158+
return nil
159+
}
160+
161+
func (e *baseExporter) pushLogsWithStatus(ctx context.Context, ld plog.Logs) error {
162+
if err := e.pushLogs(ctx, ld); err != nil {
163+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
164+
return err
165+
}
166+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
167+
return nil
150168
}
151169

152170
func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) error {
@@ -165,6 +183,15 @@ func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) e
165183
return nil
166184
}
167185

186+
func (e *baseExporter) pushProfilesWithStatus(ctx context.Context, td pprofile.Profiles) error {
187+
if err := e.pushProfiles(ctx, td); err != nil {
188+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
189+
return err
190+
}
191+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
192+
return nil
193+
}
194+
168195
func (e *baseExporter) enhanceContext(ctx context.Context) context.Context {
169196
if e.metadata.Len() > 0 {
170197
return metadata.NewOutgoingContext(ctx, e.metadata)
@@ -203,14 +230,6 @@ func processError(err error) error {
203230
return err
204231
}
205232

206-
func (e *baseExporter) reportStatusFromError(err error) {
207-
if err != nil {
208-
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
209-
return
210-
}
211-
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
212-
}
213-
214233
func shouldRetry(code codes.Code, retryInfo *errdetails.RetryInfo) bool {
215234
switch code {
216235
case codes.Canceled,

exporter/otlphttpexporter/factory.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func createTraces(
9090
}
9191

9292
return exporterhelper.NewTraces(ctx, set, cfg,
93-
oce.pushTraces,
93+
oce.pushTracesWithStatus,
9494
exporterhelper.WithStart(oce.start),
9595
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
9696
// explicitly disable since we rely on http.Client timeout logic.
@@ -116,7 +116,7 @@ func createMetrics(
116116
}
117117

118118
return exporterhelper.NewMetrics(ctx, set, cfg,
119-
oce.pushMetrics,
119+
oce.pushMetricsWithStatus,
120120
exporterhelper.WithStart(oce.start),
121121
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
122122
// explicitly disable since we rely on http.Client timeout logic.
@@ -141,7 +141,7 @@ func createLogs(
141141
}
142142

143143
return exporterhelper.NewLogs(ctx, set, cfg,
144-
oce.pushLogs,
144+
oce.pushLogsWithStatus,
145145
exporterhelper.WithStart(oce.start),
146146
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
147147
// explicitly disable since we rely on http.Client timeout logic.
@@ -167,7 +167,7 @@ func createProfiles(
167167
}
168168

169169
return exporterhelperprofiles.NewProfilesExporter(ctx, set, cfg,
170-
oce.pushProfiles,
170+
oce.pushProfilesWithStatus,
171171
exporterhelper.WithStart(oce.start),
172172
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
173173
// explicitly disable since we rely on http.Client timeout logic.

exporter/otlphttpexporter/otlp.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) error {
114114
return e.export(ctx, e.tracesURL, request, e.tracesPartialSuccessHandler)
115115
}
116116

117+
func (e *baseExporter) pushTracesWithStatus(ctx context.Context, td ptrace.Traces) error {
118+
if err := e.pushTraces(ctx, td); err != nil {
119+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
120+
return err
121+
}
122+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
123+
return nil
124+
}
125+
117126
func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error {
118127
tr := pmetricotlp.NewExportRequestFromMetrics(md)
119128

@@ -134,6 +143,15 @@ func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) erro
134143
return e.export(ctx, e.metricsURL, request, e.metricsPartialSuccessHandler)
135144
}
136145

146+
func (e *baseExporter) pushMetricsWithStatus(ctx context.Context, md pmetric.Metrics) error {
147+
if err := e.pushMetrics(ctx, md); err != nil {
148+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
149+
return err
150+
}
151+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
152+
return nil
153+
}
154+
137155
func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) error {
138156
tr := plogotlp.NewExportRequestFromLogs(ld)
139157

@@ -155,6 +173,15 @@ func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) error {
155173
return e.export(ctx, e.logsURL, request, e.logsPartialSuccessHandler)
156174
}
157175

176+
func (e *baseExporter) pushLogsWithStatus(ctx context.Context, ld plog.Logs) error {
177+
if err := e.pushLogs(ctx, ld); err != nil {
178+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
179+
return err
180+
}
181+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
182+
return nil
183+
}
184+
158185
func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) error {
159186
tr := pprofileotlp.NewExportRequestFromProfiles(td)
160187

@@ -176,10 +203,16 @@ func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) e
176203
return e.export(ctx, e.profilesURL, request, e.profilesPartialSuccessHandler)
177204
}
178205

179-
func (e *baseExporter) export(ctx context.Context, url string, request []byte, partialSuccessHandler partialSuccessHandler) (err error) {
180-
defer func() {
181-
e.reportStatusFromError(err)
182-
}()
206+
func (e *baseExporter) pushProfilesWithStatus(ctx context.Context, td pprofile.Profiles) error {
207+
if err := e.pushProfiles(ctx, td); err != nil {
208+
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
209+
return err
210+
}
211+
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
212+
return nil
213+
}
214+
215+
func (e *baseExporter) export(ctx context.Context, url string, request []byte, partialSuccessHandler partialSuccessHandler) error {
183216
e.logger.Debug("Preparing to make HTTP request", zap.String("url", url))
184217
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(request))
185218
if err != nil {
@@ -248,14 +281,6 @@ func (e *baseExporter) export(ctx context.Context, url string, request []byte, p
248281
return consumererror.NewPermanent(formattedErr)
249282
}
250283

251-
func (e *baseExporter) reportStatusFromError(err error) {
252-
if err != nil {
253-
componentstatus.ReportStatus(e.host, componentstatus.NewRecoverableErrorEvent(err))
254-
return
255-
}
256-
componentstatus.ReportStatus(e.host, componentstatus.NewEvent(componentstatus.StatusOK))
257-
}
258-
259284
// Determine if the status code is retryable according to the specification.
260285
// For more, see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#failures-1
261286
func isRetryableStatusCode(code int) bool {

0 commit comments

Comments
 (0)