Skip to content

Commit cc68b76

Browse files
committed
better performance on tracecontext removal
1 parent 6138f94 commit cc68b76

File tree

11 files changed

+301
-47
lines changed

11 files changed

+301
-47
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package netdata
2+
3+
import (
4+
valueconstants "github.com/scouter-contrib/scouter-agent-golang/scouterx/common/constants/valueconstant"
5+
"strconv"
6+
)
7+
8+
//BlobValue struct has number value
9+
type BlobValue struct {
10+
Value []byte
11+
}
12+
13+
//NewBlobValue return DeciamlValue instance
14+
func NewBlobValue(value []byte) *BlobValue {
15+
BlobValue := new(BlobValue)
16+
BlobValue.Value = value
17+
return BlobValue
18+
}
19+
20+
//NewBlobEmptyValue return BlobValue instance
21+
func NewBlobEmptyValue() *BlobValue {
22+
BlobValue := new(BlobValue)
23+
return BlobValue
24+
}
25+
26+
// Read function reads a value from datainputx
27+
func (BlobValue *BlobValue) Read(in *DataInputX) (Value, error) {
28+
var err error
29+
BlobValue.Value, err = in.ReadBlob()
30+
return BlobValue, err
31+
}
32+
33+
// Write function writes a number value to dataoutputx
34+
func (BlobValue *BlobValue) Write(out *DataOutputX) error {
35+
err := out.WriteBlob(BlobValue.Value)
36+
return err
37+
}
38+
39+
// GetValueType returns value type
40+
func (BlobValue *BlobValue) GetValueType() byte {
41+
return valueconstants.BLOB
42+
}
43+
44+
// ToString returns converted string value from decimal value
45+
func (BlobValue *BlobValue) ToString() string {
46+
v := "byte[" + strconv.Itoa(len(BlobValue.Value)) + "]"
47+
return v
48+
}

scouterx/common/netdata/datainputx.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package netdata
33
import (
44
"bytes"
55
"encoding/binary"
6+
"errors"
67
"io"
8+
"strconv"
79
)
810

911
// DataInputX is a byte buffer read stream struct
@@ -75,14 +77,21 @@ func (in *DataInputX) ReadFloat32() (float32, error) {
7577
return value, err
7678
}
7779

80+
func (in *DataInputX) ReadFloat64() (float64, error) {
81+
in.offset +=8
82+
var value float64
83+
err := binary.Read(in.reader, binary.BigEndian, &value)
84+
return value, err
85+
}
86+
7887
// ReadString returns string value
7988
func (in *DataInputX) ReadString() (string, error) {
80-
bytes, err := in.readBlob()
89+
bytes, err := in.ReadBlob()
8190
return string(bytes), err
8291

8392
}
8493

