-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an auto-instrumentable no-op implementation to the
trace
package (
#6203) This copes the `go.opentelemetry.io/auto/sdk` package into the `go.opentelemetry.io/otel/trace` package. This is done to avoid package import cycles and still provide an auto-instrumentable SDK (see open-telemetry/opentelemetry-go-instrumentation#974). ## Overview of changes The code copied is updated with the following changes. The over-all goal is to ensure none of this is exported and follows the `auto/sdk` as close as possible to help maintenance. ### `trace/auto.go` Consolidation of the following into a single file: - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/tracer_provider.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/tracer.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/span.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/limit.go Has the following changes: - `func TracerProvider()` renamed to `newAutoTracerProvider` - `type tracerProvider struct` renamed to `autoTracerProvider` - `type tracer struct` renamed to `autoTracer` - `type span struct` renamed to `autoSpan` - Lint issues addressed based on this repositories configuration (these changes are being back-ported upstream) ### `trace/auto_test.go` Consolidation of the following into a single file: - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/tracer_provider_test.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/tracer_test.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/span_test.go - https://github.com/open-telemetry/opentelemetry-go-instrumentation/blob/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/limit_test.go Has the following changes: - Renames in `trace/auto.go` are applied here - Lint issues addressed based on this repositories configuration (these changes are being back-ported upstream) ### `trace/internal/telemetry` Copied from https://github.com/open-telemetry/opentelemetry-go-instrumentation/tree/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/internal/telemetry - Pacakge vanity URLs added - Lint issues addressed based on this repositories configuration (these changes are being back-ported upstream) - Use of the package name has been updated #### `trace/internal/telemetry/test` Copied from https://github.com/open-telemetry/opentelemetry-go-instrumentation/tree/aea085dd2a3640630ac07a2187cbda9d15d2dd00/sdk/internal/telemetry/test - Module name updated - Documentation updated with new package name - Testing values updated with new package name --------- Co-authored-by: Ron Federman <[email protected]>
- Loading branch information
Showing
25 changed files
with
4,440 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,58 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry" | ||
|
||
// Attr is a key-value pair. | ||
type Attr struct { | ||
Key string `json:"key,omitempty"` | ||
Value Value `json:"value,omitempty"` | ||
} | ||
|
||
// String returns an Attr for a string value. | ||
func String(key, value string) Attr { | ||
return Attr{key, StringValue(value)} | ||
} | ||
|
||
// Int64 returns an Attr for an int64 value. | ||
func Int64(key string, value int64) Attr { | ||
return Attr{key, Int64Value(value)} | ||
} | ||
|
||
// Int returns an Attr for an int value. | ||
func Int(key string, value int) Attr { | ||
return Int64(key, int64(value)) | ||
} | ||
|
||
// Float64 returns an Attr for a float64 value. | ||
func Float64(key string, value float64) Attr { | ||
return Attr{key, Float64Value(value)} | ||
} | ||
|
||
// Bool returns an Attr for a bool value. | ||
func Bool(key string, value bool) Attr { | ||
return Attr{key, BoolValue(value)} | ||
} | ||
|
||
// Bytes returns an Attr for a []byte value. | ||
// The passed slice must not be changed after it is passed. | ||
func Bytes(key string, value []byte) Attr { | ||
return Attr{key, BytesValue(value)} | ||
} | ||
|
||
// Slice returns an Attr for a []Value value. | ||
// The passed slice must not be changed after it is passed. | ||
func Slice(key string, value ...Value) Attr { | ||
return Attr{key, SliceValue(value...)} | ||
} | ||
|
||
// Map returns an Attr for a map value. | ||
// The passed slice must not be changed after it is passed. | ||
func Map(key string, value ...Attr) Attr { | ||
return Attr{key, MapValue(value...)} | ||
} | ||
|
||
// Equal returns if a is equal to b. | ||
func (a Attr) Equal(b Attr) bool { | ||
return a.Key == b.Key && a.Value.Equal(b.Value) | ||
} |
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,156 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import "testing" | ||
|
||
func TestAttrEncoding(t *testing.T) { | ||
attrs := []Attr{ | ||
String("user", "Alice"), | ||
Bool("admin", true), | ||
Int64("floor", -2), | ||
Float64("impact", 0.21362), | ||
Slice("reports", StringValue("Bob"), StringValue("Dave")), | ||
Map("favorites", String("food", "hot dog"), Int("number", 13)), | ||
Bytes("secret", []byte("NUI4RUZGRjc5ODAzODEwM0QyNjlCNjMzODEzRkM2MEM=")), | ||
} | ||
|
||
t.Run("CamelCase", runJSONEncodingTests(attrs, []byte(`[ | ||
{ | ||
"key": "user", | ||
"value": { | ||
"stringValue": "Alice" | ||
} | ||
}, | ||
{ | ||
"key": "admin", | ||
"value": { | ||
"boolValue": true | ||
} | ||
}, | ||
{ | ||
"key": "floor", | ||
"value": { | ||
"intValue": "-2" | ||
} | ||
}, | ||
{ | ||
"key": "impact", | ||
"value": { | ||
"doubleValue": 0.21362 | ||
} | ||
}, | ||
{ | ||
"key": "reports", | ||
"value": { | ||
"arrayValue": { | ||
"values": [ | ||
{ | ||
"stringValue": "Bob" | ||
}, | ||
{ | ||
"stringValue": "Dave" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "favorites", | ||
"value": { | ||
"kvlistValue": { | ||
"values": [ | ||
{ | ||
"key": "food", | ||
"value": { | ||
"stringValue": "hot dog" | ||
} | ||
}, | ||
{ | ||
"key": "number", | ||
"value": { | ||
"intValue": "13" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "secret", | ||
"value": { | ||
"bytesValue": "TlVJNFJVWkdSamM1T0RBek9ERXdNMFF5TmpsQ05qTXpPREV6UmtNMk1FTT0=" | ||
} | ||
} | ||
]`))) | ||
|
||
t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(attrs, []byte(`[ | ||
{ | ||
"key": "user", | ||
"value": { | ||
"string_value": "Alice" | ||
} | ||
}, | ||
{ | ||
"key": "admin", | ||
"value": { | ||
"bool_value": true | ||
} | ||
}, | ||
{ | ||
"key": "floor", | ||
"value": { | ||
"int_value": "-2" | ||
} | ||
}, | ||
{ | ||
"key": "impact", | ||
"value": { | ||
"double_value": 0.21362 | ||
} | ||
}, | ||
{ | ||
"key": "reports", | ||
"value": { | ||
"array_value": { | ||
"values": [ | ||
{ | ||
"string_value": "Bob" | ||
}, | ||
{ | ||
"string_value": "Dave" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "favorites", | ||
"value": { | ||
"kvlist_value": { | ||
"values": [ | ||
{ | ||
"key": "food", | ||
"value": { | ||
"string_value": "hot dog" | ||
} | ||
}, | ||
{ | ||
"key": "number", | ||
"value": { | ||
"int_value": "13" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "secret", | ||
"value": { | ||
"bytes_value": "TlVJNFJVWkdSamM1T0RBek9ERXdNMFF5TmpsQ05qTXpPREV6UmtNMk1FTT0=" | ||
} | ||
} | ||
]`))) | ||
} |
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,70 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var ( | ||
traceID = [16]byte{0x1} | ||
|
||
spanID1 = [8]byte{0x1} | ||
spanID2 = [8]byte{0x2} | ||
|
||
now = time.Now() | ||
nowPlus1 = now.Add(1 * time.Second) | ||
|
||
spanA = &Span{ | ||
TraceID: traceID, | ||
SpanID: spanID2, | ||
ParentSpanID: spanID1, | ||
Flags: 1, | ||
Name: "span-a", | ||
StartTime: now, | ||
EndTime: nowPlus1, | ||
Status: &Status{ | ||
Message: "test status", | ||
Code: StatusCodeOK, | ||
}, | ||
} | ||
|
||
spanB = &Span{} | ||
|
||
scopeSpans = &ScopeSpans{ | ||
Scope: &Scope{ | ||
Name: "TestTracer", | ||
Version: "v0.1.0", | ||
}, | ||
SchemaURL: "http://go.opentelemetry.io/test", | ||
Spans: []*Span{spanA, spanB}, | ||
} | ||
) | ||
|
||
func BenchmarkJSONMarshalUnmarshal(b *testing.B) { | ||
var out ScopeSpans | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
var inBuf bytes.Buffer | ||
enc := json.NewEncoder(&inBuf) | ||
err := enc.Encode(scopeSpans) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
payload := inBuf.Bytes() | ||
|
||
dec := json.NewDecoder(bytes.NewReader(payload)) | ||
err = dec.Decode(&out) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
_ = out | ||
} |
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,8 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/* | ||
Package telemetry provides a lightweight representations of OpenTelemetry | ||
telemetry that is compatible with the OTLP JSON protobuf encoding. | ||
*/ | ||
package telemetry // import "go.opentelemetry.io/otel/trace/internal/telemetry" |
Oops, something went wrong.