Skip to content

Commit bae0836

Browse files
committed
Add sessionID support and BeforeExecuteWithTime to telemetry
- Add sessionID field to metricContext struct - Update BeforeExecute to accept sessionID parameter - Add BeforeExecuteWithTime method for custom start times - Update connection.go to pass sessionID in BeforeExecute call This enables proper session tracking in telemetry and allows capturing accurate execution times by providing a custom start time.
1 parent a027510 commit bae0836

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
200200
var statementID string
201201
if c.telemetry != nil && exStmtResp != nil && exStmtResp.OperationHandle != nil && exStmtResp.OperationHandle.OperationId != nil {
202202
statementID = client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID)
203-
ctx = c.telemetry.BeforeExecute(ctx, statementID)
203+
ctx = c.telemetry.BeforeExecute(ctx, c.id, statementID)
204204
defer func() {
205205
c.telemetry.AfterExecute(ctx, err)
206206
c.telemetry.CompleteStatement(ctx, statementID, err != nil)

telemetry/interceptor.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Interceptor struct {
1616

1717
// metricContext holds metric collection state in context.
1818
type metricContext struct {
19+
sessionID string
1920
statementID string
2021
startTime time.Time
2122
tags map[string]interface{}
@@ -49,12 +50,13 @@ func getMetricContext(ctx context.Context) *metricContext {
4950
// BeforeExecute is called before statement execution.
5051
// Returns a new context with metric tracking attached.
5152
// Exported for use by the driver package.
52-
func (i *Interceptor) BeforeExecute(ctx context.Context, statementID string) context.Context {
53+
func (i *Interceptor) BeforeExecute(ctx context.Context, sessionID string, statementID string) context.Context {
5354
if !i.enabled {
5455
return ctx
5556
}
5657

5758
mc := &metricContext{
59+
sessionID: sessionID,
5860
statementID: statementID,
5961
startTime: time.Now(),
6062
tags: make(map[string]interface{}),
@@ -63,6 +65,24 @@ func (i *Interceptor) BeforeExecute(ctx context.Context, statementID string) con
6365
return withMetricContext(ctx, mc)
6466
}
6567

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+
6686
// AfterExecute is called after statement execution.
6787
// Records the metric with timing and error information.
6888
// Exported for use by the driver package.
@@ -86,6 +106,7 @@ func (i *Interceptor) AfterExecute(ctx context.Context, err error) {
86106
metric := &telemetryMetric{
87107
metricType: "statement",
88108
timestamp: mc.startTime,
109+
sessionID: mc.sessionID,
89110
statementID: mc.statementID,
90111
latencyMs: time.Since(mc.startTime).Milliseconds(),
91112
tags: mc.tags,

0 commit comments

Comments
 (0)