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

     1  // Copyright 2024 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 prometheus
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  
    21  	"github.com/alibaba/ilogtail/pkg/logger"
    22  	"github.com/alibaba/ilogtail/pkg/models"
    23  	"github.com/alibaba/ilogtail/pkg/pipeline/extensions"
    24  	"github.com/alibaba/ilogtail/pkg/protocol"
    25  )
    26  
    27  const defaultSeriesLimit = 1000
    28  
    29  var errNilOrZeroGroupEvents = errors.New("nil or zero group events")
    30  
    31  type Option struct {
    32  	SeriesLimit int // config for prometheus encoder
    33  }
    34  
    35  func NewPromEncoder(seriesLimit int) extensions.Encoder {
    36  	return newPromEncoder(seriesLimit)
    37  }
    38  
    39  type Encoder struct {
    40  	SeriesLimit int
    41  }
    42  
    43  func newPromEncoder(seriesLimit int) *Encoder {
    44  	if seriesLimit <= 0 {
    45  		seriesLimit = defaultSeriesLimit
    46  	}
    47  
    48  	return &Encoder{
    49  		SeriesLimit: seriesLimit,
    50  	}
    51  }
    52  
    53  func (p *Encoder) EncodeV1(logGroups *protocol.LogGroup) ([][]byte, error) {
    54  	// TODO implement me
    55  	return nil, nil
    56  }
    57  
    58  func (p *Encoder) EncodeBatchV1(logGroups []*protocol.LogGroup) ([][]byte, error) {
    59  	// TODO implement me
    60  	return nil, nil
    61  }
    62  
    63  func (p *Encoder) EncodeV2(groupEvents *models.PipelineGroupEvents) ([][]byte, error) {
    64  	if groupEvents == nil || len(groupEvents.Events) == 0 {
    65  		return nil, errNilOrZeroGroupEvents
    66  	}
    67  
    68  	var res [][]byte
    69  
    70  	wr := getWriteRequest(p.SeriesLimit)
    71  	defer putWriteRequest(wr)
    72  
    73  	for _, event := range groupEvents.Events {
    74  		if event == nil {
    75  			logger.Debugf(context.Background(), "nil event")
    76  			continue
    77  		}
    78  
    79  		if event.GetType() != models.EventTypeMetric {
    80  			logger.Debugf(context.Background(), "event type (%s) not metric", event.GetName())
    81  			continue
    82  		}
    83  
    84  		metricEvent, ok := event.(*models.Metric)
    85  		if !ok {
    86  			logger.Debugf(context.Background(), "assert metric event type (%s) failed", event.GetName())
    87  			continue
    88  		}
    89  
    90  		wr.Timeseries = append(wr.Timeseries, genPromRemoteWriteTimeseries(metricEvent))
    91  		if len(wr.Timeseries) >= p.SeriesLimit {
    92  			res = append(res, marshalBatchTimeseriesData(wr))
    93  			wr.Timeseries = wr.Timeseries[:0]
    94  		}
    95  	}
    96  
    97  	if len(wr.Timeseries) > 0 {
    98  		res = append(res, marshalBatchTimeseriesData(wr))
    99  		wr.Timeseries = wr.Timeseries[:0]
   100  	}
   101  
   102  	return res, nil
   103  }
   104  
   105  func (p *Encoder) EncodeBatchV2(groupEventsSlice []*models.PipelineGroupEvents) ([][]byte, error) {
   106  	if len(groupEventsSlice) == 0 {
   107  		return nil, errNilOrZeroGroupEvents
   108  	}
   109  
   110  	var res [][]byte
   111  
   112  	for _, groupEvents := range groupEventsSlice {
   113  		bytes, err := p.EncodeV2(groupEvents)
   114  		if err != nil {
   115  			continue
   116  		}
   117  
   118  		res = append(res, bytes...)
   119  	}
   120  
   121  	if res == nil {
   122  		return nil, errNilOrZeroGroupEvents
   123  	}
   124  
   125  	return res, nil
   126  }