github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/decoder/statsd/decoder.go (about) 1 // Copyright 2022 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 statsd 16 17 import ( 18 "bytes" 19 "context" 20 "net/http" 21 "time" 22 23 "github.com/alibaba/ilogtail/pkg/helper" 24 "github.com/alibaba/ilogtail/pkg/logger" 25 "github.com/alibaba/ilogtail/pkg/models" 26 "github.com/alibaba/ilogtail/pkg/protocol" 27 "github.com/alibaba/ilogtail/pkg/protocol/decoder/common" 28 29 dogstatsd "github.com/narqo/go-dogstatsd-parser" 30 ) 31 32 type Decoder struct { 33 Time time.Time 34 } 35 36 func parseLabels(metric *dogstatsd.Metric) *helper.MetricLabels { 37 var labels helper.MetricLabels 38 for k, v := range metric.Tags { 39 labels.Append(k, v) 40 } 41 return &labels 42 } 43 44 func (d *Decoder) Decode(data []byte, req *http.Request, tags map[string]string) (logs []*protocol.Log, err error) { 45 now := time.Now() 46 parts := bytes.Split(data, []byte("\n")) 47 for _, part := range parts { 48 if len(part) == 0 { 49 continue 50 } 51 m, err := dogstatsd.Parse(string(part)) 52 if err != nil { 53 logger.Debug(context.Background(), "parse statsd error", err) 54 if time.Since(d.Time).Seconds() > 10 { 55 logger.Error(context.Background(), "STATSD_PARSE_ALARM", "parse err", err) 56 d.Time = time.Now() 57 } 58 continue 59 } 60 log := helper.NewMetricLog(m.Name, now.UnixNano(), m.Value.(float64), parseLabels(m)) 61 logs = append(logs, log) 62 } 63 return 64 } 65 66 func (d *Decoder) ParseRequest(res http.ResponseWriter, req *http.Request, maxBodySize int64) (data []byte, statusCode int, err error) { 67 return common.CollectBody(res, req, maxBodySize) 68 } 69 70 func (d *Decoder) DecodeV2(data []byte, req *http.Request) (groups []*models.PipelineGroupEvents, err error) { 71 //TODO: Implement DecodeV2 72 return nil, nil 73 }