package otlptraces
import (
)
const DefaultEndpoint = "http://localhost:4318/v1/traces"
const (
headerContentType = "Content-Type"
headerContentEncoding = "Content-Encoding"
contentTypeProtobuf = "application/x-protobuf"
contentEncodingGzip = "gzip"
maxErrorBodySize = 4 << 10
)
type Exporter struct {
Client httpclient.Client
Endpoint string
Header http.Header
Resource []traces.Attr
SchemaURL string
DisableGzip bool
}
func ( *Exporter) ( context.Context, []traces.Record) error {
if len() == 0 {
return nil
}
, := 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(),
}
}