package tool

import (
	"context"
	"fmt"

	"github.com/opentracing/opentracing-go/log"

	"github.com/opentracing/opentracing-go"
)

// Trace 链路追踪（无需操作名）
func Trace(ctx context.Context, name string) (opentracing.Span, context.Context) {
	// 链路跟踪
	var span opentracing.Span
	span, ctx = opentracing.StartSpanFromContext(ctx, name)
	span.SetTag(`method`, GetCallFunc(2))

	return span, ctx
}

// TraceAuto 链路追踪（无需操作名）
func TraceAuto(ctx context.Context) (opentracing.Span, context.Context) {
	var span opentracing.Span

	caller := NewCurrentCaller().CallFunc
	span, ctx = Trace(ctx, fmt.Sprintf("%s.%s", caller.Package, caller.Name))
	span.LogFields(
		log.String(`file_path`, caller.Path),
		log.Int(`file_line`, caller.Line),
	)

	return span, ctx
}
