From a6ba7567345cc4bdfea7aa10022adc6bca110184 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 12:09:55 +0100 Subject: [PATCH 01/15] [exporter/elasticsearch] fix lint issues with golangci-lint 1.62 --- .../internal/exphistogram/exphistogram.go | 14 +++++++--- .../internal/objmodel/objmodel.go | 22 +++++++++++++-- exporter/elasticsearchexporter/model.go | 27 ++++++++++++++----- exporter/elasticsearchexporter/model_test.go | 6 ++--- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go b/exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go index 255328f38f1d..6f5299946ffd 100644 --- a/exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go +++ b/exporter/elasticsearchexporter/internal/exphistogram/exphistogram.go @@ -41,12 +41,12 @@ func ToTDigest(dp pmetric.ExponentialHistogramDataPoint) (counts []int64, values } lb := -LowerBoundary(offset+i+1, scale) ub := -LowerBoundary(offset+i, scale) - counts = append(counts, int64(count)) + counts = append(counts, safeUint64ToInt64(count)) values = append(values, lb+(ub-lb)/2) } if zeroCount := dp.ZeroCount(); zeroCount != 0 { - counts = append(counts, int64(zeroCount)) + counts = append(counts, safeUint64ToInt64(zeroCount)) values = append(values, 0) } @@ -59,8 +59,16 @@ func ToTDigest(dp pmetric.ExponentialHistogramDataPoint) (counts []int64, values } lb := LowerBoundary(offset+i, scale) ub := LowerBoundary(offset+i+1, scale) - counts = append(counts, int64(count)) + counts = append(counts, safeUint64ToInt64(count)) values = append(values, lb+(ub-lb)/2) } return } + +func safeUint64ToInt64(v uint64) int64 { + if v > math.MaxInt64 { + return math.MaxInt64 + } else { + return int64(v) // nolint:goset // overflow checked + } +} diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index 6f8a9b41add8..b78999f3a2aa 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -168,6 +168,11 @@ func (doc *Document) AddInt(key string, value int64) { doc.Add(key, IntValue(value)) } +// AddInt adds an unsigned integer value to the document. +func (doc *Document) AddUInt(key string, value uint64) { + doc.Add(key, UIntValue(value)) +} + // AddAttributes expands and flattens all key-value pairs from the input attribute map into // the document. func (doc *Document) AddAttributes(key string, attributes pcommon.Map) { @@ -424,7 +429,16 @@ func (doc *Document) iterJSONDedot(w *json.Visitor, otel bool) error { func StringValue(str string) Value { return Value{kind: KindString, str: str} } // IntValue creates a new value from an integer. -func IntValue(i int64) Value { return Value{kind: KindInt, primitive: uint64(i)} } +func IntValue(i int64) Value { + var v uint64 + if i > 0 { + v = uint64(i) //nolint:gosec // overflow checked + } + return Value{kind: KindInt, primitive: v} +} + +// UIntValue creates a new value from an unsigned integer. +func UIntValue(i uint64) Value { return Value{kind: KindInt, primitive: i} } // DoubleValue creates a new value from a double value.. func DoubleValue(d float64) Value { return Value{kind: KindDouble, dbl: d} } @@ -521,7 +535,11 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error { case KindBool: return w.OnBool(v.primitive == 1) case KindInt: - return w.OnInt64(int64(v.primitive)) + var i int64 = math.MaxInt64 + if v.primitive < math.MaxInt64 { + i = int64(v.primitive) //nolint:gosec // overflow checked + } + return w.OnInt64(i) case KindDouble: if math.IsNaN(v.dbl) || math.IsInf(v.dbl, 0) { // NaN and Inf are undefined for JSON. Let's serialize to "null" diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index ba14a24d4fc6..515970b21203 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -349,7 +349,7 @@ func (m *encodeModel) upsertMetricDataPointValueOTelMode(documents map[uint32]ob if dp.HasMappingHint(hintDocCount) { docCount := dp.DocCount() - document.AddInt("_doc_count", int64(docCount)) + document.AddUInt("_doc_count", docCount) } switch value.Type() { @@ -387,7 +387,8 @@ func (dp summaryDataPoint) Value() (pcommon.Value, error) { vm := pcommon.NewValueMap() m := vm.Map() m.PutDouble("sum", dp.Sum()) - m.PutInt("value_count", int64(dp.Count())) + + m.PutInt("value_count", safeUint64ToInt64(dp.Count())) return vm, nil } @@ -413,7 +414,7 @@ func (dp exponentialHistogramDataPoint) Value() (pcommon.Value, error) { vm := pcommon.NewValueMap() m := vm.Map() m.PutDouble("sum", dp.Sum()) - m.PutInt("value_count", int64(dp.Count())) + m.PutInt("value_count", safeUint64ToInt64(dp.Count())) return vm, nil } @@ -460,7 +461,7 @@ func (dp histogramDataPoint) Value() (pcommon.Value, error) { vm := pcommon.NewValueMap() m := vm.Map() m.PutDouble("sum", dp.Sum()) - m.PutInt("value_count", int64(dp.Count())) + m.PutInt("value_count", safeUint64ToInt64(dp.Count())) return vm, nil } return histogramToValue(dp.HistogramDataPoint) @@ -518,7 +519,7 @@ func histogramToValue(dp pmetric.HistogramDataPoint) (pcommon.Value, error) { value = explicitBounds.At(i-1) + (explicitBounds.At(i)-explicitBounds.At(i-1))/2.0 } - counts.AppendEmpty().SetInt(int64(count)) + counts.AppendEmpty().SetInt(safeUint64ToInt64(count)) values.AppendEmpty().SetDouble(value) } @@ -674,7 +675,7 @@ func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSche document.AddSpanID("parent_span_id", span.ParentSpanID()) document.AddString("name", span.Name()) document.AddString("kind", span.Kind().String()) - document.AddInt("duration", int64(span.EndTimestamp()-span.StartTimestamp())) + document.AddInt("duration", safeUint64ToInt64(uint64(span.EndTimestamp()-span.StartTimestamp()))) m.encodeAttributesOTelMode(&document, span.Attributes()) @@ -985,7 +986,11 @@ func valueHash(h hash.Hash, v pcommon.Value) { h.Write(buf) case pcommon.ValueTypeInt: buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, uint64(v.Int())) + var i uint64 + if v.Int() > 0 { + i = uint64(v.Int()) //nolint:gosec // overflow checked + } + binary.LittleEndian.PutUint64(buf, i) h.Write(buf) case pcommon.ValueTypeBytes: h.Write(v.Bytes().AsRaw()) @@ -1074,3 +1079,11 @@ func mergeGeolocation(attributes pcommon.Map) { } } } + +func safeUint64ToInt64(v uint64) int64 { + if v > math.MaxInt64 { + return math.MaxInt64 + } else { + return int64(v) // nolint:goset // overflow checked + } +} diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index 9b28e2459068..27087b4de614 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -1109,8 +1109,8 @@ func TestEncodeLogOtelMode(t *testing.T) { // helper function that creates the OTel LogRecord from the test structure func createTestOTelLogRecord(t *testing.T, rec OTelRecord) (plog.LogRecord, pcommon.InstrumentationScope, pcommon.Resource) { record := plog.NewLogRecord() - record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) - record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) + record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controller from test + record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controller from test record.SetTraceID(pcommon.TraceID(rec.TraceID)) record.SetSpanID(pcommon.SpanID(rec.SpanID)) @@ -1245,7 +1245,7 @@ func TestEncodeLogBodyMapMode(t *testing.T) { resourceLogs := logs.ResourceLogs().AppendEmpty() scopeLogs := resourceLogs.ScopeLogs().AppendEmpty() logRecords := scopeLogs.LogRecords() - observedTimestamp := pcommon.Timestamp(time.Now().UnixNano()) + observedTimestamp := pcommon.Timestamp(time.Now().UnixNano()) // nolint:gosec // time.Now is safe to convert to signed integer logRecord := logRecords.AppendEmpty() logRecord.SetObservedTimestamp(observedTimestamp) From b250f8731eecc4e0fc24746a9eeb987ee4c5868c Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 12:33:06 +0100 Subject: [PATCH 02/15] keep the overflowed value for little endian --- exporter/elasticsearchexporter/model.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index 515970b21203..113193efc32f 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -986,11 +986,7 @@ func valueHash(h hash.Hash, v pcommon.Value) { h.Write(buf) case pcommon.ValueTypeInt: buf := make([]byte, 8) - var i uint64 - if v.Int() > 0 { - i = uint64(v.Int()) //nolint:gosec // overflow checked - } - binary.LittleEndian.PutUint64(buf, i) + binary.LittleEndian.PutUint64(buf, uint64(v.Int())) // nolint:gosec // overflow assumed. We prefer having high integers than zero. h.Write(buf) case pcommon.ValueTypeBytes: h.Write(v.Bytes().AsRaw()) From 50bf60b4a38ac6e01fdadfbd87dd5d1bee7c8c14 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 12:40:34 +0100 Subject: [PATCH 03/15] return an error on overflow in iterJSON --- .../internal/objmodel/objmodel.go | 8 +++--- .../internal/objmodel/objmodel_test.go | 25 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index b78999f3a2aa..91f98a7357b2 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -33,6 +33,7 @@ package objmodel // import "github.com/open-telemetry/opentelemetry-collector-co import ( "encoding/hex" + "errors" "io" "maps" "math" @@ -535,11 +536,10 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error { case KindBool: return w.OnBool(v.primitive == 1) case KindInt: - var i int64 = math.MaxInt64 - if v.primitive < math.MaxInt64 { - i = int64(v.primitive) //nolint:gosec // overflow checked + if v.primitive > math.MaxInt64 { + return errors.New("integer value is higher than maximum int64") } - return w.OnInt64(i) + return w.OnInt64(int64(v.primitive)) //nolint:gosec // overflow checked case KindDouble: if math.IsNaN(v.dbl) || math.IsInf(v.dbl, 0) { // NaN and Inf are undefined for JSON. Let's serialize to "null" diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go index 3d0a07b820d0..73cf2351e367 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go @@ -4,6 +4,7 @@ package objmodel import ( + "errors" "math" "strings" "testing" @@ -372,13 +373,19 @@ func TestDocument_Serialize_Dedot(t *testing.T) { func TestValue_Serialize(t *testing.T) { tests := map[string]struct { - value Value - want string + value Value + want string + wantErr error }{ - "nil value": {value: nilValue, want: "null"}, - "bool value: true": {value: BoolValue(true), want: "true"}, - "bool value: false": {value: BoolValue(false), want: "false"}, - "int value": {value: IntValue(42), want: "42"}, + "nil value": {value: nilValue, want: "null"}, + "bool value: true": {value: BoolValue(true), want: "true"}, + "bool value: false": {value: BoolValue(false), want: "false"}, + "int value": {value: IntValue(42), want: "42"}, + "large int value": { + value: UIntValue(math.MaxInt64 + 1), + want: "", + wantErr: errors.New("integer value is higher than maximum int64"), + }, "double value: 3.14": {value: DoubleValue(3.14), want: "3.14"}, "double value: 1.0": {value: DoubleValue(1.0), want: "1.0"}, "NaN is undefined": {value: DoubleValue(math.NaN()), want: "null"}, @@ -410,7 +417,11 @@ func TestValue_Serialize(t *testing.T) { t.Run(name, func(t *testing.T) { var buf strings.Builder err := test.value.iterJSON(newJSONVisitor(&buf), false, false) - require.NoError(t, err) + if test.wantErr == nil { + require.NoError(t, err) + } else { + assert.Equal(t, test.wantErr, err) + } assert.Equal(t, test.want, buf.String()) }) } From a913477f97d899e3bac95fb3b2212acc9811b7e9 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 13:52:04 +0100 Subject: [PATCH 04/15] Update exporter/elasticsearchexporter/model.go Co-authored-by: Carson Ip --- exporter/elasticsearchexporter/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index 113193efc32f..1492185c875d 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -675,7 +675,7 @@ func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSche document.AddSpanID("parent_span_id", span.ParentSpanID()) document.AddString("name", span.Name()) document.AddString("kind", span.Kind().String()) - document.AddInt("duration", safeUint64ToInt64(uint64(span.EndTimestamp()-span.StartTimestamp()))) + document.AddUint("duration", (span.EndTimestamp()-span.StartTimestamp())) m.encodeAttributesOTelMode(&document, span.Attributes()) From df70c7e75a640f576908b0e021f2bdab7dd5ca72 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 14:01:15 +0100 Subject: [PATCH 05/15] natively handle uint and int in objmodel --- .../internal/objmodel/objmodel.go | 36 ++++++++----------- .../internal/objmodel/objmodel_test.go | 15 +++----- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index 91f98a7357b2..71229f1d3b58 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -33,7 +33,6 @@ package objmodel // import "github.com/open-telemetry/opentelemetry-collector-co import ( "encoding/hex" - "errors" "io" "maps" "math" @@ -61,13 +60,14 @@ type field struct { // Value type that can be added to a Document. type Value struct { - kind Kind - primitive uint64 - dbl float64 - str string - arr []Value - doc Document - ts time.Time + kind Kind + ui uint64 + i int64 + dbl float64 + str string + arr []Value + doc Document + ts time.Time } // Kind represent the internal kind of a value stored in a Document. @@ -430,16 +430,10 @@ func (doc *Document) iterJSONDedot(w *json.Visitor, otel bool) error { func StringValue(str string) Value { return Value{kind: KindString, str: str} } // IntValue creates a new value from an integer. -func IntValue(i int64) Value { - var v uint64 - if i > 0 { - v = uint64(i) //nolint:gosec // overflow checked - } - return Value{kind: KindInt, primitive: v} -} +func IntValue(i int64) Value { return Value{kind: KindInt, i: i} } // UIntValue creates a new value from an unsigned integer. -func UIntValue(i uint64) Value { return Value{kind: KindInt, primitive: i} } +func UIntValue(i uint64) Value { return Value{kind: KindInt, ui: i} } // DoubleValue creates a new value from a double value.. func DoubleValue(d float64) Value { return Value{kind: KindDouble, dbl: d} } @@ -450,7 +444,7 @@ func BoolValue(b bool) Value { if b { v = 1 } - return Value{kind: KindBool, primitive: v} + return Value{kind: KindBool, ui: v} } // ArrValue combines multiple values into an array value. @@ -534,12 +528,12 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error { case KindNil: return w.OnNil() case KindBool: - return w.OnBool(v.primitive == 1) + return w.OnBool(v.ui == 1) case KindInt: - if v.primitive > math.MaxInt64 { - return errors.New("integer value is higher than maximum int64") + if v.ui != 0 { + return w.OnUint64(v.ui) } - return w.OnInt64(int64(v.primitive)) //nolint:gosec // overflow checked + return w.OnInt64(v.i) case KindDouble: if math.IsNaN(v.dbl) || math.IsInf(v.dbl, 0) { // NaN and Inf are undefined for JSON. Let's serialize to "null" diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go index 73cf2351e367..b9b1cd2cf36d 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go @@ -4,7 +4,6 @@ package objmodel import ( - "errors" "math" "strings" "testing" @@ -377,15 +376,11 @@ func TestValue_Serialize(t *testing.T) { want string wantErr error }{ - "nil value": {value: nilValue, want: "null"}, - "bool value: true": {value: BoolValue(true), want: "true"}, - "bool value: false": {value: BoolValue(false), want: "false"}, - "int value": {value: IntValue(42), want: "42"}, - "large int value": { - value: UIntValue(math.MaxInt64 + 1), - want: "", - wantErr: errors.New("integer value is higher than maximum int64"), - }, + "nil value": {value: nilValue, want: "null"}, + "bool value: true": {value: BoolValue(true), want: "true"}, + "bool value: false": {value: BoolValue(false), want: "false"}, + "int value": {value: IntValue(42), want: "42"}, + "uint value": {value: UIntValue(42), want: "42"}, "double value: 3.14": {value: DoubleValue(3.14), want: "3.14"}, "double value: 1.0": {value: DoubleValue(1.0), want: "1.0"}, "NaN is undefined": {value: DoubleValue(math.NaN()), want: "null"}, From f083f82a39b8f38881893111440f566151d9466d Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 14:03:02 +0100 Subject: [PATCH 06/15] remove added newline --- exporter/elasticsearchexporter/model.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index 1492185c875d..b29e12e89abf 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -387,7 +387,6 @@ func (dp summaryDataPoint) Value() (pcommon.Value, error) { vm := pcommon.NewValueMap() m := vm.Map() m.PutDouble("sum", dp.Sum()) - m.PutInt("value_count", safeUint64ToInt64(dp.Count())) return vm, nil } @@ -675,7 +674,7 @@ func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSche document.AddSpanID("parent_span_id", span.ParentSpanID()) document.AddString("name", span.Name()) document.AddString("kind", span.Kind().String()) - document.AddUint("duration", (span.EndTimestamp()-span.StartTimestamp())) + document.AddUint("duration", (span.EndTimestamp() - span.StartTimestamp())) m.encodeAttributesOTelMode(&document, span.Attributes()) From 52039abffcc95c9ebdc97d02006faaf9a0ba2748 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 14:05:15 +0100 Subject: [PATCH 07/15] fix AddUint call --- exporter/elasticsearchexporter/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index b29e12e89abf..009a9c9b0554 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -674,7 +674,7 @@ func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSche document.AddSpanID("parent_span_id", span.ParentSpanID()) document.AddString("name", span.Name()) document.AddString("kind", span.Kind().String()) - document.AddUint("duration", (span.EndTimestamp() - span.StartTimestamp())) + document.AddUInt("duration", uint64(span.EndTimestamp()-span.StartTimestamp())) m.encodeAttributesOTelMode(&document, span.Attributes()) From da08d56fbeeb06b38d8de4172c1770dc1d6799d1 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 18:15:05 +0100 Subject: [PATCH 08/15] Update exporter/elasticsearchexporter/model.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Rühsen --- exporter/elasticsearchexporter/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model.go b/exporter/elasticsearchexporter/model.go index 009a9c9b0554..f6ee644fb022 100644 --- a/exporter/elasticsearchexporter/model.go +++ b/exporter/elasticsearchexporter/model.go @@ -985,7 +985,7 @@ func valueHash(h hash.Hash, v pcommon.Value) { h.Write(buf) case pcommon.ValueTypeInt: buf := make([]byte, 8) - binary.LittleEndian.PutUint64(buf, uint64(v.Int())) // nolint:gosec // overflow assumed. We prefer having high integers than zero. + binary.LittleEndian.PutUint64(buf, uint64(v.Int())) // nolint:gosec // Overflow assumed. We prefer having high integers over zero. h.Write(buf) case pcommon.ValueTypeBytes: h.Write(v.Bytes().AsRaw()) From 56ba22f5e8a8eb75f38ffe7be21c29d17c160057 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 18:15:39 +0100 Subject: [PATCH 09/15] Update exporter/elasticsearchexporter/model_test.go --- exporter/elasticsearchexporter/model_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index 27087b4de614..d12c31a341a4 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -1109,7 +1109,7 @@ func TestEncodeLogOtelMode(t *testing.T) { // helper function that creates the OTel LogRecord from the test structure func createTestOTelLogRecord(t *testing.T, rec OTelRecord) (plog.LogRecord, pcommon.InstrumentationScope, pcommon.Resource) { record := plog.NewLogRecord() - record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controller from test + record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controller by tests record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controller from test record.SetTraceID(pcommon.TraceID(rec.TraceID)) From 2c6a0f1fe75e7f10e418d4c85886686181a62952 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 18:21:34 +0100 Subject: [PATCH 10/15] Update exporter/elasticsearchexporter/model_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Rühsen --- exporter/elasticsearchexporter/model_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index d12c31a341a4..bd11c5e61c13 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -1109,7 +1109,7 @@ func TestEncodeLogOtelMode(t *testing.T) { // helper function that creates the OTel LogRecord from the test structure func createTestOTelLogRecord(t *testing.T, rec OTelRecord) (plog.LogRecord, pcommon.InstrumentationScope, pcommon.Resource) { record := plog.NewLogRecord() - record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controller by tests + record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controlled by tests record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controller from test record.SetTraceID(pcommon.TraceID(rec.TraceID)) From 9245fde4756c7f209c1dfb2bc4b466c05d2a263d Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 18:21:45 +0100 Subject: [PATCH 11/15] Update exporter/elasticsearchexporter/model_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Rühsen --- exporter/elasticsearchexporter/model_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index bd11c5e61c13..3d7aaa373735 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -1110,7 +1110,7 @@ func TestEncodeLogOtelMode(t *testing.T) { func createTestOTelLogRecord(t *testing.T, rec OTelRecord) (plog.LogRecord, pcommon.InstrumentationScope, pcommon.Resource) { record := plog.NewLogRecord() record.SetTimestamp(pcommon.Timestamp(uint64(rec.Timestamp.UnixNano()))) //nolint:gosec // this input is controlled by tests - record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controller from test + record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controlled by tests record.SetTraceID(pcommon.TraceID(rec.TraceID)) record.SetSpanID(pcommon.SpanID(rec.SpanID)) From bc34bf44bb576f7b1e5f3785215348c1302b9b97 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 18:21:54 +0100 Subject: [PATCH 12/15] Update exporter/elasticsearchexporter/model_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Rühsen --- exporter/elasticsearchexporter/model_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index 3d7aaa373735..eda750a540e7 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -1245,7 +1245,7 @@ func TestEncodeLogBodyMapMode(t *testing.T) { resourceLogs := logs.ResourceLogs().AppendEmpty() scopeLogs := resourceLogs.ScopeLogs().AppendEmpty() logRecords := scopeLogs.LogRecords() - observedTimestamp := pcommon.Timestamp(time.Now().UnixNano()) // nolint:gosec // time.Now is safe to convert to signed integer + observedTimestamp := pcommon.Timestamp(time.Now().UnixNano()) // nolint:gosec // UnixNano is positive and thus safe to convert to signed integer. logRecord := logRecords.AppendEmpty() logRecord.SetObservedTimestamp(observedTimestamp) From 7f620051b3752a08a796be8b77f30600488ad0eb Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 19:39:22 +0100 Subject: [PATCH 13/15] remove unused wantErr --- .../internal/objmodel/objmodel_test.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go index b9b1cd2cf36d..6805a958a019 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel_test.go @@ -372,9 +372,8 @@ func TestDocument_Serialize_Dedot(t *testing.T) { func TestValue_Serialize(t *testing.T) { tests := map[string]struct { - value Value - want string - wantErr error + value Value + want string }{ "nil value": {value: nilValue, want: "null"}, "bool value: true": {value: BoolValue(true), want: "true"}, @@ -412,11 +411,7 @@ func TestValue_Serialize(t *testing.T) { t.Run(name, func(t *testing.T) { var buf strings.Builder err := test.value.iterJSON(newJSONVisitor(&buf), false, false) - if test.wantErr == nil { - require.NoError(t, err) - } else { - assert.Equal(t, test.wantErr, err) - } + require.NoError(t, err) assert.Equal(t, test.want, buf.String()) }) } From 92c6a012aa1c7e99ac13298546a0d032cc2fb80f Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Thu, 12 Dec 2024 19:47:08 +0100 Subject: [PATCH 14/15] introduce a KindUInt --- .../elasticsearchexporter/internal/objmodel/objmodel.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index 71229f1d3b58..2f0c90e9e25f 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -78,6 +78,7 @@ const ( KindNil Kind = iota KindBool KindInt + KindUInt KindDouble KindString KindArr @@ -433,7 +434,7 @@ func StringValue(str string) Value { return Value{kind: KindString, str: str} } func IntValue(i int64) Value { return Value{kind: KindInt, i: i} } // UIntValue creates a new value from an unsigned integer. -func UIntValue(i uint64) Value { return Value{kind: KindInt, ui: i} } +func UIntValue(i uint64) Value { return Value{kind: KindUInt, ui: i} } // DoubleValue creates a new value from a double value.. func DoubleValue(d float64) Value { return Value{kind: KindDouble, dbl: d} } @@ -530,10 +531,9 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool, otel bool) error { case KindBool: return w.OnBool(v.ui == 1) case KindInt: - if v.ui != 0 { - return w.OnUint64(v.ui) - } return w.OnInt64(v.i) + case KindUInt: + return w.OnUint64(v.ui) case KindDouble: if math.IsNaN(v.dbl) || math.IsInf(v.dbl, 0) { // NaN and Inf are undefined for JSON. Let's serialize to "null" From f46fd6f0dbccce1d22b31948fe8a5ef43f66b9cd Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Fri, 13 Dec 2024 10:01:04 +0100 Subject: [PATCH 15/15] Update exporter/elasticsearchexporter/internal/objmodel/objmodel.go Co-authored-by: Christos Markou --- exporter/elasticsearchexporter/internal/objmodel/objmodel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go index 2f0c90e9e25f..0f514e06aaaa 100644 --- a/exporter/elasticsearchexporter/internal/objmodel/objmodel.go +++ b/exporter/elasticsearchexporter/internal/objmodel/objmodel.go @@ -170,7 +170,7 @@ func (doc *Document) AddInt(key string, value int64) { doc.Add(key, IntValue(value)) } -// AddInt adds an unsigned integer value to the document. +// AddUInt adds an unsigned integer value to the document. func (doc *Document) AddUInt(key string, value uint64) { doc.Add(key, UIntValue(value)) }