@@ -61,6 +61,7 @@ type internalRequest interface {
6161 attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo )
6262 retryPolicy () RetryPolicy
6363 speculativeExecutionPolicy () SpeculativeExecutionPolicy
64+ speculativeExecutionStarted () // Used to update speculative execution count
6465 getQueryMetrics () * queryMetrics
6566 getRoutingInfo () * queryRoutingInfo
6667 getKeyspaceFunc () func () string
@@ -91,6 +92,8 @@ func (q *queryExecutor) speculate(ctx context.Context, qry internalRequest, sp S
9192 for i := 0 ; i < sp .Attempts (); i ++ {
9293 select {
9394 case <- ticker .C :
95+ // Increment speculative count in metrics so it's available to the observer
96+ qry .speculativeExecutionStarted ()
9497 go q .run (ctx , qry , hostIter , results )
9598 case <- ctx .Done ():
9699 return newErrIter (ctx .Err (), qry .getQueryMetrics (), qry .Keyspace (), qry .getRoutingInfo (), qry .getKeyspaceFunc ())
@@ -383,17 +386,18 @@ func (q *internalQuery) attempt(keyspace string, end, start time.Time, iter *Ite
383386 if q .qryOpts .observer != nil {
384387 metricsForHost := q .hostMetricsManager .attempt (latency , host )
385388 q .qryOpts .observer .ObserveQuery (q .qryOpts .context , ObservedQuery {
386- Keyspace : keyspace ,
387- Statement : q .qryOpts .stmt ,
388- Values : q .qryOpts .values ,
389- Start : start ,
390- End : end ,
391- Rows : iter .numRows ,
392- Host : host ,
393- Metrics : metricsForHost ,
394- Err : iter .err ,
395- Attempt : attempt ,
396- Query : q .originalQuery ,
389+ Keyspace : keyspace ,
390+ Statement : q .qryOpts .stmt ,
391+ Values : q .qryOpts .values ,
392+ Start : start ,
393+ End : end ,
394+ Rows : iter .numRows ,
395+ Host : host ,
396+ Metrics : metricsForHost ,
397+ Err : iter .err ,
398+ Attempt : attempt ,
399+ Query : q .originalQuery ,
400+ SpeculativeExecutions : q .metrics .speculativeExecutions (),
397401 })
398402 }
399403}
@@ -410,6 +414,10 @@ func (q *internalQuery) speculativeExecutionPolicy() SpeculativeExecutionPolicy
410414 return q .qryOpts .spec
411415}
412416
417+ func (q * internalQuery ) speculativeExecutionStarted () {
418+ q .metrics .speculativeExecution ()
419+ }
420+
413421func (q * internalQuery ) GetRoutingKey () ([]byte , error ) {
414422 if q .qryOpts .routingKey != nil {
415423 return q .qryOpts .routingKey , nil
@@ -612,11 +620,12 @@ func (b *internalBatch) attempt(keyspace string, end, start time.Time, iter *Ite
612620 Start : start ,
613621 End : end ,
614622 // Rows not used in batch observations // TODO - might be able to support it when using BatchCAS
615- Host : host ,
616- Metrics : metricsForHost ,
617- Err : iter .err ,
618- Attempt : attempt ,
619- Batch : b .originalBatch ,
623+ Host : host ,
624+ Metrics : metricsForHost ,
625+ Err : iter .err ,
626+ Attempt : attempt ,
627+ Batch : b .originalBatch ,
628+ SpeculativeExecutions : b .metrics .speculativeExecutions (),
620629 })
621630}
622631
@@ -628,6 +637,10 @@ func (b *internalBatch) speculativeExecutionPolicy() SpeculativeExecutionPolicy
628637 return b .batchOpts .spec
629638}
630639
640+ func (b * internalBatch ) speculativeExecutionStarted () {
641+ b .metrics .speculativeExecution ()
642+ }
643+
631644func (b * internalBatch ) GetRoutingKey () ([]byte , error ) {
632645 if b .batchOpts .routingKey != nil {
633646 return b .batchOpts .routingKey , nil
0 commit comments