github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/decoder/decoder.go (about)

     1  // Copyright 2021 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package decoder
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/alibaba/ilogtail/pkg/pipeline/extensions"
    23  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/common"
    24  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/influxdb"
    25  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/opentelemetry"
    26  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/prometheus"
    27  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/pyroscope"
    28  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/raw"
    29  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/sls"
    30  	"github.com/alibaba/ilogtail/pkg/protocol/decoder/statsd"
    31  )
    32  
    33  type Option struct {
    34  	FieldsExtend      bool
    35  	DisableUncompress bool
    36  	AllowUnsafeMode   bool
    37  }
    38  
    39  // GetDecoder return a new decoder for specific format
    40  func GetDecoder(format string) (extensions.Decoder, error) {
    41  	return GetDecoderWithOptions(format, Option{})
    42  }
    43  
    44  func GetDecoderWithOptions(format string, option Option) (extensions.Decoder, error) {
    45  	switch strings.TrimSpace(strings.ToLower(format)) {
    46  	case common.ProtocolSLS:
    47  		return &sls.Decoder{}, nil
    48  	case common.ProtocolPrometheus:
    49  		return &prometheus.Decoder{AllowUnsafeMode: option.AllowUnsafeMode}, nil
    50  	case common.ProtocolInflux, common.ProtocolInfluxdb:
    51  		return &influxdb.Decoder{FieldsExtend: option.FieldsExtend}, nil
    52  	case common.ProtocolStatsd:
    53  		return &statsd.Decoder{
    54  			Time: time.Now(),
    55  		}, nil
    56  	case common.ProtocolOTLPLogV1:
    57  		return &opentelemetry.Decoder{Format: common.ProtocolOTLPLogV1}, nil
    58  	case common.ProtocolOTLPMetricV1:
    59  		return &opentelemetry.Decoder{Format: common.ProtocolOTLPMetricV1}, nil
    60  	case common.ProtocolOTLPTraceV1:
    61  		return &opentelemetry.Decoder{Format: common.ProtocolOTLPTraceV1}, nil
    62  	case common.ProtocolRaw:
    63  		return &raw.Decoder{DisableUncompress: option.DisableUncompress}, nil
    64  
    65  	case common.ProtocolPyroscope:
    66  		return &pyroscope.Decoder{}, nil
    67  	default:
    68  		return nil, fmt.Errorf("not supported format: %s", format)
    69  	}
    70  }