Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize timestamps in log tests #404

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions exporter/collector/integrationtest/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var (
func DiffMetricProtos(t testing.TB, x, y *MetricExpectFixture) string {
x = proto.Clone(x).(*MetricExpectFixture)
y = proto.Clone(y).(*MetricExpectFixture)
normalizeFixture(t, x)
normalizeFixture(t, y)
normalizeMetricFixture(t, x)
normalizeMetricFixture(t, y)

return cmp.Diff(x, y, cmpOptions...)
}
Expand All @@ -44,6 +44,8 @@ func DiffMetricProtos(t testing.TB, x, y *MetricExpectFixture) string {
func DiffLogProtos(t testing.TB, x, y *LogExpectFixture) string {
x = proto.Clone(x).(*LogExpectFixture)
y = proto.Clone(y).(*LogExpectFixture)
normalizeLogFixture(t, x)
normalizeLogFixture(t, y)

return cmp.Diff(x, y, cmpOptions...)
}
22 changes: 18 additions & 4 deletions exporter/collector/integrationtest/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (tc *TestCase) SaveRecordedLogFixtures(
t testing.TB,
fixture *LogExpectFixture,
) {
normalizeLogFixture(t, fixture)

jsonBytes, err := protojson.Marshal(fixture)
require.NoError(t, err)
Expand All @@ -129,6 +130,19 @@ func (tc *TestCase) SaveRecordedLogFixtures(
t.Logf("Updated fixture %v", tc.ExpectFixturePath)
}

// Normalizes timestamps which create noise in the fixture because they can
// vary each test run
func normalizeLogFixture(t testing.TB, fixture *LogExpectFixture) {
for _, req := range fixture.WriteLogEntriesRequests {
for _, entry := range req.Entries {
// Normalize timestamps if they are set
if entry.GetTimestamp() != nil {
entry.Timestamp = &timestamppb.Timestamp{}
}
}
}
}

// Load OTLP metric fixture, test expectation fixtures and modify them so they're suitable for
// testing. Currently, this just updates the timestamps.
func (tc *TestCase) LoadOTLPMetricsInput(
Expand Down Expand Up @@ -236,7 +250,7 @@ func (tc *TestCase) SaveRecordedMetricFixtures(
t testing.TB,
fixture *MetricExpectFixture,
) {
normalizeFixture(t, fixture)
normalizeMetricFixture(t, fixture)

jsonBytes, err := protojson.Marshal(fixture)
require.NoError(t, err)
Expand All @@ -247,9 +261,9 @@ func (tc *TestCase) SaveRecordedMetricFixtures(
t.Logf("Updated fixture %v", tc.ExpectFixturePath)
}

// Normalizes timestamps and removes project ID fields which create noise in the fixture
// because they can vary each test run
func normalizeFixture(t testing.TB, fixture *MetricExpectFixture) {
// Normalizes timestamps which create noise in the fixture because they can
// vary each test run
func normalizeMetricFixture(t testing.TB, fixture *MetricExpectFixture) {
normalizeTimeSeriesReqs(t, fixture.CreateTimeSeriesRequests...)
normalizeTimeSeriesReqs(t, fixture.CreateServiceTimeSeriesRequests...)
normalizeMetricDescriptorReqs(t, fixture.CreateMetricDescriptorRequests...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:36 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -32,7 +32,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:37 +0800] \"GET /lamp.png HTTP/1.1\" 200 51164",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/lamp.png",
Expand All @@ -52,7 +52,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:37 +0800] \"GET /favicon.ico HTTP/1.1\" 200 3990",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/favicon.ico",
Expand All @@ -72,7 +72,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:51 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -92,7 +92,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:52 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -112,7 +112,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:53 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -132,7 +132,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:53 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -152,7 +152,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:53 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -172,7 +172,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:53 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -192,7 +192,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -212,7 +212,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -232,7 +232,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -252,7 +252,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -272,7 +272,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -292,7 +292,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:53:54 +0800] \"GET / HTTP/1.1\" 200 1247",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -312,7 +312,7 @@
}
},
"textPayload": "127.0.0.2 - - [26/Apr/2022:22:54:38 +0800] \"GET / HTTP/1.1\" 200 4429",
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"httpRequest": {
"requestMethod": "GET",
"requestUrl": "/",
Expand All @@ -332,7 +332,7 @@
}
},
"textPayload": "127.0.0.1 - - [26/Apr/2022:22:54:43 +0800] \"-\" 408 -",
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
}
],
"partialSuccess": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"severity": "notice",
"time": "Tue Apr 26 00:46:21.412645 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -32,7 +32,7 @@
"severity": "warn",
"time": "Tue Apr 26 00:46:21.457314 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"severity": "WARNING"
},
{
Expand All @@ -49,7 +49,7 @@
"severity": "notice",
"time": "Tue Apr 26 00:46:21.502940 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -65,7 +65,7 @@
"severity": "notice",
"time": "Tue Apr 26 00:46:21.503003 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -81,7 +81,7 @@
"severity": "notice",
"time": "Tue Apr 26 00:46:38.988423 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -97,7 +97,7 @@
"severity": "notice",
"time": "Tue Apr 26 22:48:34.466058 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -113,7 +113,7 @@
"severity": "warn",
"time": "Tue Apr 26 22:48:34.740290 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z",
"timestamp": "1970-01-01T00:00:00Z",
"severity": "WARNING"
},
{
Expand All @@ -130,7 +130,7 @@
"severity": "notice",
"time": "Tue Apr 26 22:48:35.606223 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
},
{
"logName": "projects/fakeprojectid/logs/apache-error-fixture",
Expand All @@ -146,7 +146,7 @@
"severity": "notice",
"time": "Tue Apr 26 22:48:35.606298 2022"
},
"timestamp": "2022-05-25T21:07:00.969486683Z"
"timestamp": "1970-01-01T00:00:00Z"
}
],
"partialSuccess": true
Expand Down