// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package log

import (
	
	
	

	
)

// ErrExporterShutdown is returned if Export is called after an
// Exporter has been Shutdown.
var ErrExporterShutdown = errors.New("exporter is shutdown")

// Exporter handles the delivery of log records to external receivers.
type Exporter interface {
	// DO NOT CHANGE: any modification will not be backwards compatible and
	// must never be done outside of a new major release.

	// Export transmits log records to a receiver.
	//
	// The deadline or cancellation of the passed context must be honored. An
	// appropriate error should be returned in these situations.
	//
	// All retry logic must be contained in this function. The SDK does not
	// implement any retry logic. All errors returned by this function are
	// considered unrecoverable and will be reported to a configured error
	// Handler.
	//
	// Implementations must not retain the records slice.
	//
	// Before modifying a Record, the implementation must use Record.Clone
	// to create a copy that shares no state with the original.
	//
	// Export should return [ErrExporterShutdown] if called after Shutdown.
	//
	// Export should never be called concurrently with other Export calls.
	// However, it may be called concurrently with other methods.
	Export(ctx context.Context, records []Record) error
	// DO NOT CHANGE: any modification will not be backwards compatible and
	// must never be done outside of a new major release.

	// Shutdown is called when the SDK shuts down. Any cleanup or release of
	// resources held by the exporter should be done in this call.
	//
	// The deadline or cancellation of the passed context must be honored. An
	// appropriate error should be returned in these situations.
	//
	// After Shutdown is called, calls to Shutdown or ForceFlush should perform
	// no operation and return nil. Calls to Export should return
	// [ErrExporterShutdown].
	//
	// Shutdown may be called concurrently with itself or with other methods.
	Shutdown(ctx context.Context) error
	// DO NOT CHANGE: any modification will not be backwards compatible and
	// must never be done outside of a new major release.

	// ForceFlush flushes any log records held by the Exporter.
	//
	// The deadline or cancellation of the passed context must be honored. An
	// appropriate error should be returned in these situations.
	//
	// ForceFlush may be called concurrently with itself or with other methods.
	ForceFlush(ctx context.Context) error
	// DO NOT CHANGE: any modification will not be backwards compatible and
	// must never be done outside of a new major release.
}

var defaultNoopExporter = &noopExporter{}

type noopExporter struct{}

func (noopExporter) (context.Context, []Record) error { return nil }

func (noopExporter) (context.Context) error { return nil }

func (noopExporter) (context.Context) error { return nil }

func shutdownExporter( context.Context,  Exporter) error {
	 := .ForceFlush()
	return errors.Join(, .Shutdown())
}

// chunkExporter wraps an Exporter's Export method so it is called with
// appropriately sized export payloads. Any payload larger than a defined size
// is chunked into smaller payloads and exported sequentially.
type chunkExporter struct {
	Exporter

	// size is the maximum batch size exported.
	size int
}

// newChunkExporter wraps exporter. Record payloads passed to Export are
// chunked so that they do not exceed size. If size is less than or equal to 0,
// exporter is returned directly.
func newChunkExporter( Exporter,  int) Exporter {
	if  <= 0 {
		return 
	}
	return &chunkExporter{Exporter: , size: }
}

// Export exports records in chunks no larger than c.size.
func ( chunkExporter) ( context.Context,  []Record) error {
	 := len()
	var  []error
	for ,  := 0, min(.size, );  < ; ,  = +.size, min(+.size, ) {
		if  := .Err();  != nil {
			return errors.Join(append(, )...)
		}
		if  := .Exporter.Export(, [:]);  != nil {
			 = append(, )
		}
		if  := .Err();  != nil {
			return errors.Join(append(, )...)
		}
	}
	return errors.Join(...)
}

// timeoutExporter wraps an Exporter and adds a timeout to the context of any
// call to Export.
type timeoutExporter struct {
	Exporter

	// timeout is the maximum time an export is attempted.
	timeout time.Duration
}

// newTimeoutExporter wraps exporter with an Exporter that limits the lifetime
// of the context passed to Export to the timeout value. If timeout is less than
// or equal to zero, exporter will be returned directly.
func newTimeoutExporter( Exporter,  time.Duration) Exporter {
	if  <= 0 {
		return 
	}
	return &timeoutExporter{Exporter: , timeout: }
}

// Export sets a timeout on ctx before calling the Exporter that e wraps.
func ( *timeoutExporter) ( context.Context,  []Record) error {
	// This only used by the batch processor, and it takes processor timeout config.
	// Thus, the error message points to the processor. So users know they should adjust the processor timeout.
	,  := context.WithTimeoutCause(, .timeout, errors.New("processor export timeout"))
	defer ()
	return .Exporter.Export(, )
}

// metricsExporter wraps an Exporter to record log processing metrics
// just before calling the wrapped exporter.
type metricsExporter struct {
	Exporter
	inst *observ.BLP
}

// newMetricsExporter creates a metricsExporter that wraps the given exporter.
func newMetricsExporter( Exporter,  *observ.BLP) Exporter {
	return &metricsExporter{
		Exporter: ,
		inst:     ,
	}
}

// Export records the number of log records as a metric, then forwards them to
// the wrapped Exporter. As specified, the error returned by the wrapped
// exporter is not considered because it is to be measured by the exporter.
func ( *metricsExporter) ( context.Context,  []Record) error {
	if .inst != nil {
		.inst.Processed(, int64(len()))
	}
	return .Exporter.Export(, )
}