func SendIntakeV2Trace()

in pkg/tracegen/intake.go [35:100]


func SendIntakeV2Trace(ctx context.Context, cfg Config) (apm.TraceContext, EventStats, error) {
	if err := cfg.validate(); err != nil {
		return apm.TraceContext{}, EventStats{}, err
	}

	tracer, err := newTracer(cfg)
	if err != nil {
		return apm.TraceContext{}, EventStats{}, fmt.Errorf("failed to create tracer: %w", err)
	}
	defer tracer.Close()

	// set sample rate
	ts := apm.NewTraceState(apm.TraceStateEntry{
		Key: "es", Value: fmt.Sprintf("s:%.4g", cfg.sampleRate),
	})

	traceContext := apm.TraceContext{
		Trace:   cfg.traceID,
		Options: apm.TraceOptions(0).WithRecorded(true),
		State:   ts,
	}

	tx := tracer.StartTransactionOptions("parent-tx", "apmtool", apm.TransactionOptions{
		TraceContext: traceContext,
	})

	span := tx.StartSpanOptions("parent-span", "apmtool", apm.SpanOptions{
		Parent: tx.TraceContext(),
	})

	exit := tx.StartSpanOptions("exit-span", "apmtool", apm.SpanOptions{
		Parent:   span.TraceContext(),
		ExitSpan: true,
	})

	exit.Context.SetServiceTarget(apm.ServiceTargetSpanContext{
		Type: "service_type",
		Name: "service_name",
	})

	exit.Duration = 999 * time.Millisecond
	exit.Outcome = "failure"

	// error
	e := tracer.NewError(errors.New("timeout"))
	e.Culprit = "timeout"
	e.SetSpan(exit)
	e.Send()
	exit.End()

	span.Duration = time.Second
	span.Outcome = "success"
	span.End()
	tx.Duration = 2 * time.Second
	tx.Outcome = "success"
	tx.End()

	tracer.Flush(ctx.Done())
	tracerStats := tracer.Stats()
	stats := EventStats{
		ExceptionsSent: int(tracerStats.ErrorsSent),
		SpansSent:      int(tracerStats.SpansSent + tracerStats.TransactionsSent),
	}

	return tx.TraceContext(), stats, nil
}