Skip to content

Commit

Permalink
Rename some internal funcs to allow the oc to new structs to reuse th…
Browse files Browse the repository at this point in the history
…e names (#1664)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 28, 2020
1 parent 03faf67 commit 88634ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 62 deletions.
60 changes: 26 additions & 34 deletions translator/internaldata/oc_to_old_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ func OCSliceToMetricData(ocmds []consumerdata.MetricsData) dataold.MetricData {
return metricData
}
for _, ocmd := range ocmds {
appendOcToMetriData(ocmd, metricData)
appendOcToMetricData(ocmd, metricData)
}
return metricData
}

// OCToMetricData converts OC data format to MetricData.
func OCToMetricData(md consumerdata.MetricsData) dataold.MetricData {
metricData := dataold.NewMetricData()
appendOcToMetriData(md, metricData)
appendOcToMetricData(md, metricData)
return metricData
}

func appendOcToMetriData(md consumerdata.MetricsData, dest dataold.MetricData) {
func appendOcToMetricData(md consumerdata.MetricsData, dest dataold.MetricData) {
if md.Node == nil && md.Resource == nil && len(md.Metrics) == 0 {
return
}
Expand Down Expand Up @@ -124,39 +124,39 @@ func appendOcToMetriData(md consumerdata.MetricsData, dest dataold.MetricData) {
// Add the metric to the "combinedMetrics". combinedMetrics length is equal
// to combinedMetricCount. The loop above that calculates combinedMetricCount
// has exact same conditions as we have here in this loop.
ocMetricToInternal(ocMetric, combinedMetrics.At(combinedMetricIdx))
ocMetricToOldMetrics(ocMetric, combinedMetrics.At(combinedMetricIdx))
combinedMetricIdx++
} else {
// This metric has a different Resource and must be placed in a different
// ResourceMetrics instance. Create a separate ResourceMetrics item just for this metric
// and store at resourceMetricIdx.
ocMetricToResourceMetrics(ocMetric, md.Node, rms.At(initialRmsLen+resourceMetricIdx))
ocMetricToOldResourceMetrics(ocMetric, md.Node, rms.At(initialRmsLen+resourceMetricIdx))
resourceMetricIdx++
}
}
}

