1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package sumologicexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sumologicexporter"
- import (
- "bytes"
- "compress/flate"
- "compress/gzip"
- "fmt"
- "io"
- )
- type compressor struct {
- format CompressEncodingType
- writer encoder
- buf bytes.Buffer
- }
- type encoder interface {
- io.WriteCloser
- Reset(dst io.Writer)
- }
- // newCompressor takes encoding format and returns compressor struct and error eventually
- func newCompressor(format CompressEncodingType) (compressor, error) {
- var (
- writer encoder
- err error
- )
- switch format {
- case GZIPCompression:
- writer = gzip.NewWriter(io.Discard)
- case DeflateCompression:
- writer, err = flate.NewWriter(io.Discard, flate.BestSpeed)
- if err != nil {
- return compressor{}, err
- }
- case NoCompression:
- writer = nil
- default:
- return compressor{}, fmt.Errorf("invalid format: %s", format)
- }
- return compressor{
- format: format,
- writer: writer,
- }, nil
- }
- // compress takes a reader with uncompressed data and returns
- // a reader with the same data compressed using c.writer
- func (c *compressor) compress(data io.Reader) (io.Reader, error) {
- if c.writer == nil {
- return data, nil
- }
- var dataBytes bytes.Buffer
- if _, err := dataBytes.ReadFrom(data); err != nil {
- return nil, err
- }
- // Reset c.buf to start with empty message
- c.buf.Reset()
- c.writer.Reset(&c.buf)
- if _, err := c.writer.Write(dataBytes.Bytes()); err != nil {
- return nil, err
- }
- if err := c.writer.Close(); err != nil {
- return nil, err
- }
- return bytes.NewReader(c.buf.Bytes()), nil
- }
|