-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
700 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const schema100 = "http://go.opentelemetry.io/schema/v1.0.0" | ||
|
||
var y2k = time.Unix(0, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano()) // No location. | ||
|
||
func runJSONEncodingTests[T any](decoded T, encoded []byte) func(*testing.T) { | ||
return func(t *testing.T) { | ||
t.Helper() | ||
|
||
t.Run("Unmarshal", runJSONUnmarshalTest(decoded, encoded)) | ||
t.Run("Marshal", runJSONMarshalTest(decoded, encoded)) | ||
} | ||
} | ||
|
||
func runJSONMarshalTest[T any](decoded T, encoded []byte) func(*testing.T) { | ||
return func(t *testing.T) { | ||
t.Helper() | ||
|
||
got, err := json.Marshal(decoded) | ||
require.NoError(t, err) | ||
|
||
var want bytes.Buffer | ||
require.NoError(t, json.Compact(&want, encoded)) | ||
assert.Equal(t, want.String(), string(got)) | ||
} | ||
} | ||
|
||
func runJSONUnmarshalTest[T any](decoded T, encoded []byte) func(*testing.T) { | ||
return func(t *testing.T) { | ||
t.Helper() | ||
|
||
var got T | ||
require.NoError(t, json.Unmarshal(encoded, &got)) | ||
assert.Equal(t, decoded, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import "testing" | ||
|
||
func TestResourceEncoding(t *testing.T) { | ||
res := &Resource{ | ||
Attrs: []Attr{String("key", "val")}, | ||
DroppedAttrs: 10, | ||
} | ||
|
||
t.Run("CamelCase", runJSONEncodingTests(res, []byte(`{ | ||
"attributes": [ | ||
{ | ||
"key": "key", | ||
"value": { | ||
"stringValue": "val" | ||
} | ||
} | ||
], | ||
"droppedAttributesCount": 10 | ||
}`))) | ||
|
||
t.Run("SnakeCase", runJSONUnmarshalTest(res, []byte(`{ | ||
"attributes": [ | ||
{ | ||
"key": "key", | ||
"value": { | ||
"string_value": "val" | ||
} | ||
} | ||
], | ||
"dropped_attributes_count": 10 | ||
}`))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import "testing" | ||
|
||
func TestScopeEncoding(t *testing.T) { | ||
scope := &Scope{ | ||
Name: "go.opentelemetry.io/auto/sdk/telemetry/test", | ||
Version: "v0.0.1", | ||
Attrs: []Attr{String("department", "ops")}, | ||
DroppedAttrs: 1, | ||
} | ||
|
||
t.Run("CamelCase", runJSONEncodingTests(scope, []byte(`{ | ||
"name": "go.opentelemetry.io/auto/sdk/telemetry/test", | ||
"version": "v0.0.1", | ||
"attributes": [ | ||
{ | ||
"key": "department", | ||
"value": { | ||
"stringValue": "ops" | ||
} | ||
} | ||
], | ||
"droppedAttributesCount": 1 | ||
}`))) | ||
|
||
t.Run("SnakeCase", runJSONUnmarshalTest(scope, []byte(`{ | ||
"name": "go.opentelemetry.io/auto/sdk/telemetry/test", | ||
"version": "v0.0.1", | ||
"attributes": [ | ||
{ | ||
"key": "department", | ||
"value": { | ||
"string_value": "ops" | ||
} | ||
} | ||
], | ||
"dropped_attributes_count": 1 | ||
}`))) | ||
} |
Oops, something went wrong.