@@ -334,14 +334,15 @@ func newQueryOptions(q *Query, ctx context.Context) *queryOptions {
334334}
335335
336336type internalQuery struct {
337- originalQuery * Query
338- qryOpts * queryOptions
339- pageState []byte
340- metrics * queryMetrics
341- conn * Conn
342- consistency uint32
343- session * Session
344- routingInfo * queryRoutingInfo
337+ originalQuery * Query
338+ qryOpts * queryOptions
339+ pageState []byte
340+ conn * Conn
341+ consistency uint32
342+ session * Session
343+ routingInfo * queryRoutingInfo
344+ metrics * queryMetrics
345+ hostMetricsManager hostMetricsManager
345346}
346347
347348func newInternalQuery (q * Query , ctx context.Context ) * internalQuery {
@@ -351,15 +352,22 @@ func newInternalQuery(q *Query, ctx context.Context) *internalQuery {
351352 newPageState = make ([]byte , len (pageState ))
352353 copy (newPageState , pageState )
353354 }
355+ var hostMetricsMgr hostMetricsManager
356+ if q .observer != nil {
357+ hostMetricsMgr = newHostMetricsManager ()
358+ } else {
359+ hostMetricsMgr = emptyHostMetricsManager
360+ }
354361 return & internalQuery {
355- originalQuery : q ,
356- qryOpts : newQueryOptions (q , ctx ),
357- metrics : & queryMetrics {m : make (map [string ]* hostMetrics )},
358- consistency : uint32 (q .initialConsistency ),
359- pageState : newPageState ,
360- conn : nil ,
361- session : q .session ,
362- routingInfo : & queryRoutingInfo {},
362+ originalQuery : q ,
363+ qryOpts : newQueryOptions (q , ctx ),
364+ metrics : & queryMetrics {},
365+ hostMetricsManager : hostMetricsMgr ,
366+ consistency : uint32 (q .initialConsistency ),
367+ pageState : newPageState ,
368+ conn : nil ,
369+ session : q .session ,
370+ routingInfo : & queryRoutingInfo {},
363371 }
364372}
365373
@@ -370,9 +378,10 @@ func (q *internalQuery) Attempts() int {
370378
371379func (q * internalQuery ) attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo ) {
372380 latency := end .Sub (start )
373- attempt , metricsForHost := q .metrics .attempt (1 , latency , host , q . qryOpts . observer != nil )
381+ attempt := q .metrics .attempt (latency )
374382
375383 if q .qryOpts .observer != nil {
384+ metricsForHost := q .hostMetricsManager .attempt (latency , host )
376385 q .qryOpts .observer .ObserveQuery (q .qryOpts .context , ObservedQuery {
377386 Keyspace : keyspace ,
378387 Statement : q .qryOpts .stmt ,
@@ -546,22 +555,30 @@ func newBatchOptions(b *Batch, ctx context.Context) *batchOptions {
546555}
547556
548557type internalBatch struct {
549- originalBatch * Batch
550- batchOpts * batchOptions
551- metrics * queryMetrics
552- consistency uint32
553- routingInfo * queryRoutingInfo
554- session * Session
558+ originalBatch * Batch
559+ batchOpts * batchOptions
560+ consistency uint32
561+ routingInfo * queryRoutingInfo
562+ session * Session
563+ metrics * queryMetrics
564+ hostMetricsManager hostMetricsManager
555565}
556566
557567func newInternalBatch (batch * Batch , ctx context.Context ) * internalBatch {
568+ var hostMetricsMgr hostMetricsManager
569+ if batch .observer != nil {
570+ hostMetricsMgr = newHostMetricsManager ()
571+ } else {
572+ hostMetricsMgr = emptyHostMetricsManager
573+ }
558574 return & internalBatch {
559- originalBatch : batch ,
560- batchOpts : newBatchOptions (batch , ctx ),
561- metrics : & queryMetrics {m : make (map [string ]* hostMetrics )},
562- routingInfo : & queryRoutingInfo {},
563- session : batch .session ,
564- consistency : uint32 (batch .GetConsistency ()),
575+ originalBatch : batch ,
576+ batchOpts : newBatchOptions (batch , ctx ),
577+ routingInfo : & queryRoutingInfo {},
578+ session : batch .session ,
579+ consistency : uint32 (batch .GetConsistency ()),
580+ metrics : & queryMetrics {},
581+ hostMetricsManager : hostMetricsMgr ,
565582 }
566583}
567584
@@ -572,12 +589,14 @@ func (b *internalBatch) Attempts() int {
572589
573590func (b * internalBatch ) attempt (keyspace string , end , start time.Time , iter * Iter , host * HostInfo ) {
574591 latency := end .Sub (start )
575- attempt , metricsForHost := b .metrics .attempt (1 , latency , host , b . batchOpts . observer != nil )
592+ attempt := b .metrics .attempt (latency )
576593
577594 if b .batchOpts .observer == nil {
578595 return
579596 }
580597
598+ metricsForHost := b .hostMetricsManager .attempt (latency , host )
599+
581600 statements := make ([]string , len (b .batchOpts .entries ))
582601 values := make ([][]interface {}, len (b .batchOpts .entries ))
583602
0 commit comments