@@ -15,18 +15,15 @@ type Interceptor struct {
1515}
1616
1717// metricContext holds metric collection state in context.
18- //
19- //nolint:unused // Will be used in Phase 8+
2018type metricContext struct {
19+ sessionID string
2120 statementID string
2221 startTime time.Time
2322 tags map [string ]interface {}
2423}
2524
26- //nolint:unused // Will be used in Phase 8+
2725type contextKey int
2826
29- //nolint:unused // Will be used in Phase 8+
3027const metricContextKey contextKey = 0
3128
3229// newInterceptor creates a new telemetry interceptor.
@@ -38,32 +35,28 @@ func newInterceptor(aggregator *metricsAggregator, enabled bool) *Interceptor {
3835}
3936
4037// withMetricContext adds metric context to the context.
41- //
42- //nolint:unused // Will be used in Phase 8+
4338func withMetricContext (ctx context.Context , mc * metricContext ) context.Context {
4439 return context .WithValue (ctx , metricContextKey , mc )
4540}
4641
4742// getMetricContext retrieves metric context from the context.
48- //
49- //nolint:unused // Will be used in Phase 8+
5043func getMetricContext (ctx context.Context ) * metricContext {
5144 if mc , ok := ctx .Value (metricContextKey ).(* metricContext ); ok {
5245 return mc
5346 }
5447 return nil
5548}
5649
57- // beforeExecute is called before statement execution.
50+ // BeforeExecute is called before statement execution.
5851// Returns a new context with metric tracking attached.
59- //
60- //nolint:unused // Will be used in Phase 8+
61- func (i * Interceptor ) beforeExecute (ctx context.Context , statementID string ) context.Context {
52+ // Exported for use by the driver package.
53+ func (i * Interceptor ) BeforeExecute (ctx context.Context , sessionID string , statementID string ) context.Context {
6254 if ! i .enabled {
6355 return ctx
6456 }
6557
6658 mc := & metricContext {
59+ sessionID : sessionID ,
6760 statementID : statementID ,
6861 startTime : time .Now (),
6962 tags : make (map [string ]interface {}),
@@ -72,11 +65,28 @@ func (i *Interceptor) beforeExecute(ctx context.Context, statementID string) con
7265 return withMetricContext (ctx , mc )
7366}
7467
75- // afterExecute is called after statement execution.
68+ // BeforeExecuteWithTime is called before statement execution with a custom start time.
69+ // This is useful when the statement ID is not known until after execution starts.
70+ // Exported for use by the driver package.
71+ func (i * Interceptor ) BeforeExecuteWithTime (ctx context.Context , sessionID string , statementID string , startTime time.Time ) context.Context {
72+ if ! i .enabled {
73+ return ctx
74+ }
75+
76+ mc := & metricContext {
77+ sessionID : sessionID ,
78+ statementID : statementID ,
79+ startTime : startTime ,
80+ tags : make (map [string ]interface {}),
81+ }
82+
83+ return withMetricContext (ctx , mc )
84+ }
85+
86+ // AfterExecute is called after statement execution.
7687// Records the metric with timing and error information.
77- //
78- //nolint:unused // Will be used in Phase 8+
79- func (i * Interceptor ) afterExecute (ctx context.Context , err error ) {
88+ // Exported for use by the driver package.
89+ func (i * Interceptor ) AfterExecute (ctx context.Context , err error ) {
8090 if ! i .enabled {
8191 return
8292 }
@@ -96,6 +106,7 @@ func (i *Interceptor) afterExecute(ctx context.Context, err error) {
96106 metric := & telemetryMetric {
97107 metricType : "statement" ,
98108 timestamp : mc .startTime ,
109+ sessionID : mc .sessionID ,
99110 statementID : mc .statementID ,
100111 latencyMs : time .Since (mc .startTime ).Milliseconds (),
101112 tags : mc .tags ,
@@ -109,10 +120,9 @@ func (i *Interceptor) afterExecute(ctx context.Context, err error) {
109120 i .aggregator .recordMetric (ctx , metric )
110121}
111122
112- // addTag adds a tag to the current metric context.
113- //
114- //nolint:unused // Will be used in Phase 8+
115- func (i * Interceptor ) addTag (ctx context.Context , key string , value interface {}) {
123+ // AddTag adds a tag to the current metric context.
124+ // Exported for use by the driver package.
125+ func (i * Interceptor ) AddTag (ctx context.Context , key string , value interface {}) {
116126 if ! i .enabled {
117127 return
118128 }
@@ -146,10 +156,9 @@ func (i *Interceptor) recordConnection(ctx context.Context, tags map[string]inte
146156 i .aggregator .recordMetric (ctx , metric )
147157}
148158
149- // completeStatement marks a statement as complete and flushes aggregated metrics.
150- //
151- //nolint:unused // Will be used in Phase 8+
152- func (i * Interceptor ) completeStatement (ctx context.Context , statementID string , failed bool ) {
159+ // CompleteStatement marks a statement as complete and flushes aggregated metrics.
160+ // Exported for use by the driver package.
161+ func (i * Interceptor ) CompleteStatement (ctx context.Context , statementID string , failed bool ) {
153162 if ! i .enabled {
154163 return
155164 }
0 commit comments