From 3893e951b7e63b966df7012ff86bb84f972c3e17 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 26 Jul 2021 12:30:06 -0700 Subject: [PATCH] remove IntExemplar and IntExemplarSlice (#3705) --- cmd/pdatagen/internal/metrics_structs.go | 32 ---- model/pdata/generated_metrics.go | 182 ----------------------- model/pdata/generated_metrics_test.go | 166 --------------------- 3 files changed, 380 deletions(-) diff --git a/cmd/pdatagen/internal/metrics_structs.go b/cmd/pdatagen/internal/metrics_structs.go index 0aec59b1757..b1d6a138912 100644 --- a/cmd/pdatagen/internal/metrics_structs.go +++ b/cmd/pdatagen/internal/metrics_structs.go @@ -51,8 +51,6 @@ var metricsFile = &File{ summaryDataPoint, quantileValuesSlice, quantileValues, - intExemplarSlice, - intExemplar, exemplarSlice, exemplar, }, @@ -226,7 +224,6 @@ var intDataPoint = &messageValueStruct{ startTimeField, timeField, valueInt64Field, - intExemplarsField, }, } @@ -328,29 +325,6 @@ var quantileValues = &messageValueStruct{ }, } -var intExemplarSlice = &sliceOfValues{ - structName: "IntExemplarSlice", - element: intExemplar, -} - -var intExemplar = &messageValueStruct{ - structName: "IntExemplar", - description: "// IntExemplar is a sample input int measurement.\n//\n" + - "// Exemplars also hold information about the environment when the measurement was recorded,\n" + - "// for example the span and trace ID of the active span when the exemplar was recorded.", - - originFullName: "otlpmetrics.IntExemplar", - fields: []baseField{ - timeField, - valueInt64Field, - &sliceField{ - fieldName: "FilteredLabels", - originFieldName: "FilteredLabels", - returnSlice: stringMap, - }, - }, -} - var exemplarSlice = &sliceOfValues{ structName: "ExemplarSlice", element: exemplar, @@ -401,12 +375,6 @@ var labelsField = &sliceField{ returnSlice: stringMap, } -var intExemplarsField = &sliceField{ - fieldName: "Exemplars", - originFieldName: "Exemplars", - returnSlice: intExemplarSlice, -} - var exemplarsField = &sliceField{ fieldName: "Exemplars", originFieldName: "Exemplars", diff --git a/model/pdata/generated_metrics.go b/model/pdata/generated_metrics.go index 44b76d2c4e5..5212c0d2ff7 100644 --- a/model/pdata/generated_metrics.go +++ b/model/pdata/generated_metrics.go @@ -1022,18 +1022,12 @@ func (ms IntDataPoint) SetValue(v int64) { (*ms.orig).Value = v } -// Exemplars returns the Exemplars associated with this IntDataPoint. -func (ms IntDataPoint) Exemplars() IntExemplarSlice { - return newIntExemplarSlice(&(*ms.orig).Exemplars) -} - // CopyTo copies all properties from the current struct to the dest. func (ms IntDataPoint) CopyTo(dest IntDataPoint) { ms.LabelsMap().CopyTo(dest.LabelsMap()) dest.SetStartTimestamp(ms.StartTimestamp()) dest.SetTimestamp(ms.Timestamp()) dest.SetValue(ms.Value()) - ms.Exemplars().CopyTo(dest.Exemplars()) } // NumberDataPointSlice logically represents a slice of NumberDataPoint. @@ -1913,182 +1907,6 @@ func (ms ValueAtQuantile) CopyTo(dest ValueAtQuantile) { dest.SetValue(ms.Value()) } -// IntExemplarSlice logically represents a slice of IntExemplar. -// -// This is a reference type. If passed by value and callee modifies it, the -// caller will see the modification. -// -// Must use NewIntExemplarSlice function to create new instances. -// Important: zero-initialized instance is not valid for use. -type IntExemplarSlice struct { - // orig points to the slice otlpmetrics.IntExemplar field contained somewhere else. - // We use pointer-to-slice to be able to modify it in functions like EnsureCapacity. - orig *[]otlpmetrics.IntExemplar -} - -func newIntExemplarSlice(orig *[]otlpmetrics.IntExemplar) IntExemplarSlice { - return IntExemplarSlice{orig} -} - -// NewIntExemplarSlice creates a IntExemplarSlice with 0 elements. -// Can use "EnsureCapacity" to initialize with a given capacity. -func NewIntExemplarSlice() IntExemplarSlice { - orig := []otlpmetrics.IntExemplar(nil) - return IntExemplarSlice{&orig} -} - -// Len returns the number of elements in the slice. -// -// Returns "0" for a newly instance created with "NewIntExemplarSlice()". -func (es IntExemplarSlice) Len() int { - return len(*es.orig) -} - -// At returns the element at the given index. -// -// This function is used mostly for iterating over all the values in the slice: -// for i := 0; i < es.Len(); i++ { -// e := es.At(i) -// ... // Do something with the element -// } -func (es IntExemplarSlice) At(ix int) IntExemplar { - return newIntExemplar(&(*es.orig)[ix]) -} - -// CopyTo copies all elements from the current slice to the dest. -func (es IntExemplarSlice) CopyTo(dest IntExemplarSlice) { - srcLen := es.Len() - destCap := cap(*dest.orig) - if srcLen <= destCap { - (*dest.orig) = (*dest.orig)[:srcLen:destCap] - } else { - (*dest.orig) = make([]otlpmetrics.IntExemplar, srcLen) - } - - for i := range *es.orig { - newIntExemplar(&(*es.orig)[i]).CopyTo(newIntExemplar(&(*dest.orig)[i])) - } -} - -// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. -// 1. If the newCap <= cap then no change in capacity. -// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. -// -// Here is how a new IntExemplarSlice can be initialized: -// es := NewIntExemplarSlice() -// es.EnsureCapacity(4) -// for i := 0; i < 4; i++ { -// e := es.AppendEmpty() -// // Here should set all the values for e. -// } -func (es IntExemplarSlice) EnsureCapacity(newCap int) { - oldCap := cap(*es.orig) - if newCap <= oldCap { - return - } - - newOrig := make([]otlpmetrics.IntExemplar, len(*es.orig), newCap) - copy(newOrig, *es.orig) - *es.orig = newOrig -} - -// AppendEmpty will append to the end of the slice an empty IntExemplar. -// It returns the newly added IntExemplar. -func (es IntExemplarSlice) AppendEmpty() IntExemplar { - *es.orig = append(*es.orig, otlpmetrics.IntExemplar{}) - return es.At(es.Len() - 1) -} - -// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. -// The current slice will be cleared. -func (es IntExemplarSlice) MoveAndAppendTo(dest IntExemplarSlice) { - if *dest.orig == nil { - // We can simply move the entire vector and avoid any allocations. - *dest.orig = *es.orig - } else { - *dest.orig = append(*dest.orig, *es.orig...) - } - *es.orig = nil -} - -// RemoveIf calls f sequentially for each element present in the slice. -// If f returns true, the element is removed from the slice. -func (es IntExemplarSlice) RemoveIf(f func(IntExemplar) bool) { - newLen := 0 - for i := 0; i < len(*es.orig); i++ { - if f(es.At(i)) { - continue - } - if newLen == i { - // Nothing to move, element is at the right place. - newLen++ - continue - } - (*es.orig)[newLen] = (*es.orig)[i] - newLen++ - } - // TODO: Prevent memory leak by erasing truncated values. - *es.orig = (*es.orig)[:newLen] -} - -// IntExemplar is a sample input int measurement. -// -// Exemplars also hold information about the environment when the measurement was recorded, -// for example the span and trace ID of the active span when the exemplar was recorded. -// -// This is a reference type, if passed by value and callee modifies it the -// caller will see the modification. -// -// Must use NewIntExemplar function to create new instances. -// Important: zero-initialized instance is not valid for use. -// -type IntExemplar struct { - orig *otlpmetrics.IntExemplar -} - -func newIntExemplar(orig *otlpmetrics.IntExemplar) IntExemplar { - return IntExemplar{orig: orig} -} - -// NewIntExemplar creates a new empty IntExemplar. -// -// This must be used only in testing code since no "Set" method available. -func NewIntExemplar() IntExemplar { - return newIntExemplar(&otlpmetrics.IntExemplar{}) -} - -// Timestamp returns the timestamp associated with this IntExemplar. -func (ms IntExemplar) Timestamp() Timestamp { - return Timestamp((*ms.orig).TimeUnixNano) -} - -// SetTimestamp replaces the timestamp associated with this IntExemplar. -func (ms IntExemplar) SetTimestamp(v Timestamp) { - (*ms.orig).TimeUnixNano = uint64(v) -} - -// Value returns the value associated with this IntExemplar. -func (ms IntExemplar) Value() int64 { - return (*ms.orig).Value -} - -// SetValue replaces the value associated with this IntExemplar. -func (ms IntExemplar) SetValue(v int64) { - (*ms.orig).Value = v -} - -// FilteredLabels returns the FilteredLabels associated with this IntExemplar. -func (ms IntExemplar) FilteredLabels() StringMap { - return newStringMap(&(*ms.orig).FilteredLabels) -} - -// CopyTo copies all properties from the current struct to the dest. -func (ms IntExemplar) CopyTo(dest IntExemplar) { - dest.SetTimestamp(ms.Timestamp()) - dest.SetValue(ms.Value()) - ms.FilteredLabels().CopyTo(dest.FilteredLabels()) -} - // ExemplarSlice logically represents a slice of Exemplar. // // This is a reference type. If passed by value and callee modifies it, the diff --git a/model/pdata/generated_metrics_test.go b/model/pdata/generated_metrics_test.go index 52f8685dedc..f7e3a054dfc 100644 --- a/model/pdata/generated_metrics_test.go +++ b/model/pdata/generated_metrics_test.go @@ -697,14 +697,6 @@ func TestIntDataPoint_Value(t *testing.T) { assert.EqualValues(t, testValValue, ms.Value()) } -func TestIntDataPoint_Exemplars(t *testing.T) { - ms := NewIntDataPoint() - assert.EqualValues(t, NewIntExemplarSlice(), ms.Exemplars()) - fillTestIntExemplarSlice(ms.Exemplars()) - testValExemplars := generateTestIntExemplarSlice() - assert.EqualValues(t, testValExemplars, ms.Exemplars()) -} - func TestNumberDataPointSlice(t *testing.T) { es := NewNumberDataPointSlice() assert.EqualValues(t, 0, es.Len()) @@ -1344,137 +1336,6 @@ func TestValueAtQuantile_Value(t *testing.T) { assert.EqualValues(t, testValValue, ms.Value()) } -func TestIntExemplarSlice(t *testing.T) { - es := NewIntExemplarSlice() - assert.EqualValues(t, 0, es.Len()) - es = newIntExemplarSlice(&[]otlpmetrics.IntExemplar{}) - assert.EqualValues(t, 0, es.Len()) - - es.EnsureCapacity(7) - emptyVal := newIntExemplar(&otlpmetrics.IntExemplar{}) - testVal := generateTestIntExemplar() - assert.EqualValues(t, 7, cap(*es.orig)) - for i := 0; i < es.Len(); i++ { - el := es.AppendEmpty() - assert.EqualValues(t, emptyVal, el) - fillTestIntExemplar(el) - assert.EqualValues(t, testVal, el) - } -} - -func TestIntExemplarSlice_CopyTo(t *testing.T) { - dest := NewIntExemplarSlice() - // Test CopyTo to empty - NewIntExemplarSlice().CopyTo(dest) - assert.EqualValues(t, NewIntExemplarSlice(), dest) - - // Test CopyTo larger slice - generateTestIntExemplarSlice().CopyTo(dest) - assert.EqualValues(t, generateTestIntExemplarSlice(), dest) - - // Test CopyTo same size slice - generateTestIntExemplarSlice().CopyTo(dest) - assert.EqualValues(t, generateTestIntExemplarSlice(), dest) -} - -func TestIntExemplarSlice_EnsureCapacity(t *testing.T) { - es := generateTestIntExemplarSlice() - // Test ensure smaller capacity. - const ensureSmallLen = 4 - expectedEs := make(map[*otlpmetrics.IntExemplar]bool) - for i := 0; i < es.Len(); i++ { - expectedEs[es.At(i).orig] = true - } - assert.Equal(t, es.Len(), len(expectedEs)) - es.EnsureCapacity(ensureSmallLen) - assert.Less(t, ensureSmallLen, es.Len()) - foundEs := make(map[*otlpmetrics.IntExemplar]bool, es.Len()) - for i := 0; i < es.Len(); i++ { - foundEs[es.At(i).orig] = true - } - assert.EqualValues(t, expectedEs, foundEs) - - // Test ensure larger capacity - const ensureLargeLen = 9 - oldLen := es.Len() - assert.Equal(t, oldLen, len(expectedEs)) - es.EnsureCapacity(ensureLargeLen) - assert.Equal(t, ensureLargeLen, cap(*es.orig)) -} - -func TestIntExemplarSlice_MoveAndAppendTo(t *testing.T) { - // Test MoveAndAppendTo to empty - expectedSlice := generateTestIntExemplarSlice() - dest := NewIntExemplarSlice() - src := generateTestIntExemplarSlice() - src.MoveAndAppendTo(dest) - assert.EqualValues(t, generateTestIntExemplarSlice(), dest) - assert.EqualValues(t, 0, src.Len()) - assert.EqualValues(t, expectedSlice.Len(), dest.Len()) - - // Test MoveAndAppendTo empty slice - src.MoveAndAppendTo(dest) - assert.EqualValues(t, generateTestIntExemplarSlice(), dest) - assert.EqualValues(t, 0, src.Len()) - assert.EqualValues(t, expectedSlice.Len(), dest.Len()) - - // Test MoveAndAppendTo not empty slice - generateTestIntExemplarSlice().MoveAndAppendTo(dest) - assert.EqualValues(t, 2*expectedSlice.Len(), dest.Len()) - for i := 0; i < expectedSlice.Len(); i++ { - assert.EqualValues(t, expectedSlice.At(i), dest.At(i)) - assert.EqualValues(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) - } -} - -func TestIntExemplarSlice_RemoveIf(t *testing.T) { - // Test RemoveIf on empty slice - emptySlice := NewIntExemplarSlice() - emptySlice.RemoveIf(func(el IntExemplar) bool { - t.Fail() - return false - }) - - // Test RemoveIf - filtered := generateTestIntExemplarSlice() - pos := 0 - filtered.RemoveIf(func(el IntExemplar) bool { - pos++ - return pos%3 == 0 - }) - assert.Equal(t, 5, filtered.Len()) -} - -func TestIntExemplar_CopyTo(t *testing.T) { - ms := NewIntExemplar() - generateTestIntExemplar().CopyTo(ms) - assert.EqualValues(t, generateTestIntExemplar(), ms) -} - -func TestIntExemplar_Timestamp(t *testing.T) { - ms := NewIntExemplar() - assert.EqualValues(t, Timestamp(0), ms.Timestamp()) - testValTimestamp := Timestamp(1234567890) - ms.SetTimestamp(testValTimestamp) - assert.EqualValues(t, testValTimestamp, ms.Timestamp()) -} - -func TestIntExemplar_Value(t *testing.T) { - ms := NewIntExemplar() - assert.EqualValues(t, int64(0), ms.Value()) - testValValue := int64(-17) - ms.SetValue(testValValue) - assert.EqualValues(t, testValValue, ms.Value()) -} - -func TestIntExemplar_FilteredLabels(t *testing.T) { - ms := NewIntExemplar() - assert.EqualValues(t, NewStringMap(), ms.FilteredLabels()) - fillTestStringMap(ms.FilteredLabels()) - testValFilteredLabels := generateTestStringMap() - assert.EqualValues(t, testValFilteredLabels, ms.FilteredLabels()) -} - func TestExemplarSlice(t *testing.T) { es := NewExemplarSlice() assert.EqualValues(t, 0, es.Len()) @@ -1781,7 +1642,6 @@ func fillTestIntDataPoint(tv IntDataPoint) { tv.SetStartTimestamp(Timestamp(1234567890)) tv.SetTimestamp(Timestamp(1234567890)) tv.SetValue(int64(-17)) - fillTestIntExemplarSlice(tv.Exemplars()) } func generateTestNumberDataPointSlice() NumberDataPointSlice { @@ -1898,32 +1758,6 @@ func fillTestValueAtQuantile(tv ValueAtQuantile) { tv.SetValue(float64(17.13)) } -func generateTestIntExemplarSlice() IntExemplarSlice { - tv := NewIntExemplarSlice() - fillTestIntExemplarSlice(tv) - return tv -} - -func fillTestIntExemplarSlice(tv IntExemplarSlice) { - l := 7 - tv.EnsureCapacity(l) - for i := 0; i < l; i++ { - fillTestIntExemplar(tv.AppendEmpty()) - } -} - -func generateTestIntExemplar() IntExemplar { - tv := NewIntExemplar() - fillTestIntExemplar(tv) - return tv -} - -func fillTestIntExemplar(tv IntExemplar) { - tv.SetTimestamp(Timestamp(1234567890)) - tv.SetValue(int64(-17)) - fillTestStringMap(tv.FilteredLabels()) -} - func generateTestExemplarSlice() ExemplarSlice { tv := NewExemplarSlice() fillTestExemplarSlice(tv)