@@ -145,61 +145,70 @@ func NewInstrumentation(id int64) (*Instrumentation, error) {
145145 return i , err
146146}
147147
148- // ExportSpansDone is a function that is called when a call to an Exporter's
149- // ExportSpans method completes.
150- //
151- // The number of successful exports is provided as success. Any error that is
152- // encountered is provided as err.
153- type ExportSpansDone func (success int64 , err error )
154-
155148// ExportSpans instruments the ExportSpans method of the exporter. It returns a
156149// function that needs to be deferred so it is called when the method returns.
157- func (i * Instrumentation ) ExportSpans (ctx context.Context , nSpans int ) ExportSpansDone {
150+ func (i * Instrumentation ) ExportSpans (ctx context.Context , nSpans int ) ExportOp {
158151 start := time .Now ()
159152
160153 addOpt := get [metric.AddOption ](addOptPool )
161154 defer put (addOptPool , addOpt )
162155 * addOpt = append (* addOpt , i .setOpt )
163156 i .inflightSpans .Add (ctx , int64 (nSpans ), * addOpt ... )
164157
165- return i .end (ctx , start , int64 (nSpans ))
158+ return ExportOp {
159+ ctx : ctx ,
160+ start : start ,
161+ nSpans : int64 (nSpans ),
162+ inst : i ,
163+ }
164+ }
165+
166+ // ExportOp is an in-progress ExportSpans operation.
167+ type ExportOp struct {
168+ ctx context.Context
169+ start time.Time
170+ nSpans int64
171+ inst * Instrumentation
166172}
167173
168- func (i * Instrumentation ) end (ctx context.Context , start time.Time , n int64 ) ExportSpansDone {
169- return func (success int64 , err error ) {
170- addOpt := get [metric.AddOption ](addOptPool )
171- defer put (addOptPool , addOpt )
172- * addOpt = append (* addOpt , i .setOpt )
173-
174- i .inflightSpans .Add (ctx , - n , * addOpt ... )
175-
176- // Record the success and duration of the operation.
177- //
178- // Do not exclude 0 values, as they are valid and indicate no spans
179- // were exported which is meaningful for certain aggregations.
180- i .exportedSpans .Add (ctx , success , * addOpt ... )
181-
182- mOpt := i .setOpt
183- if err != nil {
184- attrs := get [attribute.KeyValue ](measureAttrsPool )
185- defer put (measureAttrsPool , attrs )
186- * attrs = append (* attrs , i .attrs ... )
187- * attrs = append (* attrs , semconv .ErrorType (err ))
188-
189- // Do not inefficiently make a copy of attrs by using
190- // WithAttributes instead of WithAttributeSet.
191- set := attribute .NewSet (* attrs ... )
192- mOpt = metric .WithAttributeSet (set )
193-
194- // Reset addOpt with new attribute set.
195- * addOpt = append ((* addOpt )[:0 ], mOpt )
196-
197- i .exportedSpans .Add (ctx , n - success , * addOpt ... )
198- }
199-
200- recordOpt := get [metric.RecordOption ](recordOptPool )
201- defer put (recordOptPool , recordOpt )
202- * recordOpt = append (* recordOpt , mOpt )
203- i .opDuration .Record (ctx , time .Since (start ).Seconds (), * recordOpt ... )
174+ // End ends the ExportSpans operation, recording its success and duration.
175+ //
176+ // The success parameter indicates how many spans were successfully exported.
177+ // The err parameter indicates whether the operation failed. If err is not nil,
178+ // the number of failed spans (nSpans - success) is also recorded.
179+ func (e ExportOp ) End (success int64 , err error ) {
180+ addOpt := get [metric.AddOption ](addOptPool )
181+ defer put (addOptPool , addOpt )
182+ * addOpt = append (* addOpt , e .inst .setOpt )
183+
184+ e .inst .inflightSpans .Add (e .ctx , - e .nSpans , * addOpt ... )
185+
186+ // Record the success and duration of the operation.
187+ //
188+ // Do not exclude 0 values, as they are valid and indicate no spans
189+ // were exported which is meaningful for certain aggregations.
190+ e .inst .exportedSpans .Add (e .ctx , success , * addOpt ... )
191+
192+ mOpt := e .inst .setOpt
193+ if err != nil {
194+ attrs := get [attribute.KeyValue ](measureAttrsPool )
195+ defer put (measureAttrsPool , attrs )
196+ * attrs = append (* attrs , e .inst .attrs ... )
197+ * attrs = append (* attrs , semconv .ErrorType (err ))
198+
199+ // Do not inefficiently make a copy of attrs by using
200+ // WithAttributes instead of WithAttributeSet.
201+ set := attribute .NewSet (* attrs ... )
202+ mOpt = metric .WithAttributeSet (set )
203+
204+ // Reset addOpt with new attribute set.
205+ * addOpt = append ((* addOpt )[:0 ], mOpt )
206+
207+ e .inst .exportedSpans .Add (e .ctx , e .nSpans - success , * addOpt ... )
204208 }
209+
210+ recordOpt := get [metric.RecordOption ](recordOptPool )
211+ defer put (recordOptPool , recordOpt )
212+ * recordOpt = append (* recordOpt , mOpt )
213+ e .inst .opDuration .Record (e .ctx , time .Since (e .start ).Seconds (), * recordOpt ... )
205214}
0 commit comments