Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flusher_stdout: print typed-value metric with type int and uint #1854

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/models/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
ValueTypeBoolean
ValueTypeArray
ValueTypeMap
ValueTypeInteger
ValueTypeUnsigned

ContentKey = "content"
BodyKey = ContentKey
Expand Down
57 changes: 55 additions & 2 deletions plugins/flusher/stdout/flusher_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (p *FlusherStdout) Init(context pipeline.Context) error {
}

func (*FlusherStdout) Description() string {
return "stdout flusher for logtail"
return "stdout flusher for loongcollector"
}

// Flush the logGroup list to stdout or files.
Expand Down Expand Up @@ -229,8 +229,11 @@ func (p *FlusherStdout) writeMetricValues(writer *jsoniter.Stream, metric *model
}
i++
}

if metric.GetTypedValue().Len() > 0 {
_, _ = writer.Write([]byte{','})
if values.Len() > 0 {
_, _ = writer.Write([]byte{','})
}
i = 0
for k, v := range metric.GetTypedValue().Iterator() {
writer.WriteObjectField(k)
Expand All @@ -239,6 +242,16 @@ func (p *FlusherStdout) writeMetricValues(writer *jsoniter.Stream, metric *model
writer.WriteString(v.Value.(string))
case models.ValueTypeBoolean:
writer.WriteBool(v.Value.(bool))
case models.ValueTypeInteger:
if !p.handleIntegerTypeAssertion(writer, v.Value, false) {
continue
}
case models.ValueTypeUnsigned:
if !p.handleIntegerTypeAssertion(writer, v.Value, true) {
continue
}
default:
writer.WriteVal(v.Value)
}
if i < metric.GetTypedValue().Len()-1 {
_, _ = writer.Write([]byte{','})
Expand Down Expand Up @@ -308,3 +321,43 @@ func init() {
}
}
}

func (p *FlusherStdout) handleIntegerTypeAssertion(writer *jsoniter.Stream, value interface{}, unsigned bool) bool {
integerValue, ok := getInteger(value)
if !ok {
logger.Error(p.context.GetRuntimeContext(), "FLUSHER_TYPE_ASSERT_ALARM", "unsigned", unsigned, "value", value)
return false
}
if unsigned {
writer.WriteUint64(uint64(integerValue))
} else {
writer.WriteInt64(integerValue)
}
return true
}

func getInteger(v any) (int64, bool) {
switch v := v.(type) {
case int:
return int64(v), true
case int8:
return int64(v), true
case int16:
return int64(v), true
case int32:
return int64(v), true
case int64:
return v, true
case uint:
return int64(v), true
case uint8:
return int64(v), true
case uint16:
return int64(v), true
case uint32:
return int64(v), true
case uint64:
return int64(v), true
}
return 0, false
}
92 changes: 92 additions & 0 deletions plugins/flusher/stdout/flusher_stdout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package stdout

import (
"encoding/json"
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"

"github.com/alibaba/ilogtail/pkg/models"
)

func TestFlusherStdout_writeMetricValues(t *testing.T) {
flusher := &FlusherStdout{}

metric1 := &models.Metric{
Name: "cpu.load.short",
MetricType: models.MetricTypeGauge,
Timestamp: 1672321328000000000,
Tags: models.NewTagsWithKeyValues("host", "server01", "region", "cn"),
Value: &models.MetricSingleValue{Value: 0.64},
}
metric2 := &models.Metric{
Name: "cpu.load.short",
MetricType: models.MetricTypeUntyped,
Timestamp: 1672321328000000000,
Tags: models.NewTagsWithKeyValues("host", "server01", "region", "cn"),
Value: models.NewMetricMultiValueWithMap(map[string]float64{"value": 0.65}),
TypedValue: models.NewMetricTypedValueWithMap(
map[string]*models.TypedValue{
"value1": {
Value: "test",
Type: models.ValueTypeString,
},
"value2": {
Value: true,
Type: models.ValueTypeBoolean,
},
"value3": {
Value: 100,
Type: models.ValueTypeInteger,
},
"value4": {
Value: 100,
Type: models.ValueTypeUnsigned,
},
},
),
}

writer := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
writer.WriteObjectStart()
flusher.writeMetricValues(writer, metric1)
writer.WriteObjectEnd()
type singleValueMetric struct {
MetricType string `json:"metricType"`
Value float64 `json:"value"`
}

var e singleValueMetric
err := json.Unmarshal(writer.Buffer(), &e)
assert.NoError(t, err)
assert.Equal(t, "Gauge", e.MetricType)
assert.Equal(t, 0.64, e.Value)

writer = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
writer.WriteObjectStart()
flusher.writeMetricValues(writer, metric2)
writer.WriteObjectEnd()

type typedValueMetric struct {
MetricType string `json:"metricType"`
Values map[string]interface{} `json:"values"`
}
var e2 typedValueMetric
err = json.Unmarshal(writer.Buffer(), &e2)
assert.NoError(t, err)
for k, v := range e2.Values {
switch k {
case "value":
assert.Equal(t, 0.65, v)
case "value1":
assert.Equal(t, "test", v)
case "value2":
assert.Equal(t, true, v)
case "value3":
assert.Equal(t, float64(100), v)
case "value4":
assert.Equal(t, float64(100), v)
}
}
}
Loading