Skip to content

Commit bb09cbf

Browse files
[pkg/golden] expose writing golden data to files outside of a test (#39673)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Expose methods to write data to file outside of the scope of a test Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 74eeb11 commit bb09cbf

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

.chloggen/golden_write.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/golden
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Expose methods to write data to file outside of the scope of a test
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39673]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
Expose WriteMetricsToFile to write pmetric data to a file outside of the scope of a test.
20+
Expose WriteTracesToFile to write ptrace data to a file outside of the scope of a test
21+
Expose WriteLogsToFile to write plog data to a file outside of the scope of a test
22+
Expose WriteProfilesToFile to write pprofile data to a file outside of the scope of a test
23+
24+
# If your change doesn't affect end users or the exported elements of any package,
25+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
26+
# Optional: The change log or logs in which this entry should be included.
27+
# e.g. '[user]' or '[user, api]'
28+
# Include 'user' if the change is relevant to end users.
29+
# Include 'api' if there is a change to a library API.
30+
# Default: '[user]'
31+
change_logs: [api]

pkg/golden/golden.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ReadMetrics(filePath string) (pmetric.Metrics, error) {
3939

4040
// WriteMetrics writes a pmetric.Metrics to the specified file in YAML format.
4141
func WriteMetrics(tb testing.TB, filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error {
42-
if err := writeMetrics(filePath, metrics, opts...); err != nil {
42+
if err := WriteMetricsToFile(filePath, metrics, opts...); err != nil {
4343
return err
4444
}
4545
tb.Logf("Golden file successfully written to %s.", filePath)
@@ -68,8 +68,9 @@ func MarshalMetricsYAML(metrics pmetric.Metrics) ([]byte, error) {
6868
return b.Bytes(), nil
6969
}
7070

71-
// writeMetrics writes a pmetric.Metrics to the specified file in YAML format.
72-
func writeMetrics(filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error {
71+
// WriteMetricsToFile writes a pmetric.Metrics to the specified file in YAML format.
72+
// Prefer using WriteMetrics in tests.
73+
func WriteMetricsToFile(filePath string, metrics pmetric.Metrics, opts ...WriteMetricsOption) error {
7374
optsStruct := writeMetricsOptions{
7475
normalizeTimestamps: true,
7576
}
@@ -112,7 +113,7 @@ func ReadLogs(filePath string) (plog.Logs, error) {
112113

113114
// WriteLogs writes a plog.Logs to the specified file in YAML format.
114115
func WriteLogs(tb testing.TB, filePath string, logs plog.Logs) error {
115-
if err := writeLogs(filePath, logs); err != nil {
116+
if err := WriteLogsToFile(filePath, logs); err != nil {
116117
return err
117118
}
118119
tb.Logf("Golden file successfully written to %s.", filePath)
@@ -121,8 +122,9 @@ func WriteLogs(tb testing.TB, filePath string, logs plog.Logs) error {
121122
return nil
122123
}
123124

124-
// writeLogs writes a plog.Logs to the specified file in YAML format.
125-
func writeLogs(filePath string, logs plog.Logs) error {
125+
// WriteLogsToFile writes a plog.Logs to the specified file in YAML format.
126+
// Prefer using WriteLogs in tests.
127+
func WriteLogsToFile(filePath string, logs plog.Logs) error {
126128
unmarshaler := &plog.JSONMarshaler{}
127129
fileBytes, err := unmarshaler.MarshalLogs(logs)
128130
if err != nil {
@@ -163,7 +165,7 @@ func ReadTraces(filePath string) (ptrace.Traces, error) {
163165

164166
// WriteTraces writes a ptrace.Traces to the specified file in YAML format.
165167
func WriteTraces(tb testing.TB, filePath string, traces ptrace.Traces) error {
166-
if err := writeTraces(filePath, traces); err != nil {
168+
if err := WriteTracesToFile(filePath, traces); err != nil {
167169
return err
168170
}
169171
tb.Logf("Golden file successfully written to %s.", filePath)
@@ -172,8 +174,9 @@ func WriteTraces(tb testing.TB, filePath string, traces ptrace.Traces) error {
172174
return nil
173175
}
174176

175-
// writeTraces writes a ptrace.Traces to the specified file
176-
func writeTraces(filePath string, traces ptrace.Traces) error {
177+
// WriteTracesToFile writes a ptrace.Traces to the specified file
178+
// Prefer using WriteTraces in tests.
179+
func WriteTracesToFile(filePath string, traces ptrace.Traces) error {
177180
unmarshaler := &ptrace.JSONMarshaler{}
178181
fileBytes, err := unmarshaler.MarshalTraces(traces)
179182
if err != nil {
@@ -214,7 +217,7 @@ func ReadProfiles(filePath string) (pprofile.Profiles, error) {
214217

215218
// WriteProfiles writes a pprofile.Profiles to the specified file in YAML format.
216219
func WriteProfiles(tb testing.TB, filePath string, profiles pprofile.Profiles) error {
217-
if err := writeProfiles(filePath, profiles); err != nil {
220+
if err := WriteProfilesToFile(filePath, profiles); err != nil {
218221
return err
219222
}
220223
tb.Logf("Golden file successfully written to %s.", filePath)
@@ -223,8 +226,9 @@ func WriteProfiles(tb testing.TB, filePath string, profiles pprofile.Profiles) e
223226
return nil
224227
}
225228

226-
// writeProfiles writes a pprofile.Profiles to the specified file in YAML format.
227-
func writeProfiles(filePath string, profiles pprofile.Profiles) error {
229+
// WriteProfilesToFile writes a pprofile.Profiles to the specified file in YAML format.
230+
// Prefer using WriteProfiles in tests.
231+
func WriteProfilesToFile(filePath string, profiles pprofile.Profiles) error {
228232
unmarshaler := &pprofile.JSONMarshaler{}
229233
fileBytes, err := unmarshaler.MarshalProfiles(profiles)
230234
if err != nil {

pkg/golden/golden_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestWriteMetrics(t *testing.T) {
2626
metricslice.CopyTo(metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics())
2727

2828
actualFile := filepath.Join(t.TempDir(), "metrics.yaml")
29-
require.NoError(t, writeMetrics(actualFile, metrics))
29+
require.NoError(t, WriteMetricsToFile(actualFile, metrics))
3030

3131
actualBytes, err := os.ReadFile(actualFile)
3232
require.NoError(t, err)
@@ -49,7 +49,7 @@ func TestWriteMetrics_SkipTimestampNormalization(t *testing.T) {
4949
metricslice.CopyTo(metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics())
5050

5151
actualFile := filepath.Join(t.TempDir(), "metrics.yaml")
52-
require.NoError(t, writeMetrics(actualFile, metrics, SkipMetricTimestampNormalization()))
52+
require.NoError(t, WriteMetricsToFile(actualFile, metrics, SkipMetricTimestampNormalization()))
5353

5454
actualBytes, err := os.ReadFile(actualFile)
5555
require.NoError(t, err)
@@ -83,7 +83,7 @@ func TestRoundTrip(t *testing.T) {
8383
metricslice.CopyTo(expectedMetrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics())
8484

8585
tempDir := filepath.Join(t.TempDir(), "metrics.yaml")
86-
require.NoError(t, writeMetrics(tempDir, expectedMetrics))
86+
require.NoError(t, WriteMetricsToFile(tempDir, expectedMetrics))
8787

8888
actualMetrics, err := ReadMetrics(tempDir)
8989
require.NoError(t, err)
@@ -186,7 +186,7 @@ func TestWriteLogs(t *testing.T) {
186186
logs := CreateTestLogs()
187187

188188
actualFile := filepath.Join(t.TempDir(), "logs.yaml")
189-
require.NoError(t, writeLogs(actualFile, logs))
189+
require.NoError(t, WriteLogsToFile(actualFile, logs))
190190

191191
actualBytes, err := os.ReadFile(actualFile)
192192
require.NoError(t, err)
@@ -207,7 +207,7 @@ func TestLogsRoundTrip(t *testing.T) {
207207
expectedLogs := CreateTestLogs()
208208

209209
tempDir := filepath.Join(t.TempDir(), "logs.yaml")
210-
require.NoError(t, writeLogs(tempDir, expectedLogs))
210+
require.NoError(t, WriteLogsToFile(tempDir, expectedLogs))
211211

212212
actualLogs, err := ReadLogs(tempDir)
213213
require.NoError(t, err)
@@ -251,7 +251,7 @@ func TestWriteTraces(t *testing.T) {
251251
traces := CreateTestTraces()
252252

253253
actualFile := filepath.Join("./testdata/traces-roundtrip", "traces.yaml")
254-
require.NoError(t, writeTraces(actualFile, traces))
254+
require.NoError(t, WriteTracesToFile(actualFile, traces))
255255

256256
actualBytes, err := os.ReadFile(actualFile)
257257
require.NoError(t, err)
@@ -338,7 +338,7 @@ func TestWriteProfiles(t *testing.T) {
338338
profiles := CreateTestProfiles()
339339

340340
actualFile := filepath.Join(t.TempDir(), "profiles.yaml")
341-
require.NoError(t, writeProfiles(actualFile, profiles))
341+
require.NoError(t, WriteProfilesToFile(actualFile, profiles))
342342

343343
actualBytes, err := os.ReadFile(actualFile)
344344
require.NoError(t, err)
@@ -359,7 +359,7 @@ func TestProfilesRoundTrip(t *testing.T) {
359359
expectedProfiles := CreateTestProfiles()
360360

361361
tempDir := filepath.Join(t.TempDir(), "profiles.yaml")
362-
require.NoError(t, writeProfiles(tempDir, expectedProfiles))
362+
require.NoError(t, WriteProfilesToFile(tempDir, expectedProfiles))
363363

364364
actualProfiles, err := ReadProfiles(tempDir)
365365
require.NoError(t, err)

0 commit comments

Comments
 (0)