Skip to content

Commit

Permalink
Switch ints that can't be negative to uints (#123)
Browse files Browse the repository at this point in the history
* use uints

* uint don't need a min=0 validation

* Update codec/utils_test.go

Co-authored-by: Marc Lopez Rubio <[email protected]>

* Update input/elasticapm/internal/modeldecoder/v2/metricset_test.go

Co-authored-by: Marc Lopez Rubio <[email protected]>

* Update model/modelpb/util_test.go

Co-authored-by: Marc Lopez Rubio <[email protected]>

* fix renamed methods calls

* Revert "uint don't need a min=0 validation"

This reverts commit dc27da9.

* ignore lint for < 0 in generated code

---------

Co-authored-by: Marc Lopez Rubio <[email protected]>
  • Loading branch information
dmathieu and marclop authored Jul 28, 2023
1 parent 1076ea8 commit 31428f0
Show file tree
Hide file tree
Showing 55 changed files with 204 additions and 192 deletions.
14 changes: 7 additions & 7 deletions codec/fullevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func fullEvent(t *testing.B) *modelpb.APMEvent {
Value: []string{"bar"},
},
},
AgeMillis: int64Ptr(2),
AgeMillis: uint64Ptr(2),
QueueName: "queuename",
RoutingKey: "routingkey",
},
Expand Down Expand Up @@ -158,7 +158,7 @@ func fullEvent(t *testing.B) *modelpb.APMEvent {
Value: []string{"bar"},
},
},
AgeMillis: int64Ptr(2),
AgeMillis: uint64Ptr(2),
QueueName: "queuename",
RoutingKey: "routingkey",
},
Expand All @@ -168,7 +168,7 @@ func fullEvent(t *testing.B) *modelpb.APMEvent {
Id: "id",
DurationHistogram: &modelpb.Histogram{
Values: []float64{4},
Counts: []int64{5},
Counts: []uint64{5},
},
DroppedSpansStats: []*modelpb.DroppedSpanStats{
{
Expand Down Expand Up @@ -200,7 +200,7 @@ func fullEvent(t *testing.B) *modelpb.APMEvent {
Unit: "unit",
Histogram: &modelpb.Histogram{
Values: []float64{1},
Counts: []int64{2},
Counts: []uint64{2},
},
Summary: &modelpb.SummaryMetric{
Count: 3,
Expand Down Expand Up @@ -367,9 +367,9 @@ func fullEvent(t *testing.B) *modelpb.APMEvent {
Headers: randomHTTPHeaders(t),
Finished: boolPtr(true),
HeadersSent: boolPtr(true),
TransferSize: int64Ptr(1),
EncodedBodySize: int64Ptr(2),
DecodedBodySize: int64Ptr(3),
TransferSize: uint64Ptr(1),
EncodedBodySize: uint64Ptr(2),
DecodedBodySize: uint64Ptr(3),
StatusCode: 200,
},
Version: "version",
Expand Down
2 changes: 1 addition & 1 deletion codec/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func uintPtr(i uint32) *uint32 {
return &i
}

func int64Ptr(i int64) *int64 {
func uint64Ptr(i uint64) *uint64 {
return &i
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ var (
nullableTypeInt: TypeNameInteger,
"int": TypeNameInteger,
"int64": TypeNameInteger,
"uint": TypeNameInteger,
"uint64": TypeNameInteger,
nullableTypeTimeMicrosUnix: TypeNameInteger,
nullableTypeString: TypeNameString,
"string": TypeNameString,
Expand Down
1 change: 1 addition & 0 deletions input/elasticapm/internal/modeldecoder/generator/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ for _, elem := range val.%s{
func sliceRuleMinVals(w io.Writer, f structField, rule validationRule) error {
fmt.Fprintf(w, `
for _, elem := range val.%s{
//lint:ignore SA4003 don't reject the check for uint
if elem %s %s{
return fmt.Errorf("'%s': validation rule '%s(%s)' violated")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func SetStructValues(in interface{}, values *Values, opts ...SetStructValuesOpti
elemVal = reflect.ValueOf(values.Int)
case []int64:
elemVal = reflect.ValueOf(int64(values.Int))
case []uint:
elemVal = reflect.ValueOf(uint(values.Int))
case []uint64:
elemVal = reflect.ValueOf(uint64(values.Int))
case []uint8:
elemVal = reflect.ValueOf(uint8(values.Int))
case []float64:
Expand Down Expand Up @@ -302,9 +306,14 @@ func AssertStructValues(t *testing.T, i interface{}, isException func(string) bo
newVal = values.Int
case int64:
newVal = int64(values.Int)
case uint64:
newVal = uint64(values.Int)
case *int64:
val := int64(values.Int)
newVal = &val
case *uint64:
val := uint64(values.Int)
newVal = &val
case *int:
newVal = &values.Int
case int32:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func SetInternalMetrics(event *modelpb.APMEvent) bool {
if event.Span.SelfTime == nil {
event.Span.SelfTime = &modelpb.AggregatedDuration{}
}
event.Span.SelfTime.Count = int64(v.Value)
event.Span.SelfTime.Count = uint64(v.Value)
haveMetrics = true
case "span.self_time.sum.us":
if event.Span.SelfTime == nil {
Expand Down
24 changes: 12 additions & 12 deletions input/elasticapm/internal/modeldecoder/rumv3/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func mapToTransactionMetricsetModel(from *transactionMetricset, event *modelpb.A
if event.Span != nil {
if value := from.Samples.SpanSelfTimeCount.Value; value.IsSet() {
event.Span.SelfTime = populateNil(event.Span.SelfTime)
event.Span.SelfTime.Count = int64(value.Val)
event.Span.SelfTime.Count = uint64(value.Val)
ok = true
}
if value := from.Samples.SpanSelfTimeSum.Value; value.IsSet() {
Expand All @@ -424,18 +424,18 @@ func mapToResponseModel(from contextResponse, out *modelpb.HTTPResponse) {
out.Headers = modeldecoderutil.HTTPHeadersToModelpb(from.Headers.Val)
}
if from.StatusCode.IsSet() {
out.StatusCode = int32(from.StatusCode.Val)
out.StatusCode = uint32(from.StatusCode.Val)
}
if from.TransferSize.IsSet() {
val := int64(from.TransferSize.Val)
val := uint64(from.TransferSize.Val)
out.TransferSize = &val
}
if from.EncodedBodySize.IsSet() {
val := int64(from.EncodedBodySize.Val)
val := uint64(from.EncodedBodySize.Val)
out.EncodedBodySize = &val
}
if from.DecodedBodySize.IsSet() {
val := int64(from.DecodedBodySize.Val)
val := uint64(from.DecodedBodySize.Val)
out.DecodedBodySize = &val
}
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
if from.Context.HTTP.StatusCode.IsSet() {
event.Http = populateNil(event.Http)
event.Http.Response = &response
event.Http.Response.StatusCode = int32(from.Context.HTTP.StatusCode.Val)
event.Http.Response.StatusCode = uint32(from.Context.HTTP.StatusCode.Val)
}
if from.Context.HTTP.URL.IsSet() {
event.Url = populateNil(event.Url)
Expand All @@ -566,15 +566,15 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
event.Http = populateNil(event.Http)
event.Http.Response = &response
if from.Context.HTTP.Response.DecodedBodySize.IsSet() {
val := int64(from.Context.HTTP.Response.DecodedBodySize.Val)
val := uint64(from.Context.HTTP.Response.DecodedBodySize.Val)
event.Http.Response.DecodedBodySize = &val
}
if from.Context.HTTP.Response.EncodedBodySize.IsSet() {
val := int64(from.Context.HTTP.Response.EncodedBodySize.Val)
val := uint64(from.Context.HTTP.Response.EncodedBodySize.Val)
event.Http.Response.EncodedBodySize = &val
}
if from.Context.HTTP.Response.TransferSize.IsSet() {
val := int64(from.Context.HTTP.Response.TransferSize.Val)
val := uint64(from.Context.HTTP.Response.TransferSize.Val)
event.Http.Response.TransferSize = &val
}
}
Expand Down Expand Up @@ -784,7 +784,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
if from.Session.ID.IsSet() {
event.Session = &modelpb.Session{
Id: from.Session.ID.Val,
Sequence: int64(from.Session.Sequence.Val),
Sequence: uint64(from.Session.Sequence.Val),
}
}
if from.SpanCount.Dropped.IsSet() {
Expand All @@ -810,7 +810,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
CumulativeLayoutShift: -1,
FirstInputDelay: -1,
TotalBlockingTime: -1,
LongTask: &modelpb.LongtaskMetrics{Count: -1},
LongTask: nil,
}
if from.UserExperience.CumulativeLayoutShift.IsSet() {
out.UserExperience.CumulativeLayoutShift = from.UserExperience.CumulativeLayoutShift.Val
Expand All @@ -823,7 +823,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
}
if from.UserExperience.Longtask.IsSet() {
out.UserExperience.LongTask = &modelpb.LongtaskMetrics{
Count: int64(from.UserExperience.Longtask.Count.Val),
Count: uint64(from.UserExperience.Longtask.Count.Val),
Sum: from.UserExperience.Longtask.Sum.Val,
Max: from.UserExperience.Longtask.Max.Val,
}
Expand Down
36 changes: 18 additions & 18 deletions input/elasticapm/internal/modeldecoder/v2/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func mapToDroppedSpansModel(from []transactionDroppedSpanStats, tx *modelpb.Tran
}
if f.Duration.IsSet() {
to.Duration = &modelpb.AggregatedDuration{}
to.Duration.Count = int64(f.Duration.Count.Val)
to.Duration.Count = uint64(f.Duration.Count.Val)
sum := f.Duration.Sum
if sum.IsSet() {
to.Duration.Sum = durationpb.New(time.Duration(sum.Us.Val) * time.Microsecond)
Expand Down Expand Up @@ -737,15 +737,15 @@ func mapToMetricsetModel(from *metricset, event *modelpb.APMEvent) bool {
if len(from.Samples) > 0 {
samples := make([]*modelpb.MetricsetSample, 0, len(from.Samples))
for name, sample := range from.Samples {
var counts []int64
var counts []uint64
var values []float64
var histogram *modelpb.Histogram
if n := len(sample.Values); n > 0 {
values = make([]float64, n)
copy(values, sample.Values)
}
if n := len(sample.Counts); n > 0 {
counts = make([]int64, n)
counts = make([]uint64, n)
copy(counts, sample.Counts)
}
if len(counts) != 0 || len(values) != 0 {
Expand Down Expand Up @@ -871,18 +871,18 @@ func mapToResponseModel(from contextResponse, out *modelpb.HTTPResponse) {
out.HeadersSent = &val
}
if from.StatusCode.IsSet() {
out.StatusCode = int32(from.StatusCode.Val)
out.StatusCode = uint32(from.StatusCode.Val)
}
if from.TransferSize.IsSet() {
val := int64(from.TransferSize.Val)
val := uint64(from.TransferSize.Val)
out.TransferSize = &val
}
if from.EncodedBodySize.IsSet() {
val := int64(from.EncodedBodySize.Val)
val := uint64(from.EncodedBodySize.Val)
out.EncodedBodySize = &val
}
if from.DecodedBodySize.IsSet() {
val := int64(from.DecodedBodySize.Val)
val := uint64(from.DecodedBodySize.Val)
out.DecodedBodySize = &val
}
}
Expand Down Expand Up @@ -1068,29 +1068,29 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
event.Http = populateNil(event.Http)
response := modelpb.HTTPResponse{}
if from.Context.HTTP.Response.DecodedBodySize.IsSet() {
val := int64(from.Context.HTTP.Response.DecodedBodySize.Val)
val := uint64(from.Context.HTTP.Response.DecodedBodySize.Val)
response.DecodedBodySize = &val
}
if from.Context.HTTP.Response.EncodedBodySize.IsSet() {
val := int64(from.Context.HTTP.Response.EncodedBodySize.Val)
val := uint64(from.Context.HTTP.Response.EncodedBodySize.Val)
response.EncodedBodySize = &val
}
if from.Context.HTTP.Response.Headers.IsSet() {
response.Headers = modeldecoderutil.HTTPHeadersToModelpb(from.Context.HTTP.Response.Headers.Val)
}
if from.Context.HTTP.Response.StatusCode.IsSet() {
response.StatusCode = int32(from.Context.HTTP.Response.StatusCode.Val)
response.StatusCode = uint32(from.Context.HTTP.Response.StatusCode.Val)
}
if from.Context.HTTP.Response.TransferSize.IsSet() {
val := int64(from.Context.HTTP.Response.TransferSize.Val)
val := uint64(from.Context.HTTP.Response.TransferSize.Val)
response.TransferSize = &val
}
event.Http.Response = &response
}
if from.Context.HTTP.StatusCode.IsSet() {
event.Http = populateNil(event.Http)
event.Http.Response = populateNil(event.Http.Response)
event.Http.Response.StatusCode = int32(from.Context.HTTP.StatusCode.Val)
event.Http.Response.StatusCode = uint32(from.Context.HTTP.StatusCode.Val)
}
if from.Context.HTTP.URL.IsSet() {
event.Url = populateNil(event.Url)
Expand All @@ -1106,7 +1106,7 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
message.Headers = modeldecoderutil.HTTPHeadersToModelpb(from.Context.Message.Headers.Val)
}
if from.Context.Message.Age.Milliseconds.IsSet() {
val := int64(from.Context.Message.Age.Milliseconds.Val)
val := uint64(from.Context.Message.Age.Milliseconds.Val)
message.AgeMillis = &val
}
if from.Context.Message.Queue.Name.IsSet() {
Expand Down Expand Up @@ -1291,7 +1291,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
if from.Context.Message.IsSet() {
out.Message = &modelpb.Message{}
if from.Context.Message.Age.IsSet() {
val := int64(from.Context.Message.Age.Milliseconds.Val)
val := uint64(from.Context.Message.Age.Milliseconds.Val)
out.Message.AgeMillis = &val
}
if from.Context.Message.Body.IsSet() {
Expand Down Expand Up @@ -1397,7 +1397,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
if from.Session.ID.IsSet() {
event.Session = &modelpb.Session{
Id: from.Session.ID.Val,
Sequence: int64(from.Session.Sequence.Val),
Sequence: uint64(from.Session.Sequence.Val),
}
}
if from.SpanCount.Dropped.IsSet() {
Expand Down Expand Up @@ -1426,7 +1426,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
CumulativeLayoutShift: -1,
FirstInputDelay: -1,
TotalBlockingTime: -1,
LongTask: &modelpb.LongtaskMetrics{Count: -1},
LongTask: nil,
}
if from.UserExperience.CumulativeLayoutShift.IsSet() {
out.UserExperience.CumulativeLayoutShift = from.UserExperience.CumulativeLayoutShift.Val
Expand All @@ -1440,7 +1440,7 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
}
if from.UserExperience.Longtask.IsSet() {
out.UserExperience.LongTask = &modelpb.LongtaskMetrics{
Count: int64(from.UserExperience.Longtask.Count.Val),
Count: uint64(from.UserExperience.Longtask.Count.Val),
Sum: from.UserExperience.Longtask.Sum.Val,
Max: from.UserExperience.Longtask.Max.Val,
}
Expand Down Expand Up @@ -1507,7 +1507,7 @@ func mapToLogModel(from *log, event *modelpb.APMEvent) {
event.Log = populateNil(event.Log)
event.Log.Origin = populateNil(event.Log.Origin)
event.Log.Origin.File = populateNil(event.Log.Origin.File)
event.Log.Origin.File.Line = int32(from.OriginFileLine.Val)
event.Log.Origin.File.Line = uint32(from.OriginFileLine.Val)
}
if from.OriginFileName.IsSet() {
event.Log = populateNil(event.Log)
Expand Down
10 changes: 5 additions & 5 deletions input/elasticapm/internal/modeldecoder/v2/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestDecodeNestedLog(t *testing.T) {
assert.Equal(t, "warn", batch[0].Log.Level)
assert.Equal(t, "testLogger", batch[0].Log.Logger)
assert.Equal(t, "testFile", batch[0].Log.Origin.File.Name)
assert.Equal(t, int32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, uint32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, "testFunc", batch[0].Log.Origin.FunctionName)
assert.Equal(t, "testsvc", batch[0].Service.Name)
assert.Equal(t, "v1.2.0", batch[0].Service.Version)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestDecodeNestedLog(t *testing.T) {
assert.Equal(t, "error", batch[0].Log.Level)
assert.Equal(t, "testLogger", batch[0].Log.Logger)
assert.Equal(t, "testFile", batch[0].Log.Origin.File.Name)
assert.Equal(t, int32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, uint32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, "testFunc", batch[0].Log.Origin.FunctionName)
assert.Equal(t, "testsvc", batch[0].Service.Name)
assert.Equal(t, "v1.2.0", batch[0].Service.Version)
Expand All @@ -117,7 +117,7 @@ func TestDecodeNestedLog(t *testing.T) {
assert.Equal(t, "error", batch[0].Log.Level)
assert.Equal(t, "testLogger", batch[0].Log.Logger)
assert.Equal(t, "testFile", batch[0].Log.Origin.File.Name)
assert.Equal(t, int32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, uint32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, "testFunc", batch[0].Log.Origin.FunctionName)
assert.Equal(t, "testsvc", batch[0].Service.Name)
assert.Equal(t, "v1.2.0", batch[0].Service.Version)
Expand All @@ -144,7 +144,7 @@ func TestDecodeNestedLog(t *testing.T) {
assert.Equal(t, "error", batch[0].Log.Level)
assert.Equal(t, "testLogger", batch[0].Log.Logger)
assert.Equal(t, "testFile", batch[0].Log.Origin.File.Name)
assert.Equal(t, int32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, uint32(10), batch[0].Log.Origin.File.Line)
assert.Equal(t, "testFunc", batch[0].Log.Origin.FunctionName)
assert.Equal(t, "testsvc", batch[0].Service.Name)
assert.Equal(t, "v1.2.0", batch[0].Service.Version)
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestDecodeMapToLogModel(t *testing.T) {
assert.Equal(t, "warn", out.Log.Level)
assert.Equal(t, "testlogger", out.Log.Logger)
assert.Equal(t, "testfile", out.Log.Origin.File.Name)
assert.Equal(t, int32(10), out.Log.Origin.File.Line)
assert.Equal(t, uint32(10), out.Log.Origin.File.Line)
assert.Equal(t, "testfunc", out.Log.Origin.FunctionName)
})

Expand Down
Loading

0 comments on commit 31428f0

Please sign in to comment.