Skip to content

Allow Custom Span Name #40

@Quaqmre

Description

@Quaqmre

Feature Request: Allow Custom Span Name Generation in before Hook

I have implemented a change that allows dynamic span name generation in the before hook of otelPlugin.

Currently, before sets spanName statically:

func (p *otelPlugin) before(spanName string) gormHookFunc {
    return func(tx *gorm.DB) {
        tx.Statement.Context, _ = p.tracer.Start(tx.Statement.Context, spanName, trace.WithSpanKind(trace.SpanKindClient))
    }
}

I modified it to accept a function parameter for dynamic span naming:

func (p *otelPlugin) before(spanNameFunc func(string) string) gormHookFunc {
    return func(tx *gorm.DB) {
        spanName := spanNameFunc(tx.Statement.Table) // Example usage
        tx.Statement.Context, _ = p.tracer.Start(tx.Statement.Context, spanName, trace.WithSpanKind(trace.SpanKindClient))
    }
}

This provides more flexibility by allowing users to customize span names based on runtime context.

Motivation
Enables dynamic span naming based on table names, operations, or other context-specific factors.
Improves trace readability and debugging.
Such as i want to set name as caller method name.
Provides a more flexible API for observability use cases.
Next Steps
If this feature is accepted, I would like to open a PR with my implementation. Please let me know if this approach aligns with the project's goals.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions