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

[exporter/elasticsearch] [chore] fix lint issues with golangci-lint 1.62 #36798

Merged
merged 15 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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))
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
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)
}

Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in wrong values without notifying the user.
IMO, we can't just silently go on with modified count values here.
Or it needs to documented very well.

Copy link
Member Author

@dmathieu dmathieu Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that an unchecked overflow will already send wrong values. We're chosing to send the highest maximum value rather than a negative number which would be even more confusing.

I don't mind adding some logging, but I haven't found any such precedent within the package.
And for that log line to be useful and not just cause more noise, we'd have to be able to correlate it to a specific document ID.

} else {
return int64(v) // nolint:goset // overflow checked
}
}
22 changes: 20 additions & 2 deletions exporter/elasticsearchexporter/internal/objmodel/objmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down Expand Up @@ -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}
}
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved

// 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} }
Expand Down Expand Up @@ -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)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
case KindDouble:
if math.IsNaN(v.dbl) || math.IsInf(v.dbl, 0) {
// NaN and Inf are undefined for JSON. Let's serialize to "null"
Expand Down
27 changes: 20 additions & 7 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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())))
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

m.encodeAttributesOTelMode(&document, span.Attributes())

Expand Down Expand Up @@ -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)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
h.Write(buf)
case pcommon.ValueTypeBytes:
h.Write(v.Bytes().AsRaw())
Expand Down Expand Up @@ -1074,3 +1079,11 @@ func mergeGeolocation(attributes pcommon.Map) {
}
}
}

func safeUint64ToInt64(v uint64) int64 {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
if v > math.MaxInt64 {
return math.MaxInt64
} else {
return int64(v) // nolint:goset // overflow checked
}
}
6 changes: 3 additions & 3 deletions exporter/elasticsearchexporter/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
record.SetObservedTimestamp(pcommon.Timestamp(uint64(rec.ObservedTimestamp.UnixNano()))) //nolint:gosec // this input is controller from test
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

record.SetTraceID(pcommon.TraceID(rec.TraceID))
record.SetSpanID(pcommon.SpanID(rec.SpanID))
Expand Down Expand Up @@ -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
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

logRecord := logRecords.AppendEmpty()
logRecord.SetObservedTimestamp(observedTimestamp)
Expand Down
Loading