package otlptraces

import (
	
	
	
	
	
	

	

	

	
)

// DefaultEndpoint is the default OTLP/HTTP traces endpoint URL.
const DefaultEndpoint = "http://localhost:4318/v1/traces"

const (
	headerContentType     = "Content-Type"
	headerContentEncoding = "Content-Encoding"
	contentTypeProtobuf   = "application/x-protobuf"
	contentEncodingGzip   = "gzip"
	maxErrorBodySize      = 4 << 10
)

// Exporter sends span batches to an OTLP/HTTP traces endpoint using protobuf
// encoding. It is stateless and safe for concurrent use.
//
// Each [Exporter.Export] call performs a single HTTP POST request; compose
// retries and batching around Exporter as needed.
type Exporter struct {
	// Client specifies the HTTP client to use.
	// Defaults to [httpclient.DefaultClient].
	Client httpclient.Client

	// Endpoint is the full OTLP/HTTP traces URL.
	// Defaults to [DefaultEndpoint].
	Endpoint string

	// Header specifies additional HTTP headers added to every request
	// (such as Authorization or api-key).
	Header http.Header

	// Resource specifies resource attributes describing the process
	// producing the spans (such as service.name and service.version).
	Resource []traces.Attr

	// SchemaURL optionally specifies the OpenTelemetry semantic conventions
	// schema URL attached to exported ResourceSpans and ScopeSpans.
	SchemaURL string

	// DisableGzip disables gzip compression of the OTLP protobuf request body.
	// By default, request bodies are gzip-compressed with Content-Encoding: gzip.
	DisableGzip bool
}

// Export encodes batch as OTLP protobuf and POSTs it once to
// [Exporter.Endpoint]. An empty batch is a no-op and returns nil.
func ( *Exporter) ( context.Context,  []traces.Record) error {
	if len() == 0 {
		return nil
	}

	// TracesData is wire-identical to ExportTraceServiceRequest (both have
	// "repeated ResourceSpans resource_spans = 1"). Using it avoids
	// importing collector/trace/v1, which pulls in gRPC and grpc-gateway.
	,  := proto.Marshal(.encode())
	if  != nil {
		return fmt.Errorf("otlptraces: marshal: %w", )
	}
	if !.DisableGzip {
		var  bytes.Buffer
		 := gzip.NewWriter(&)
		if ,  := .Write();  != nil {
			return fmt.Errorf("otlptraces: gzip: %w", )
		}
		if  := .Close();  != nil {
			return fmt.Errorf("otlptraces: gzip close: %w", )
		}
		 = .Bytes()
	}

	 := .Endpoint
	if  == "" {
		 = DefaultEndpoint
	}

	,  := http.NewRequestWithContext(
		,
		http.MethodPost,
		,
		bytes.NewReader(),
	)
	if  != nil {
		return fmt.Errorf("otlptraces: %w", )
	}
	for ,  := range .Header {
		for ,  := range  {
			.Header.Add(, )
		}
	}
	.Header.Set(headerContentType, contentTypeProtobuf)
	if !.DisableGzip {
		.Header.Set(headerContentEncoding, contentEncodingGzip)
	}

	 := .Client
	if  == nil {
		 = httpclient.DefaultClient()
	}
	,  := .Do()
	if  != nil {
		return fmt.Errorf("otlptraces: %w", )
	}
	defer func() { _ = .Body.Close() }()

	,  := io.ReadAll(io.LimitReader(.Body, maxErrorBodySize))
	if .StatusCode/100 == 2 {
		return nil
	}

	return &HTTPError{
		StatusCode: .StatusCode,
		Header:     .Header,
		Body:       string(),
	}
}