Skip to content

Commit

Permalink
update NumberDataPoint to support DoubleVal and IntVal
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten committed Jul 22, 2021
1 parent cfbb0f6 commit e1058b2
Show file tree
Hide file tree
Showing 25 changed files with 252 additions and 66 deletions.
110 changes: 110 additions & 0 deletions cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func (ms ${structName}) Set${fieldName}(v ${returnType}) {
(*ms.orig).${originFieldName} = v
}`

const accessorsOneofPrimitiveTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${returnType} {
return (*ms.orig).GetAs${fieldType}()
}
// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${returnType}) {
(*ms.orig).${originFieldName} = &${originFullName}_As${fieldType}{
As${fieldType}: v,
}
}`

const accessorsPrimitiveAsDoubleTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${returnType} {
return (*ms.orig).GetAsDouble()
Expand Down Expand Up @@ -462,3 +474,101 @@ func (pf *primitiveAsDoubleField) generateCopyToValue(sb *strings.Builder) {
}

var _ baseField = (*primitiveField)(nil)

type oneOfPrimitiveValue struct {
name string
defaultVal string
testVal string
returnType string
originFieldName string
originFullName string
fieldType string
}

func (opv *oneOfPrimitiveValue) generateAccessors(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorsOneofPrimitiveTemplate, func(name string) string {
switch name {
case "structName":
return ms.getName()
case "fieldName":
return opv.name
case "lowerFieldName":
return strings.ToLower(opv.name)
case "returnType":
return opv.returnType
case "originFieldName":
return opv.originFieldName
case "originFullName":
return opv.originFullName
case "fieldType":
return opv.fieldType
default:
panic(name)
}
}))
sb.WriteString("\n")
}

func (opv *oneOfPrimitiveValue) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorsPrimitiveTestTemplate, func(name string) string {
switch name {
case "structName":
return ms.getName()
case "defaultVal":
return opv.defaultVal
case "fieldName":
return opv.name
case "testValue":
return opv.testVal
default:
panic(name)
}
}))
sb.WriteString("\n")
}

func (opv *oneOfPrimitiveValue) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\t tv.Set" + opv.name + "(" + opv.testVal + ")\n")
}

func (opv *oneOfPrimitiveValue) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("\tcase NumberDataPointType" + opv.fieldType + ":\n")
sb.WriteString("\t dest.Set" + opv.name + "(ms." + opv.name + "())\n")
}

var _ baseField = (*oneOfPrimitiveValue)(nil)

type numberField struct {
fields []*oneOfPrimitiveValue
}

func (nf *numberField) generateAccessors(ms baseStruct, sb *strings.Builder) {
for _, field := range nf.fields {
field.generateAccessors(ms, sb)
}
}

func (nf *numberField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
for _, field := range nf.fields {
field.generateAccessorsTest(ms, sb)
}
}

func (nf *numberField) generateSetWithTestValue(sb *strings.Builder) {
for _, field := range nf.fields {
field.generateSetWithTestValue(sb)
// TODO: this test should be generated for all number values,
// for now, it's ok to only set one value
return
}
}

func (nf *numberField) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("\tswitch ms.Type() {\n")
for _, field := range nf.fields {
field.generateCopyToValue(sb)
}
sb.WriteString("\t}\n")
}

