XSAM/otelsql

[feature] Automatic aggregation of traces without parent span

AtlanCI opened this issue · 2 comments

Question

In the latest version if no span is given before the sql operation is executed, then the span of the sql execution operation will be scattered.

example :

Modify the query function in the example package to the following

func query(db *sql.DB) error {
	// Create a span
	//tracer := otel.GetTracerProvider()
	//ctx, span := tracer.Tracer(instrumentationName).Start(context.Background(), "example")
	//defer span.End()
	ctx := context.Background()
	// Make a query
	rows, err := db.QueryContext(ctx, `SELECT CURRENT_TIMESTAMP()`)
	if err != nil {
		return err
	}
	defer rows.Close()

	var currentTime time.Time
	for rows.Next() {
		err = rows.Scan(&currentTime)
		if err != nil {
			return err
		}
	}
	fmt.Println(currentTime)
	return nil
}

result

image

Requirement

There is no way to realize that if the upper layer does not pass the parent span, I become the parent span myself. To achieve automatic aggregation of scattered spans.

XSAM commented

sql.rows is not a child of sql.conn.query, or you will see weird spans in which the child span starts where the parent span ends.

# A is the parent of B

A span: start --- end
                     |
B span:               start --- end

So, there is no need to aggregate these spans even though they are the root span.

I've thought about it for a while and if the user doesn't provide the parent span there is no way to aggregate perfectly. The standard library sql is not a tree model, somewhat similar to a forest.
Users do not execute sql through the same portal

I will close the issue