85-
func (in *DataInputX) readBlob() ([]byte, error) {
94+
func (in *DataInputX) ReadBlob() ([]byte, error) {
8695
baseLen, err := in.ReadUInt8()
8796
var length int32
8897
switch baseLen {
@@ -147,15 +156,30 @@ func (in *DataInputX) ReadBoolean() (bool, error) {
147156
func (in *DataInputX) ReadValue() (Value, error) {
148157
valueType, err := in.ReadInt8()
149158
value := CreateValue(byte(valueType))
159+
if value == nil {
160+
return NewNilValue(), errors.New("[scouter] Not defined value type:" + strconv.FormatUint(uint64(valueType), 10))
161+
}
150162
readValue, err := value.Read(in)
151163
return readValue, err
152164

153165
}
154166

155167
func (in *DataInputX) ReadIntBytes() ([]byte, error) {
156168
length, err := in.ReadInt32()
157-
val := make([]byte, length)
158-
_, err = in.reader.Read(val)
169+
b, err := in.Read(length)
170+
return b, err
171+
//val := make([]byte, length)
172+
//_, err = in.reader.Read(val)
173+
//if err != nil {
174+
// val = []byte{}
175+
//}
176+
//return val, err
177+
}
178+
179+
func (in *DataInputX) Read(len int32) ([]byte, error) {
180+
in.offset += len
181+
val := make([]byte, len)
182+
_, err := in.reader.Read(val)
159183
if err != nil {
160184
val = []byte{}
161185
}

scouterx/common/netdata/dataoutputx.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ func (out *DataOutputX) WriteFloat32(value float32) (*DataOutputX, error) {
101101
return out, err
102102
}
103103

104+
// WriteFloat64 writes float64 value to buffer
105+
func (out *DataOutputX) WriteFloat64(value float64) (*DataOutputX, error) {
106+
out.written += 8
107+
err := binary.Write(out.writer, binary.BigEndian, value)
108+
return out, err
109+
}
110+
104111
// WriteDecimal writes number type value to buffer
105112
func (out *DataOutputX) WriteDecimal(value int64) (*DataOutputX, error) {
106113
var err error
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package netdata
2+
3+
import (
4+
"strconv"
5+
6+
valueconstants "github.com/scouter-contrib/scouter-agent-golang/scouterx/common/constants/valueconstant"
7+
)
8+
9+
type DoubleValue struct {
10+
Value float64
11+
}
12+
13+
func NewDoubleValue(value float64) *DoubleValue {
14+
doubleValue := new(DoubleValue)
15+
doubleValue.Value = value
16+
return doubleValue
17+
}
18+
19+
//NewFloatEmptyValue returns new FloatValue instance
20+
func NewDoubleEmptyValue() *DoubleValue {
21+
doubleValue := new(DoubleValue)
22+
return doubleValue
23+
}
24+
25+
// Read function reads a value from datainputx
26+
func (value *DoubleValue) Read(in *DataInputX) (Value, error) {
27+
var err error
28+
value.Value, err = in.ReadFloat64()
29+
return value, err
30+
}
31+
32+
// Write function write a float value to dataoutputx
33+
func (value *DoubleValue) Write(out *DataOutputX) error {
34+
_, err := out.WriteFloat64(value.Value)
35+
return err
36+
}
37+
38+
// GetValueType returns value type
39+
func (value *DoubleValue) GetValueType() byte {
40+
return valueconstants.DOUBLE
41+
}
42+
43+
// ToString returns converted float value
44+
func (value *DoubleValue) ToString() string {
45+
return strconv.FormatFloat(float64(value.Value), 'f', 3, 64)
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package netdata
2+
3+
import (
4+
valueconstants "github.com/scouter-contrib/scouter-agent-golang/scouterx/common/constants/valueconstant"
5+
"github.com/scouter-contrib/scouter-agent-golang/scouterx/common/util"
6+
)
7+
8+
//Ip4Value struct has number value
9+
type Ip4Value struct {
10+
Value []byte
11+
}
12+
13+
//NewIp4Value return DeciamlValue instance
14+
func NewIp4Value(ip string) *Ip4Value {
15+
Ip4Value := new(Ip4Value)
16+
Ip4Value.Value = util.IpToBytes(ip)
17+
return Ip4Value
18+
}
19+
20+
//NewIp4EmptyValue return Ip4Value instance
21+
func NewIp4EmptyValue() *Ip4Value {
22+
Ip4Value := new(Ip4Value)
23+
Ip4Value.Value = make([]byte, 4)
24+
return Ip4Value
25+
}
26+
27+
// Read function reads a value from datainputx
28+
func (Ip4Value *Ip4Value) Read(in *DataInputX) (Value, error) {
29+
var err error
30+
Ip4Value.Value, err = in.Read(4)
31+
return Ip4Value, err
32+
}
33+
34+
// Write function writes a number value to dataoutputx
35+
func (Ip4Value *Ip4Value) Write(out *DataOutputX) error {
36+
err := out.Write(Ip4Value.Value)
37+
return err
38+
}
39+
40+
// GetValueType returns value type
41+
func (Ip4Value *Ip4Value) GetValueType() byte {
42+
return valueconstants.IP4ADDR
43+
}
44+
45+
// ToString returns converted string value from decimal value
46+
func (Ip4Value *Ip4Value) ToString() string {
47+
v := "ip[" + string(Ip4Value.Value) + "]"
48+
return v
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package netdata
2+
3+
import (
4+
"strconv"
5+
6+
valueconstants "github.com/scouter-contrib/scouter-agent-golang/scouterx/common/constants/valueconstant"
7+
)
8+
9+
//TextHashValue struct has number value
10+
type TextHashValue struct {
11+
Value int32
12+
}
13+
14+
//NewTextHashValue return DeciamlValue instance
15+
func NewTextHashValue(value int32) *TextHashValue {
16+
textHashValue := new(TextHashValue)
17+
textHashValue.Value = value
18+
return textHashValue
19+
}
20+
21+
//NewDecimalEmptyValue return DeciamlValue instance
22+
func NewTextHashEmptyValue() *TextHashValue {
23+
textHashValue := new(TextHashValue)
24+
return textHashValue
25+
}
26+
27+
// Read function reads a value from datainputx
28+
func (text *TextHashValue) Read(in *DataInputX) (Value, error) {
29+
var err error
30+
text.Value, err = in.ReadInt32()
31+
return text, err
32+
}
33+
34+
// Write function writes a number value to dataoutputx
35+
func (text *TextHashValue) Write(out *DataOutputX) error {
36+
_, err := out.WriteInt32(text.Value)
37+
return err
38+
}
39+
40+
// GetValueType returns value type
41+
func (text *TextHashValue) GetValueType() byte {
42+
return valueconstants.TEXT_HASH
43+
}
44+
45+
// ToString returns converted string value from decimal value
46+
func (text *TextHashValue) ToString() string {
47+
v := strconv.FormatInt(int64(text.Value), 10)
48+
return v
49+
}

scouterx/common/netdata/valuecreator.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@ import (
44
valueconstants "github.com/scouter-contrib/scouter-agent-golang/scouterx/common/constants/valueconstant"
55
)
66

7-
//CreateValue return Value instacne
7+
//CreateValue return Value instance
88
func CreateValue(valueType byte) Value {
99
switch valueType {
1010
case valueconstants.NULL:
1111
return NewNilValue()
12-
case valueconstants.FLOAT:
13-
return NewFloatEmptyValue()
12+
case valueconstants.BOOLEAN:
13+
return NewBooleanEmptyValue()
1414
case valueconstants.DECIMAL:
1515
return NewDecimalEmptyValue()
16-
case valueconstants.LIST:
17-
return NewListValue()
16+
case valueconstants.FLOAT:
17+
return NewFloatEmptyValue()
18+
case valueconstants.DOUBLE:
19+
return NewDoubleEmptyValue()
1820
case valueconstants.TEXT:
1921
return NewTextEmptyValue()
20-
case valueconstants.BOOLEAN:
21-
return NewBooleanEmptyValue()
22+
case valueconstants.TEXT_HASH:
23+
return NewTextHashEmptyValue()
24+
case valueconstants.BLOB:
25+
return NewBlobEmptyValue()
26+
case valueconstants.IP4ADDR:
27+
return NewBlobEmptyValue()
28+
case valueconstants.LIST:
29+
return NewListValue()
2230
case valueconstants.MAP:
2331
return NewMapValue()
2432
default:

0 commit comments

Comments
 (0)