-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ext_default_encoder with prometheus encoder (#1704)
* feat: add encoder extension interface in pkg/pipeline/extensions * feat: add prometheus encoder whitch implements extension.Encoder interface * feat: add ext_default_encoder extension plugin * refactor: rename package name, var name of encoder * docs: add docs for ext_default_encoder & update docs of other extensions * docs: add ext_default_encoder doc in docs/cn/plugins/overview.md * fix: fix build failed for package referenece * chore: update pkg/go.mod to solve lint failed * fix: fix ci-lint error
- Loading branch information
Showing
17 changed files
with
1,243 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 扩展 | ||
|
||
扩展插件用于对其它插件能力的补充(e.g. 鉴权、编解码、熔断、限流……) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# DefaultEncoder Encoder扩展 | ||
|
||
## 简介 | ||
|
||
[ext_default_encoder](https://github.com/alibaba/ilogtail/blob/main/plugins/extension/default_encoder/default_encoder.go) | ||
扩展,实现了 [Encoder](https://github.com/alibaba/ilogtail/blob/main/pkg/pipeline/extensions/encoder.go) 接口,可以用在 | ||
`flusher_http` 等插件中用于序列化不同的协议数据。 | ||
|
||
## 版本 | ||
|
||
[Alpha](../stability-level.md) | ||
|
||
## 配置参数 | ||
|
||
| 参数 | 类型 | 是否必选 | 说明 | | ||
|-------------|--------|------|-----------------------------------------------------------------------------------------------------------| | ||
| Format | String | 是 | 具体的协议,[查看支持的具体协议列表](https://github.com/alibaba/ilogtail/blob/master/pkg/protocol/encoder/common/comon.go) | | ||
| SeriesLimit | Int | 否 | 触发序列化时序切片的最大长度,默认 1000,仅针对 Format=prometheus 时有效 | | ||
|
||
## 样例 | ||
|
||
使用 `flusher_http` flusher 插件,配置发送 `prometheus` 协议数据。 | ||
|
||
```yaml | ||
enable: true | ||
flushers: | ||
- Type: flusher_http | ||
... | ||
Encoder: | ||
Type: ext_default_encoder/prometheus | ||
... | ||
... | ||
extensions: | ||
- Type: ext_default_encoder/prometheus | ||
Format: 'prometheus' | ||
SeriesLimit: 1024 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package extensions | ||
|
||
import ( | ||
"github.com/alibaba/ilogtail/pkg/models" | ||
"github.com/alibaba/ilogtail/pkg/pipeline" | ||
"github.com/alibaba/ilogtail/pkg/protocol" | ||
) | ||
|
||
// Encoder encodes data of iLogtail data models into bytes. | ||
// Different drivers with different encoding protocols implement Encoder interface. | ||
// | ||
// drivers: raw, influxdb, prometheus, sls, ... | ||
type Encoder interface { | ||
EncoderV1 | ||
EncoderV2 | ||
} | ||
|
||
// EncoderV1 supports v1 pipeline plugin interface, | ||
// encodes data of v1 model into bytes. | ||
// | ||
// drivers: sls, influxdb, ... | ||
type EncoderV1 interface { | ||
EncodeV1(*protocol.LogGroup) ([][]byte, error) | ||
EncodeBatchV1([]*protocol.LogGroup) ([][]byte, error) | ||
} | ||
|
||
// EncoderV2 supports v2 pipeline plugin interface, | ||
// encodes data of v2 model into bytes. | ||
// | ||
// drivers: raw, influxdb, prometheus, ... | ||
type EncoderV2 interface { | ||
EncodeV2(*models.PipelineGroupEvents) ([][]byte, error) | ||
EncodeBatchV2([]*models.PipelineGroupEvents) ([][]byte, error) | ||
} | ||
|
||
type EncoderExtension interface { | ||
Encoder | ||
pipeline.Extension | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
const ( | ||
ProtocolPrometheus = "prometheus" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package encoder | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
|
||
"github.com/alibaba/ilogtail/pkg/pipeline/extensions" | ||
"github.com/alibaba/ilogtail/pkg/protocol/encoder/common" | ||
"github.com/alibaba/ilogtail/pkg/protocol/encoder/prometheus" | ||
) | ||
|
||
func NewEncoder(format string, options map[string]any) (extensions.Encoder, error) { | ||
switch strings.TrimSpace(strings.ToLower(format)) { | ||
case common.ProtocolPrometheus: | ||
var opt prometheus.Option | ||
if err := mapstructure.Decode(options, &opt); err != nil { | ||
return nil, err | ||
} | ||
return prometheus.NewPromEncoder(opt.SeriesLimit), nil | ||
|
||
default: | ||
return nil, fmt.Errorf("not supported encode format: %s", format) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2024 iLogtail Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/alibaba/ilogtail/pkg/logger" | ||
"github.com/alibaba/ilogtail/pkg/models" | ||
"github.com/alibaba/ilogtail/pkg/pipeline/extensions" | ||
"github.com/alibaba/ilogtail/pkg/protocol" | ||
) | ||
|
||
const defaultSeriesLimit = 1000 | ||
|
||
var errNilOrZeroGroupEvents = errors.New("nil or zero group events") | ||
|
||
type Option struct { | ||
SeriesLimit int // config for prometheus encoder | ||
} | ||
|
||
func NewPromEncoder(seriesLimit int) extensions.Encoder { | ||
return newPromEncoder(seriesLimit) | ||
} | ||
|
||
type Encoder struct { | ||
SeriesLimit int | ||
} | ||
|
||
func newPromEncoder(seriesLimit int) *Encoder { | ||
if seriesLimit <= 0 { | ||
seriesLimit = defaultSeriesLimit | ||
} | ||
|
||
return &Encoder{ | ||
SeriesLimit: seriesLimit, | ||
} | ||
} | ||
|
||
func (p *Encoder) EncodeV1(logGroups *protocol.LogGroup) ([][]byte, error) { | ||
// TODO implement me | ||
return nil, nil | ||
} | ||
|
||
func (p *Encoder) EncodeBatchV1(logGroups []*protocol.LogGroup) ([][]byte, error) { | ||
// TODO implement me | ||
return nil, nil | ||
} | ||
|
||
func (p *Encoder) EncodeV2(groupEvents *models.PipelineGroupEvents) ([][]byte, error) { | ||
if groupEvents == nil || len(groupEvents.Events) == 0 { | ||
return nil, errNilOrZeroGroupEvents | ||
} | ||
|
||
var res [][]byte | ||
|
||
wr := getWriteRequest(p.SeriesLimit) | ||
defer putWriteRequest(wr) | ||
|
||
for _, event := range groupEvents.Events { | ||
if event == nil { | ||
logger.Debugf(context.Background(), "nil event") | ||
continue | ||
} | ||
|
||
if event.GetType() != models.EventTypeMetric { | ||
logger.Debugf(context.Background(), "event type (%s) not metric", event.GetName()) | ||
continue | ||
} | ||
|
||
metricEvent, ok := event.(*models.Metric) | ||
if !ok { | ||
logger.Debugf(context.Background(), "assert metric event type (%s) failed", event.GetName()) | ||
continue | ||
} | ||
|
||
wr.Timeseries = append(wr.Timeseries, genPromRemoteWriteTimeseries(metricEvent)) | ||
if len(wr.Timeseries) >= p.SeriesLimit { | ||
res = append(res, marshalBatchTimeseriesData(wr)) | ||
wr.Timeseries = wr.Timeseries[:0] | ||
} | ||
} | ||
|
||
if len(wr.Timeseries) > 0 { | ||
res = append(res, marshalBatchTimeseriesData(wr)) | ||
wr.Timeseries = wr.Timeseries[:0] | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (p *Encoder) EncodeBatchV2(groupEventsSlice []*models.PipelineGroupEvents) ([][]byte, error) { | ||
if len(groupEventsSlice) == 0 { | ||
return nil, errNilOrZeroGroupEvents | ||
} | ||
|
||
var res [][]byte | ||
|
||
for _, groupEvents := range groupEventsSlice { | ||
bytes, err := p.EncodeV2(groupEvents) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
res = append(res, bytes...) | ||
} | ||
|
||
if res == nil { | ||
return nil, errNilOrZeroGroupEvents | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.