Skip to content

Commit

Permalink
[pkg/golden]: Add option to skip normalizing timestamps when writing …
Browse files Browse the repository at this point in the history
…metrics (#31161)

**Description:** <Describe what has changed.>
* Adds a new WriteMetricsOption type, which can be specified when
calling WriteMetrics
* Adds a WriteMetricsOption for skipping the timestamp normalization
step.

**Link to tracking Issue:** Closes #30919

**Testing:** <Describe what testing was performed and which tests were
added.>
* Unit test added for new functionality
  • Loading branch information
BinaryFissionGames authored Feb 9, 2024
1 parent debaf05 commit 7320350
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .chloggen/feat_optional-metric-timestamp-normalization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/golden

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added an option to skip the metric timestamp normalization for WriteMetrics.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [30919]

change_logs: ["api"]
19 changes: 15 additions & 4 deletions pkg/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func ReadMetrics(filePath string) (pmetric.Metrics, error) {
}

// WriteMetrics writes a pmetric.Metrics to the specified file in YAML format.
func WriteMetrics(t testing.TB, filePath string, metrics pmetric.Metrics) error {
if err := writeMetrics(filePath, metrics); err != nil {
func WriteMetrics(t testing.TB, filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error {
if err := writeMetrics(filePath, metrics, opts...); err != nil {
return err
}
t.Logf("Golden file successfully written to %s.", filePath)
Expand Down Expand Up @@ -68,9 +68,20 @@ func MarshalMetricsYAML(metrics pmetric.Metrics) ([]byte, error) {
}

// writeMetrics writes a pmetric.Metrics to the specified file in YAML format.
func writeMetrics(filePath string, metrics pmetric.Metrics) error {
func writeMetrics(filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error {
optsStruct := writeMetricsOptions{
normalizeTimestamps: true,
}

for _, opt := range opts {
opt(&optsStruct)
}

sortMetrics(metrics)
normalizeTimestamps(metrics)
if optsStruct.normalizeTimestamps {
normalizeTimestamps(metrics)
}

b, err := MarshalMetricsYAML(metrics)
if err != nil {
return err
Expand Down
23 changes: 23 additions & 0 deletions pkg/golden/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ func TestWriteMetrics(t *testing.T) {
require.Equal(t, expectedBytes, actualBytes)
}

func TestWriteMetrics_SkipTimestampNormalization(t *testing.T) {
metricslice := testMetrics()
metrics := pmetric.NewMetrics()
metricslice.CopyTo(metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics())

actualFile := filepath.Join(t.TempDir(), "metrics.yaml")
require.NoError(t, writeMetrics(actualFile, metrics, SkipMetricTimestampNormalization()))

actualBytes, err := os.ReadFile(actualFile)
require.NoError(t, err)

expectedFile := filepath.Join("testdata", "skip-timestamp-norm", "expected.yaml")
expectedBytes, err := os.ReadFile(expectedFile)
require.NoError(t, err)

if runtime.GOOS == "windows" {
// os.ReadFile adds a '\r' that we don't actually expect
expectedBytes = bytes.ReplaceAll(expectedBytes, []byte("\r\n"), []byte("\n"))
}

require.Equal(t, string(expectedBytes), string(actualBytes))
}

func TestReadMetrics(t *testing.T) {
metricslice := testMetrics()
expectedMetrics := pmetric.NewMetrics()
Expand Down
18 changes: 18 additions & 0 deletions pkg/golden/metrics_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package golden // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"

// WriteMetricsOption is an option for the WriteMetrics function
type WriteMetricsOption func(*writeMetricsOptions)

type writeMetricsOptions struct {
normalizeTimestamps bool
}

// SkipMetricTimestampNormalization is an option that skips normalizing timestamps before writing metrics to disk.
func SkipMetricTimestampNormalization() WriteMetricsOption {
return func(wmo *writeMetricsOptions) {
wmo.normalizeTimestamps = false
}
}
66 changes: 66 additions & 0 deletions pkg/golden/testdata/skip-timestamp-norm/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
resourceMetrics:
- resource: {}
scopeMetrics:
- metrics:
- description: multi gauge
gauge:
dataPoints:
- asDouble: 2
attributes:
- key: testKey1
value:
stringValue: teststringvalue1
- key: testKey2
value:
stringValue: testvalue1
timeUnixNano: "11651379494838206464"
- asDouble: 2
attributes:
- key: testKey1
value:
stringValue: teststringvalue2
- key: testKey2
value:
stringValue: testvalue2
timeUnixNano: "11651379494838206464"
name: test gauge multi
unit: "1"
- description: single gauge
gauge:
dataPoints:
- asInt: "2"
attributes:
- key: testKey2
value:
stringValue: teststringvalue2
timeUnixNano: "11651379494838206464"
name: test gauge single
unit: By
- description: multi sum
name: test delta sum multi
sum:
aggregationTemporality: 1
dataPoints:
- asInt: "2"
attributes:
- key: testKey2
value:
stringValue: teststringvalue2
timeUnixNano: "11651379494838206464"
- asInt: "2"
attributes:
- key: testKey2
value:
stringValue: teststringvalue2
timeUnixNano: "11651379494838206464"
unit: s
- description: single sum
name: test cumulative sum single
sum:
aggregationTemporality: 2
dataPoints:
- asDouble: 2
timeUnixNano: "869965261000000001"
isMonotonic: true
unit: 1/s
scope: {}

0 comments on commit 7320350

Please sign in to comment.