func ocMetricToResourceMetrics(ocMetric *ocmetrics.Metric, node *occommon.Node, out dataold.ResourceMetrics) {
func ocMetricToOldResourceMetrics(ocMetric *ocmetrics.Metric, node *occommon.Node, out dataold.ResourceMetrics) {
ocNodeResourceToInternal(node, ocMetric.Resource, out.Resource())
ilms := out.InstrumentationLibraryMetrics()
ilms.Resize(1)
metrics := ilms.At(0).Metrics()
metrics.Resize(1)
ocMetricToInternal(ocMetric, metrics.At(0))
ocMetricToOldMetrics(ocMetric, metrics.At(0))
}

// ocMetricToInternal conversts ocMetric to internal representation and fill metric
func ocMetricToInternal(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
descriptorToInternal(ocMetric.GetMetricDescriptor(), metric.MetricDescriptor())
setDataPoints(ocMetric, metric)
// ocMetricToOldMetrics conversts ocMetric to internal representation and fill metric
func ocMetricToOldMetrics(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
descriptorToOldMetrics(ocMetric.GetMetricDescriptor(), metric.MetricDescriptor())
setDataPointsToOldMetrics(ocMetric, metric)
}

func descriptorToInternal(ocDescriptor *ocmetrics.MetricDescriptor, descriptor dataold.MetricDescriptor) {
func descriptorToOldMetrics(ocDescriptor *ocmetrics.MetricDescriptor, descriptor dataold.MetricDescriptor) {
if ocDescriptor == nil {
return
}

descriptorType := descriptorTypeToInternal(ocDescriptor.Type)
descriptorType := descriptorTypeToOldMetrics(ocDescriptor.Type)
if descriptorType == invalidMetricType {
return
}
Expand All @@ -168,7 +168,7 @@ func descriptorToInternal(ocDescriptor *ocmetrics.MetricDescriptor, descriptor d
descriptor.SetUnit(ocDescriptor.GetUnit())
}

func descriptorTypeToInternal(t ocmetrics.MetricDescriptor_Type) dataold.MetricType {
func descriptorTypeToOldMetrics(t ocmetrics.MetricDescriptor_Type) dataold.MetricType {
switch t {
case ocmetrics.MetricDescriptor_UNSPECIFIED:
return dataold.MetricTypeInvalid
Expand All @@ -189,8 +189,8 @@ func descriptorTypeToInternal(t ocmetrics.MetricDescriptor_Type) dataold.MetricT
}
}

// setDataPoints converts OC timeseries to internal datapoints based on metric type
func setDataPoints(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
// setDataPointsToOldMetrics converts OC timeseries to internal datapoints based on metric type
func setDataPointsToOldMetrics(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
var int64DataPointsNum, doubleDataPointsNum, histogramDataPointsNum, summaryDataPointsNum int
ocLabelsKeys := ocMetric.GetMetricDescriptor().GetLabelKeys()
ocPointsCount := getPointsCount(ocMetric)
Expand All @@ -215,7 +215,7 @@ func setDataPoints(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
dataPoint := dataPoints.At(int64DataPointsNum)
dataPoint.SetStartTime(startTimestamp)
dataPoint.SetTimestamp(pointTimestamp)
setInt64DataPointValue(dataPoint, point)
dataPoint.SetValue(point.GetInt64Value())
setLabelsMap(ocLabelsKeys, timeseries.GetLabelValues(), dataPoint.LabelsMap())
int64DataPointsNum++

Expand All @@ -227,7 +227,7 @@ func setDataPoints(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
dataPoint := dataPoints.At(doubleDataPointsNum)
dataPoint.SetStartTime(startTimestamp)
dataPoint.SetTimestamp(pointTimestamp)
setDoubleDataPointValue(dataPoint, point)
dataPoint.SetValue(point.GetDoubleValue())
setLabelsMap(ocLabelsKeys, timeseries.GetLabelValues(), dataPoint.LabelsMap())
doubleDataPointsNum++

Expand All @@ -251,7 +251,7 @@ func setDataPoints(ocMetric *ocmetrics.Metric, metric dataold.Metric) {
dataPoint := dataPoints.At(summaryDataPointsNum)
dataPoint.SetStartTime(startTimestamp)
dataPoint.SetTimestamp(pointTimestamp)
setSummaryDataPointValue(dataPoint, point)
setSummaryDataPointValueToOldMetrics(dataPoint, point)
setLabelsMap(ocLabelsKeys, timeseries.GetLabelValues(), dataPoint.LabelsMap())
summaryDataPointsNum++
}
Expand Down Expand Up @@ -296,35 +296,27 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics
}
}

func setInt64DataPointValue(dataPoint dataold.Int64DataPoint, point *ocmetrics.Point) {
dataPoint.SetValue(point.GetInt64Value())
}

func setDoubleDataPointValue(dataPoint dataold.DoubleDataPoint, point *ocmetrics.Point) {
dataPoint.SetValue(point.GetDoubleValue())
}

func setHistogramDataPointValue(dataPoint dataold.HistogramDataPoint, point *ocmetrics.Point) {
distributionValue := point.GetDistributionValue()
dataPoint.SetSum(distributionValue.GetSum())
dataPoint.SetCount(uint64(distributionValue.GetCount()))
histogramBucketsToInternal(distributionValue.GetBuckets(), dataPoint.Buckets())
histogramBucketsToOldMetrics(distributionValue.GetBuckets(), dataPoint.Buckets())
dataPoint.SetExplicitBounds(distributionValue.GetBucketOptions().GetExplicit().GetBounds())
}

func histogramBucketsToInternal(ocBuckets []*ocmetrics.DistributionValue_Bucket, buckets dataold.HistogramBucketSlice) {
func histogramBucketsToOldMetrics(ocBuckets []*ocmetrics.DistributionValue_Bucket, buckets dataold.HistogramBucketSlice) {
buckets.Resize(len(ocBuckets))
for i := 0; i < buckets.Len(); i++ {
bucket := buckets.At(i)
bucket.SetCount(uint64(ocBuckets[i].GetCount()))
if ocBuckets[i].GetExemplar() != nil {
bucket.Exemplar().InitEmpty()
exemplarToInternal(ocBuckets[i].GetExemplar(), bucket.Exemplar())
exemplarToOldMetrics(ocBuckets[i].GetExemplar(), bucket.Exemplar())
}
}
}

func exemplarToInternal(ocExemplar *ocmetrics.DistributionValue_Exemplar, exemplar dataold.HistogramBucketExemplar) {
func exemplarToOldMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exemplar dataold.HistogramBucketExemplar) {
if ocExemplar.GetTimestamp() != nil {
exemplar.SetTimestamp(internal.TimestampToUnixNano(ocExemplar.GetTimestamp()))
}
Expand All @@ -337,14 +329,14 @@ func exemplarToInternal(ocExemplar *ocmetrics.DistributionValue_Exemplar, exempl
}
}

func setSummaryDataPointValue(dataPoint dataold.SummaryDataPoint, point *ocmetrics.Point) {
func setSummaryDataPointValueToOldMetrics(dataPoint dataold.SummaryDataPoint, point *ocmetrics.Point) {
summaryValue := point.GetSummaryValue()
dataPoint.SetSum(summaryValue.GetSum().GetValue())
dataPoint.SetCount(uint64(summaryValue.GetCount().GetValue()))
percentileToInternal(summaryValue.GetSnapshot().GetPercentileValues(), dataPoint.ValueAtPercentiles())
percentileToOldMetrics(summaryValue.GetSnapshot().GetPercentileValues(), dataPoint.ValueAtPercentiles())
}

func percentileToInternal(
func percentileToOldMetrics(
ocPercentiles []*ocmetrics.SummaryValue_Snapshot_ValueAtPercentile,
percentiles dataold.SummaryValueAtPercentileSlice,
) {
Expand Down
56 changes: 28 additions & 28 deletions translator/internaldata/old_metrics_to_oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func MetricDataToOC(md dataold.MetricData) []consumerdata.MetricsData {
if rs.IsNil() {
continue
}
ocResourceMetricsList = append(ocResourceMetricsList, ResourceMetricsToOC(rs))
ocResourceMetricsList = append(ocResourceMetricsList, oldResourceMetricsToOC(rs))
}

return ocResourceMetricsList
}

func ResourceMetricsToOC(rm dataold.ResourceMetrics) consumerdata.MetricsData {
func oldResourceMetricsToOC(rm dataold.ResourceMetrics) consumerdata.MetricsData {
ocMetricsData := consumerdata.MetricsData{}
ocMetricsData.Node, ocMetricsData.Resource = internalResourceToOC(rm.Resource())
ilms := rm.InstrumentationLibraryMetrics()
Expand All @@ -79,7 +79,7 @@ func ResourceMetricsToOC(rm dataold.ResourceMetrics) consumerdata.MetricsData {
if m.IsNil() {
continue
}
ocMetrics = append(ocMetrics, metricToOC(m))
ocMetrics = append(ocMetrics, oldMetricToOC(m))
}
}
if len(ocMetrics) != 0 {
Expand All @@ -88,16 +88,16 @@ func ResourceMetricsToOC(rm dataold.ResourceMetrics) consumerdata.MetricsData {
return ocMetricsData
}

func metricToOC(metric dataold.Metric) *ocmetrics.Metric {
labelKeys := collectLabelKeys(metric)
func oldMetricToOC(metric dataold.Metric) *ocmetrics.Metric {
labelKeys := oldMetricCollectLabelKeys(metric)
return &ocmetrics.Metric{
MetricDescriptor: descriptorToOC(metric.MetricDescriptor(), labelKeys),
Timeseries: dataPointsToTimeseries(metric, labelKeys),
MetricDescriptor: oldMetricDescriptorToOC(metric.MetricDescriptor(), labelKeys),
Timeseries: oldMetricDataPointsToTimeseries(metric, labelKeys),
Resource: nil,
}
}

func collectLabelKeys(metric dataold.Metric) *labelKeys {
func oldMetricCollectLabelKeys(metric dataold.Metric) *labelKeys {
// NOTE: Internal data structure and OpenCensus have different representations of labels:
// - OC has a single "global" ordered list of label keys per metric in the MetricDescriptor;
// then, every data point has an ordered list of label values matching the key index.
Expand Down Expand Up @@ -187,20 +187,20 @@ func addLabelKeys(keySet map[string]struct{}, labels pdata.StringMap) {
})
}

func descriptorToOC(descriptor dataold.MetricDescriptor, labelKeys *labelKeys) *ocmetrics.MetricDescriptor {
func oldMetricDescriptorToOC(descriptor dataold.MetricDescriptor, labelKeys *labelKeys) *ocmetrics.MetricDescriptor {
if descriptor.IsNil() {
return nil
}
return &ocmetrics.MetricDescriptor{
Name: descriptor.Name(),
Description: descriptor.Description(),
Unit: descriptor.Unit(),
Type: descriptorTypeToOC(descriptor.Type()),
Type: oldMetricDescriptorTypeToOC(descriptor.Type()),
LabelKeys: labelKeys.keys,
}
}

func descriptorTypeToOC(t dataold.MetricType) ocmetrics.MetricDescriptor_Type {
func oldMetricDescriptorTypeToOC(t dataold.MetricType) ocmetrics.MetricDescriptor_Type {
switch t {
case dataold.MetricTypeInvalid:
return ocmetrics.MetricDescriptor_UNSPECIFIED
Expand All @@ -221,7 +221,7 @@ func descriptorTypeToOC(t dataold.MetricType) ocmetrics.MetricDescriptor_Type {
}
}

func dataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocmetrics.TimeSeries {
func oldMetricDataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocmetrics.TimeSeries {
length := metric.Int64DataPoints().Len() + metric.DoubleDataPoints().Len() +
metric.HistogramDataPoints().Len() + metric.SummaryDataPoints().Len()
if length == 0 {
Expand All @@ -235,7 +235,7 @@ func dataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocme
if ip.IsNil() {
continue
}
ts := int64PointToOC(ip, labelKeys)
ts := oldMetricInt64PointToOC(ip, labelKeys)
timeseries = append(timeseries, ts)
}
dps := metric.DoubleDataPoints()
Expand All @@ -244,7 +244,7 @@ func dataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocme
if dp.IsNil() {
continue
}
ts := doublePointToOC(dp, labelKeys)
ts := oldMetricDoublePointToOC(dp, labelKeys)
timeseries = append(timeseries, ts)
}
hps := metric.HistogramDataPoints()
Expand All @@ -253,7 +253,7 @@ func dataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocme
if hp.IsNil() {
continue
}
ts := histogramPointToOC(hp, labelKeys)
ts := oldMetricHistogramPointToOC(hp, labelKeys)
timeseries = append(timeseries, ts)
}
sps := metric.SummaryDataPoints()
Expand All @@ -262,14 +262,14 @@ func dataPointsToTimeseries(metric dataold.Metric, labelKeys *labelKeys) []*ocme
if sp.IsNil() {
continue
}
ts := summaryPointToOC(sp, labelKeys)
ts := oldMetricSummaryPointToOC(sp, labelKeys)
timeseries = append(timeseries, ts)
}

return timeseries
}

func int64PointToOC(point dataold.Int64DataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
func oldMetricInt64PointToOC(point dataold.Int64DataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
return &ocmetrics.TimeSeries{
StartTimestamp: internal.UnixNanoToTimestamp(point.StartTime()),
LabelValues: labelValuesToOC(point.LabelsMap(), labelKeys),
Expand All @@ -284,7 +284,7 @@ func int64PointToOC(point dataold.Int64DataPoint, labelKeys *labelKeys) *ocmetri
}
}

func doublePointToOC(point dataold.DoubleDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
func oldMetricDoublePointToOC(point dataold.DoubleDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
return &ocmetrics.TimeSeries{
StartTimestamp: internal.UnixNanoToTimestamp(point.StartTime()),
LabelValues: labelValuesToOC(point.LabelsMap(), labelKeys),
Expand All @@ -299,7 +299,7 @@ func doublePointToOC(point dataold.DoubleDataPoint, labelKeys *labelKeys) *ocmet
}
}

func histogramPointToOC(point dataold.HistogramDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
func oldMetricHistogramPointToOC(point dataold.HistogramDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
return &ocmetrics.TimeSeries{
StartTimestamp: internal.UnixNanoToTimestamp(point.StartTime()),
LabelValues: labelValuesToOC(point.LabelsMap(), labelKeys),
Expand All @@ -311,16 +311,16 @@ func histogramPointToOC(point dataold.HistogramDataPoint, labelKeys *labelKeys)
Count: int64(point.Count()),
Sum: point.Sum(),
SumOfSquaredDeviation: 0,
BucketOptions: histogramExplicitBoundsToOC(point.ExplicitBounds()),
Buckets: histogramBucketsToOC(point.Buckets()),
BucketOptions: oldMetricHistogramExplicitBoundsToOC(point.ExplicitBounds()),
Buckets: oldMetricHistogramBucketsToOC(point.Buckets()),
},
},
},
},
}
}

func histogramExplicitBoundsToOC(bounds []float64) *ocmetrics.DistributionValue_BucketOptions {
func oldMetricHistogramExplicitBoundsToOC(bounds []float64) *ocmetrics.DistributionValue_BucketOptions {
if len(bounds) == 0 {
return nil
}
Expand All @@ -334,7 +334,7 @@ func histogramExplicitBoundsToOC(bounds []float64) *ocmetrics.DistributionValue_
}
}

func histogramBucketsToOC(buckets dataold.HistogramBucketSlice) []*ocmetrics.DistributionValue_Bucket {
func oldMetricHistogramBucketsToOC(buckets dataold.HistogramBucketSlice) []*ocmetrics.DistributionValue_Bucket {
if buckets.Len() == 0 {
return nil
}
Expand All @@ -344,13 +344,13 @@ func histogramBucketsToOC(buckets dataold.HistogramBucketSlice) []*ocmetrics.Dis
bucket := buckets.At(i)
ocBuckets = append(ocBuckets, &ocmetrics.DistributionValue_Bucket{
Count: int64(bucket.Count()),
Exemplar: exemplarToOC(bucket.Exemplar()),
Exemplar: oldMetricExemplarToOC(bucket.Exemplar()),
})
}
return ocBuckets
}

func exemplarToOC(exemplar dataold.HistogramBucketExemplar) *ocmetrics.DistributionValue_Exemplar {
func oldMetricExemplarToOC(exemplar dataold.HistogramBucketExemplar) *ocmetrics.DistributionValue_Exemplar {
if exemplar.IsNil() {
return nil
}
Expand All @@ -374,7 +374,7 @@ func exemplarToOC(exemplar dataold.HistogramBucketExemplar) *ocmetrics.Distribut
}
}

func summaryPointToOC(point dataold.SummaryDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
func oldMetricSummaryPointToOC(point dataold.SummaryDataPoint, labelKeys *labelKeys) *ocmetrics.TimeSeries {
return &ocmetrics.TimeSeries{
StartTimestamp: internal.UnixNanoToTimestamp(point.StartTime()),
LabelValues: labelValuesToOC(point.LabelsMap(), labelKeys),
Expand All @@ -385,15 +385,15 @@ func summaryPointToOC(point dataold.SummaryDataPoint, labelKeys *labelKeys) *ocm
SummaryValue: &ocmetrics.SummaryValue{
Count: int64Value(point.Count()),
Sum: doubleValue(point.Sum()),
Snapshot: percentileToOC(point.ValueAtPercentiles()),
Snapshot: oldMetricPercentileToOC(point.ValueAtPercentiles()),
},
},
},
},
}
}

func percentileToOC(percentiles dataold.SummaryValueAtPercentileSlice) *ocmetrics.SummaryValue_Snapshot {
func oldMetricPercentileToOC(percentiles dataold.SummaryValueAtPercentileSlice) *ocmetrics.SummaryValue_Snapshot {
if percentiles.Len() == 0 {
return nil
}
Expand Down

0 comments on commit 88634ae

Please sign in to comment.