Skip to content

A way to get the otel span inside the worker func #7

@scriptnull

Description

@scriptnull

We have something like

	someWorker, outputQueue, _ := work.NewTransformer(
		"this does some work",
		intputQueue,
		func(in Input) (*Output, error) {
			return doSomething(in)
		},
		transformer.WithErrorQueue(errQueue),
		transformer.WithWorkerPoolSize(conf.Parallelism),
		transformer.WithSpanName("some work"),
	)

I would like to have a way to access the OTEL span inside the doSomething function, so that I can attach some span metrics to it. My proposal would be to achieve this via context package with something like this

	someWorker, outputQueue, _ := work.NewTransformer(
		"this does some work",
		intputQueue,
		func(ctx context.Context, in Input) (*Output, error) {
			return doSomething(ctx, in)
		},
		transformer.WithErrorQueue(errQueue),
		transformer.WithWorkerPoolSize(conf.Parallelism),
		transformer.WithSpanName("some work"),
	)

In doSomething we get the tracer like

func doSomething(ctx context.Context, in Input) (*Output, error) {
    span := trace.SpanFromContext(ctx)
    // attach attributes and events to span
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions