Trace don't show up if entry point isn't http func

If we have http handler as entry point, everything is okay, and we have full trace, but if it in workers (for example), we have only transaction name and that's all.

I think that I misunderstand official docs part about transactions and spans, and it would be great if anyone could help me

And also I have another few questions:

  1. For what purpose we need spanType as third argument in startSpan() call (haven't find anything about it in docs).
  2. How to make available transaction result of worker running? In case that we have http call it's clear - we got http code, but what to do in this case? Should I write transaction.Result by myself and write errors like in bottom example?

HTTP handler example:

package main

import (
	"context"
	"net/http"
	"go.elastic.co/apm"
)

func handler(r *http.Request, w http.ResponseWriter) {
	example(r.Context())
	
	//do something else
}

func example(ctx context.Context) {
	span, apmCtx := apm.StartSpan(ctx, "example", "")
	defer span.End()

	//do something
}

Example of worker:

package main

import (
	"context"

	"go.elastic.co/apm"
)

func worker(ctx context.Context, msgs <-chan amqp.Delivery) error {
	for m := range msgs {
		transaction := app.Tracer().StartTransaction("my-worker-name", "worker")
		transactionCtx := apm.ContextWithTransaction(ctx, transaction)

		err := example(transactionCtx)
		if err != nil {
			//do something
		}
	} //range over messages from RabbitMQ
}

func example(ctx context.Context) error {
	span, apmCtx := apm.StartSpan(ctx, "example", "")
	defer span.End()

	//do something
}

In result we got next things: If example() func calling from http handler, we see it in trace, otherwise - we don't (in our case, if we call it in worker)

Error send example:

apmErr := apm.CaptureError(transactionCtx, err)
apmErr.Send()

@gvencadze welcome to the forum!

  1. For what purpose we need spanType as third argument in startSpan() call (haven't find anything about it in docs).

You should always set a non-empty span type. This is used for the "Time spent by span type" chart in Kibana, and for filtering. There's a bug in the agent (and server) which means that if the span type is empty, those spans don't show up in Kibana. We'll have this fixed in a future release, but in the mean time please set it to something, e.g. "custom".

  1. How to make available transaction result of worker running? In case that we have http call it's clear - we got http code, but what to do in this case? Should I write transaction.Result by myself and write errors like in bottom example?

Yes, that sounds right to me. Just keep in mind that transaction.Result should have low cardinality. e.g. it would be fine to set Result to a handful of different result strings, like "OK", "Failed", "Unknown". It wouldn't be a good idea to set it to something more dynamic, like "Processed message #123".

Thank you for your answer, I'll try and write here about results

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.