@@ -61,7 +61,7 @@ type internalRequest interface {
6161 attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo )
6262 retryPolicy () RetryPolicy
6363 speculativeExecutionPolicy () SpeculativeExecutionPolicy
64- getQueryMetrics () * queryMetrics
64+ getQueryMetrics () queryMetrics
6565 getRoutingInfo () * queryRoutingInfo
6666 getKeyspaceFunc () func () string
6767 RetryableQuery
@@ -280,6 +280,8 @@ type queryOptions struct {
280280 // Protocol flag
281281 disableSkipMetadata bool
282282
283+ withQueryMetrics bool
284+
283285 customPayload map [string ][]byte
284286 prefetch float64
285287 rt RetryPolicy
@@ -330,14 +332,15 @@ func newQueryOptions(q *Query, ctx context.Context) *queryOptions {
330332 nowInSecondsValue : q .nowInSecondsValue ,
331333 keyspace : q .keyspace ,
332334 hostID : q .hostID ,
335+ withQueryMetrics : q .withQueryMetrics ,
333336 }
334337}
335338
336339type internalQuery struct {
337340 originalQuery * Query
338341 qryOpts * queryOptions
339342 pageState []byte
340- metrics * queryMetrics
343+ metrics queryMetrics
341344 conn * Conn
342345 consistency uint32
343346 session * Session
@@ -351,10 +354,16 @@ func newInternalQuery(q *Query, ctx context.Context) *internalQuery {
351354 newPageState = make ([]byte , len (pageState ))
352355 copy (newPageState , pageState )
353356 }
357+ var metrics queryMetrics
358+ if q .withQueryMetrics {
359+ metrics = & queryMetricsImpl {m : make (map [string ]* hostMetrics )}
360+ } else {
361+ metrics = nilQueryMetrics
362+ }
354363 return & internalQuery {
355364 originalQuery : q ,
356365 qryOpts : newQueryOptions (q , ctx ),
357- metrics : & queryMetrics { m : make ( map [ string ] * hostMetrics )} ,
366+ metrics : metrics ,
358367 consistency : uint32 (q .initialConsistency ),
359368 pageState : newPageState ,
360369 conn : nil ,
@@ -370,7 +379,7 @@ func (q *internalQuery) Attempts() int {
370379
371380func (q * internalQuery ) attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo ) {
372381 latency := end .Sub (start )
373- attempt , metricsForHost := q .metrics .attempt (1 , latency , host , q . qryOpts . observer != nil )
382+ attempt , metricsForHost := q .metrics .attempt (1 , latency , host )
374383
375384 if q .qryOpts .observer != nil {
376385 q .qryOpts .observer .ObserveQuery (q .qryOpts .context , ObservedQuery {
@@ -381,7 +390,7 @@ func (q *internalQuery) attempt(keyspace string, end, start time.Time, iter *Ite
381390 End : end ,
382391 Rows : iter .numRows ,
383392 Host : host ,
384- Metrics : metricsForHost ,
393+ Metrics : * metricsForHost ,
385394 Err : iter .err ,
386395 Attempt : attempt ,
387396 Query : q .originalQuery ,
@@ -457,7 +466,7 @@ func (q *internalQuery) IsIdempotent() bool {
457466 return q .qryOpts .idempotent
458467}
459468
460- func (q * internalQuery ) getQueryMetrics () * queryMetrics {
469+ func (q * internalQuery ) getQueryMetrics () queryMetrics {
461470 return q .metrics
462471}
463472
@@ -548,17 +557,23 @@ func newBatchOptions(b *Batch, ctx context.Context) *batchOptions {
548557type internalBatch struct {
549558 originalBatch * Batch
550559 batchOpts * batchOptions
551- metrics * queryMetrics
560+ metrics queryMetrics
552561 consistency uint32
553562 routingInfo * queryRoutingInfo
554563 session * Session
555564}
556565
557566func newInternalBatch (batch * Batch , ctx context.Context ) * internalBatch {
567+ var metrics queryMetrics
568+ if batch .withQueryMetrics {
569+ metrics = & queryMetricsImpl {m : make (map [string ]* hostMetrics )}
570+ } else {
571+ metrics = nilQueryMetrics
572+ }
558573 return & internalBatch {
559574 originalBatch : batch ,
560575 batchOpts : newBatchOptions (batch , ctx ),
561- metrics : & queryMetrics { m : make ( map [ string ] * hostMetrics )} ,
576+ metrics : metrics ,
562577 routingInfo : & queryRoutingInfo {},
563578 session : batch .session ,
564579 consistency : uint32 (batch .GetConsistency ()),
@@ -572,7 +587,7 @@ func (b *internalBatch) Attempts() int {
572587
573588func (b * internalBatch ) attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo ) {
574589 latency := end .Sub (start )
575- attempt , metricsForHost := b .metrics .attempt (1 , latency , host , b . batchOpts . observer != nil )
590+ attempt , metricsForHost := b .metrics .attempt (1 , latency , host )
576591
577592 if b .batchOpts .observer == nil {
578593 return
@@ -594,7 +609,7 @@ func (b *internalBatch) attempt(keyspace string, end, start time.Time, iter *Ite
594609 End : end ,
595610 // Rows not used in batch observations // TODO - might be able to support it when using BatchCAS
596611 Host : host ,
597- Metrics : metricsForHost ,
612+ Metrics : * metricsForHost ,
598613 Err : iter .err ,
599614 Attempt : attempt ,
600615 Batch : b .originalBatch ,
@@ -651,7 +666,7 @@ func (b *internalBatch) IsIdempotent() bool {
651666 return b .batchOpts .idempotent
652667}
653668
654- func (b * internalBatch ) getQueryMetrics () * queryMetrics {
669+ func (b * internalBatch ) getQueryMetrics () queryMetrics {
655670 return b .metrics
656671}
657672
0 commit comments