var _ baseField = (*numberField)(nil)
30 changes: 22 additions & 8 deletions cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,33 @@ var numberDataPointSlice = &sliceOfPtrs{

var numberDataPoint = &messageValueStruct{
structName: "NumberDataPoint",
description: "// NumberDataPoint is a single data point in a timeseries that describes the time-varying value of a double metric.",
description: "// NumberDataPoint is a single data point in a timeseries that describes the time-varying value of a number metric.",
originFullName: "otlpmetrics.NumberDataPoint",
fields: []baseField{
labelsField,
startTimeField,
timeField,
&primitiveAsDoubleField{
originFullName: "otlpmetrics.NumberDataPoint",
fieldName: "Value",
originFieldName: "Value",
returnType: "float64",
defaultVal: "float64(0.0)",
testVal: "float64(17.13)",
&numberField{
fields: []*oneOfPrimitiveValue{
{
originFullName: "otlpmetrics.NumberDataPoint",
name: "DoubleVal",
originFieldName: "Value",
returnType: "float64",
defaultVal: "float64(0.0)",
testVal: "float64(17.13)",
fieldType: "Double",
},
{
originFullName: "otlpmetrics.NumberDataPoint",
name: "IntVal",
originFieldName: "Value",
returnType: "int64",
defaultVal: "int64(0)",
testVal: "int64(17)",
fieldType: "Int",
},
},
},
exemplarsField,
},
Expand Down
22 changes: 11 additions & 11 deletions exporter/prometheusexporter/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestAccumulateDeltaAggregation(t *testing.T) {
metric.SetDataType(pdata.MetricDataTypeSum)
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityDelta)
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(42.42)
dp.SetDoubleVal(42.42)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestAccumulateMetrics(t *testing.T) {
metric.SetDataType(pdata.MetricDataTypeGauge)
metric.SetDescription("test description")
dp := metric.Gauge().DataPoints().AppendEmpty()
dp.SetValue(v)
dp.SetDoubleVal(v)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestAccumulateMetrics(t *testing.T) {
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityCumulative)
metric.SetDescription("test description")
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(v)
dp.SetDoubleVal(v)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestAccumulateMetrics(t *testing.T) {
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityCumulative)
metric.SetDescription("test description")
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(v)
dp.SetDoubleVal(v)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -238,14 +238,14 @@ func TestAccumulateMetrics(t *testing.T) {
n := a.Accumulate(resourceMetrics2)
require.Equal(t, 1, n)

m2Labels, _, m2Value, m2Temporality, m2IsMonotonic := getMerticProperties(ilm2.Metrics().At(0))
m2Labels, _, m2Value, m2Temporality, m2IsMonotonic := getMetricProperties(ilm2.Metrics().At(0))

signature := timeseriesSignature(ilm2.InstrumentationLibrary().Name(), ilm2.Metrics().At(0), m2Labels)
m, ok := a.registeredMetrics.Load(signature)
require.True(t, ok)

v := m.(*accumulatedValue)
vLabels, vTS, vValue, vTemporality, vIsMonotonic := getMerticProperties(ilm2.Metrics().At(0))
vLabels, vTS, vValue, vTemporality, vIsMonotonic := getMetricProperties(ilm2.Metrics().At(0))

require.Equal(t, v.instrumentationLibrary.Name(), "test")
require.Equal(t, v.value.DataType(), ilm2.Metrics().At(0).DataType())
Expand All @@ -269,23 +269,23 @@ func TestAccumulateMetrics(t *testing.T) {
tt.metric(ts3, 34, ilm3.Metrics())
tt.metric(ts1, 13, ilm3.Metrics())

_, _, m3Value, _, _ := getMerticProperties(ilm3.Metrics().At(1))
_, _, m3Value, _, _ := getMetricProperties(ilm3.Metrics().At(1))

n = a.Accumulate(resourceMetrics3)
require.Equal(t, 2, n)

m, ok = a.registeredMetrics.Load(signature)
require.True(t, ok)
v = m.(*accumulatedValue)
_, vTS, vValue, _, _ = getMerticProperties(v.value)
_, vTS, vValue, _, _ = getMetricProperties(v.value)

require.Equal(t, m3Value, vValue)
require.Equal(t, ts3.Unix(), vTS.Unix())
})
}
}

func getMerticProperties(metric pdata.Metric) (
func getMetricProperties(metric pdata.Metric) (
labels pdata.StringMap,
ts time.Time,
value float64,
Expand All @@ -308,13 +308,13 @@ func getMerticProperties(metric pdata.Metric) (
case pdata.MetricDataTypeGauge:
labels = metric.Gauge().DataPoints().At(0).LabelsMap()
ts = metric.Gauge().DataPoints().At(0).Timestamp().AsTime()
value = metric.Gauge().DataPoints().At(0).Value()
value = metric.Gauge().DataPoints().At(0).DoubleVal()
temporality = pdata.AggregationTemporalityUnspecified
isMonotonic = false
case pdata.MetricDataTypeSum:
labels = metric.Sum().DataPoints().At(0).LabelsMap()
ts = metric.Sum().DataPoints().At(0).Timestamp().AsTime()
value = metric.Sum().DataPoints().At(0).Value()
value = metric.Sum().DataPoints().At(0).DoubleVal()
temporality = metric.Sum().AggregationTemporality()
isMonotonic = metric.Sum().IsMonotonic()
case pdata.MetricDataTypeHistogram:
Expand Down
4 changes: 2 additions & 2 deletions exporter/prometheusexporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *collector) convertDoubleGauge(metric pdata.Metric) (prometheus.Metric,
ip := metric.Gauge().DataPoints().At(0)

desc, labels := c.getMetricMetadata(metric, ip.LabelsMap())
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, ip.Value(), labels...)
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, ip.DoubleVal(), labels...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (c *collector) convertSum(metric pdata.Metric) (prometheus.Metric, error) {
}

desc, labels := c.getMetricMetadata(metric, ip.LabelsMap())
m, err := prometheus.NewConstMetric(desc, metricType, ip.Value(), labels...)
m, err := prometheus.NewConstMetric(desc, metricType, ip.DoubleVal(), labels...)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions exporter/prometheusexporter/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestCollectMetrics(t *testing.T) {
metric.SetDataType(pdata.MetricDataTypeGauge)
metric.SetDescription("test description")
dp := metric.Gauge().DataPoints().AppendEmpty()
dp.SetValue(42.42)
dp.SetDoubleVal(42.42)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestCollectMetrics(t *testing.T) {
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityCumulative)
metric.SetDescription("test description")
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(42.42)
dp.SetDoubleVal(42.42)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestCollectMetrics(t *testing.T) {
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityCumulative)
metric.SetDescription("test description")
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(42.42)
dp.SetDoubleVal(42.42)
dp.LabelsMap().Insert("label_1", "1")
dp.LabelsMap().Insert("label_2", "2")
dp.SetTimestamp(pdata.TimestampFromTime(ts))
Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheusremotewriteexporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func addSingleNumberDataPoint(pt pdata.NumberDataPoint, resource pdata.Resource,
name := getPromMetricName(metric, namespace)
labels := createLabelSet(resource, pt.LabelsMap(), externalLabels, nameStr, name)
sample := &prompb.Sample{
Value: pt.Value(),
Value: pt.DoubleVal(),
// convert ns to ms
Timestamp: convertTimeStamp(pt.Timestamp()),
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/prometheusremotewriteexporter/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func getDoubleGaugeMetric(name string, labels pdata.StringMap, value float64, ts
metric.SetName(name)
metric.SetDataType(pdata.MetricDataTypeGauge)
dp := metric.Gauge().DataPoints().AppendEmpty()
dp.SetValue(value)
dp.SetDoubleVal(value)

labels.Range(func(k string, v string) bool {
dp.LabelsMap().Upsert(k, v)
Expand Down Expand Up @@ -314,7 +314,7 @@ func getSumMetric(name string, labels pdata.StringMap, value float64, ts uint64)
metric.SetDataType(pdata.MetricDataTypeSum)
metric.Sum().SetAggregationTemporality(pdata.AggregationTemporalityCumulative)
dp := metric.Sum().DataPoints().AppendEmpty()
dp.SetValue(value)
dp.SetDoubleVal(value)

labels.Range(func(k string, v string) bool {
dp.LabelsMap().Upsert(k, v)
Expand Down
2 changes: 1 addition & 1 deletion internal/goldendataset/metrics_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func populateDoublePoints(cfg MetricsCfg, pts pdata.NumberDataPointSlice) {
pt := pts.AppendEmpty()
pt.SetStartTimestamp(pdata.Timestamp(cfg.StartTime))
pt.SetTimestamp(getTimestamp(cfg.StartTime, cfg.StepSize, i))
pt.SetValue(float64(cfg.PtVal + i))
pt.SetDoubleVal(float64(cfg.PtVal + i))
populatePtLabels(cfg, pt.LabelsMap())
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/goldendataset/metrics_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestGenDoubleGauge(t *testing.T) {
pts := metric.Gauge().DataPoints()
require.Equal(t, 1, pts.Len())
pt := pts.At(0)
require.EqualValues(t, 1, pt.Value())
require.EqualValues(t, 1, pt.DoubleVal())
}

func getMetric(md pdata.Metrics) pdata.Metric {
Expand Down
2 changes: 1 addition & 1 deletion internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (b *dataBuffer) logNumberDataPoints(ps pdata.NumberDataPointSlice) {

b.logEntry("StartTimestamp: %s", p.StartTimestamp())
b.logEntry("Timestamp: %s", p.Timestamp())
b.logEntry("Value: %f", p.Value())
b.logEntry("Value: %f", p.DoubleVal())
}
}

Expand Down

0 comments on commit e1058b2

Please sign in to comment.