From 05799d7b0f80495139524725107a619ea82fe6f7 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Mon, 4 Nov 2024 07:58:14 -0800 Subject: [PATCH] Deprecate the sdk/telemetry pkg Resolve #1234 The `sdk/telemetry` package is no longer used in the `auto` package since the merge of #1207. It is not needed outside of the `sdk` package and needs to be removed prior to stabilization of the `go.opentelemetry.io/auto/sdk` module. --- .github/dependabot.yml | 9 + CHANGELOG.md | 7 + sdk/internal/telemetry/attr.go | 58 +++ sdk/internal/telemetry/attr_test.go | 156 ++++++ sdk/internal/telemetry/bench_test.go | 70 +++ sdk/internal/telemetry/conv_test.go | 50 ++ sdk/internal/telemetry/doc.go | 8 + sdk/internal/telemetry/id.go | 103 ++++ sdk/internal/telemetry/number.go | 67 +++ sdk/internal/telemetry/resource.go | 66 +++ sdk/internal/telemetry/resource_test.go | 37 ++ sdk/internal/telemetry/scope.go | 67 +++ sdk/internal/telemetry/scope_test.go | 43 ++ sdk/internal/telemetry/span.go | 456 ++++++++++++++++++ sdk/internal/telemetry/span_test.go | 200 ++++++++ sdk/internal/telemetry/status.go | 40 ++ .../telemetry/test/conversion_test.go | 243 ++++++++++ sdk/internal/telemetry/test/doc.go | 7 + sdk/internal/telemetry/test/go.mod | 29 ++ sdk/internal/telemetry/test/go.sum | 80 +++ sdk/internal/telemetry/traces.go | 189 ++++++++ sdk/internal/telemetry/traces_test.go | 145 ++++++ sdk/internal/telemetry/value.go | 452 +++++++++++++++++ sdk/telemetry/doc.go | 2 + sdk/telemetry/test/conversion_test.go | 2 +- sdk/telemetry/test/doc.go | 2 + sdk/telemetry/test/go.mod | 1 + sdk/trace.go | 2 +- sdk/trace_test.go | 2 +- versions.yaml | 1 + 30 files changed, 2591 insertions(+), 3 deletions(-) create mode 100644 sdk/internal/telemetry/attr.go create mode 100644 sdk/internal/telemetry/attr_test.go create mode 100644 sdk/internal/telemetry/bench_test.go create mode 100644 sdk/internal/telemetry/conv_test.go create mode 100644 sdk/internal/telemetry/doc.go create mode 100644 sdk/internal/telemetry/id.go create mode 100644 sdk/internal/telemetry/number.go create mode 100644 sdk/internal/telemetry/resource.go create mode 100644 sdk/internal/telemetry/resource_test.go create mode 100644 sdk/internal/telemetry/scope.go create mode 100644 sdk/internal/telemetry/scope_test.go create mode 100644 sdk/internal/telemetry/span.go create mode 100644 sdk/internal/telemetry/span_test.go create mode 100644 sdk/internal/telemetry/status.go create mode 100644 sdk/internal/telemetry/test/conversion_test.go create mode 100644 sdk/internal/telemetry/test/doc.go create mode 100644 sdk/internal/telemetry/test/go.mod create mode 100644 sdk/internal/telemetry/test/go.sum create mode 100644 sdk/internal/telemetry/traces.go create mode 100644 sdk/internal/telemetry/traces_test.go create mode 100644 sdk/internal/telemetry/value.go diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 5557a189b..c39da77df 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -253,6 +253,15 @@ updates: schedule: interval: weekly day: sunday + - package-ecosystem: gomod + directory: /sdk/internal/telemetry/test + labels: + - dependencies + - go + - Skip Changelog + schedule: + interval: weekly + day: sunday - package-ecosystem: gomod directory: /sdk/telemetry/test labels: diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e5ce5ce..67275f89c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,13 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http - Sporadic shutdown deadlock. ([#1220](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1220)) - Only support gRPC status codes for gRPC >= 1.40. ([#1235](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1235)) +### Deprecated + +- The `go.opentelemetry.io/auto/sdk/telemetry` package is deprecated. + This package will be removed in the next release. ([#1238](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1238)) +- The `go.opentelemetry.io/auto/sdk/telemetry/test` module is deprecated. + This module will be removed in the next release. ([#1238](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1238)) + ## [v0.16.0-alpha] - 2024-10-22 ### Added diff --git a/sdk/internal/telemetry/attr.go b/sdk/internal/telemetry/attr.go new file mode 100644 index 000000000..af6ef171f --- /dev/null +++ b/sdk/internal/telemetry/attr.go @@ -0,0 +1,58 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package 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) +} diff --git a/sdk/internal/telemetry/attr_test.go b/sdk/internal/telemetry/attr_test.go new file mode 100644 index 000000000..adbabc303 --- /dev/null +++ b/sdk/internal/telemetry/attr_test.go @@ -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=" + } + } + ]`))) +} diff --git a/sdk/internal/telemetry/bench_test.go b/sdk/internal/telemetry/bench_test.go new file mode 100644 index 000000000..c3e73a5cf --- /dev/null +++ b/sdk/internal/telemetry/bench_test.go @@ -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 +} diff --git a/sdk/internal/telemetry/conv_test.go b/sdk/internal/telemetry/conv_test.go new file mode 100644 index 000000000..6a65b787f --- /dev/null +++ b/sdk/internal/telemetry/conv_test.go @@ -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) + } +} diff --git a/sdk/internal/telemetry/doc.go b/sdk/internal/telemetry/doc.go new file mode 100644 index 000000000..949e2165c --- /dev/null +++ b/sdk/internal/telemetry/doc.go @@ -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 diff --git a/sdk/internal/telemetry/id.go b/sdk/internal/telemetry/id.go new file mode 100644 index 000000000..e854d7e84 --- /dev/null +++ b/sdk/internal/telemetry/id.go @@ -0,0 +1,103 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "encoding/hex" + "errors" + "fmt" +) + +const ( + traceIDSize = 16 + spanIDSize = 8 +) + +// TraceID is a custom data type that is used for all trace IDs. +type TraceID [traceIDSize]byte + +// String returns the hex string representation form of a TraceID. +func (tid TraceID) String() string { + return hex.EncodeToString(tid[:]) +} + +// IsEmpty returns false if id contains at least one non-zero byte. +func (tid TraceID) IsEmpty() bool { + return tid == [traceIDSize]byte{} +} + +// MarshalJSON converts the trace ID into a hex string enclosed in quotes. +func (tid TraceID) MarshalJSON() ([]byte, error) { + if tid.IsEmpty() { + return []byte(`""`), nil + } + return marshalJSON(tid[:]) +} + +// UnmarshalJSON inflates the trace ID from hex string, possibly enclosed in +// quotes. +func (tid *TraceID) UnmarshalJSON(data []byte) error { + *tid = [traceIDSize]byte{} + return unmarshalJSON(tid[:], data) +} + +// SpanID is a custom data type that is used for all span IDs. +type SpanID [spanIDSize]byte + +// String returns the hex string representation form of a SpanID. +func (sid SpanID) String() string { + return hex.EncodeToString(sid[:]) +} + +// IsEmpty returns true if the span ID contains at least one non-zero byte. +func (sid SpanID) IsEmpty() bool { + return sid == [spanIDSize]byte{} +} + +// MarshalJSON converts span ID into a hex string enclosed in quotes. +func (sid SpanID) MarshalJSON() ([]byte, error) { + if sid.IsEmpty() { + return []byte(`""`), nil + } + return marshalJSON(sid[:]) +} + +// UnmarshalJSON decodes span ID from hex string, possibly enclosed in quotes. +func (sid *SpanID) UnmarshalJSON(data []byte) error { + *sid = [spanIDSize]byte{} + return unmarshalJSON(sid[:], data) +} + +// marshalJSON converts id into a hex string enclosed in quotes. +func marshalJSON(id []byte) ([]byte, error) { + // Plus 2 quote chars at the start and end. + hexLen := hex.EncodedLen(len(id)) + 2 + + b := make([]byte, hexLen) + hex.Encode(b[1:hexLen-1], id) + b[0], b[hexLen-1] = '"', '"' + + return b, nil +} + +// unmarshalJSON inflates trace id from hex string, possibly enclosed in quotes. +func unmarshalJSON(dst []byte, src []byte) error { + if l := len(src); l >= 2 && src[0] == '"' && src[l-1] == '"' { + src = src[1 : l-1] + } + nLen := len(src) + if nLen == 0 { + return nil + } + + if len(dst) != hex.DecodedLen(nLen) { + return errors.New("invalid length for ID") + } + + _, err := hex.Decode(dst, src) + if err != nil { + return fmt.Errorf("cannot unmarshal ID from string '%s': %w", string(src), err) + } + return nil +} diff --git a/sdk/internal/telemetry/number.go b/sdk/internal/telemetry/number.go new file mode 100644 index 000000000..29e629d66 --- /dev/null +++ b/sdk/internal/telemetry/number.go @@ -0,0 +1,67 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "encoding/json" + "strconv" +) + +// protoInt64 represents the protobuf encoding of integers which can be either +// strings or integers. +type protoInt64 int64 + +// Int64 returns the protoInt64 as an int64. +func (i *protoInt64) Int64() int64 { return int64(*i) } + +// UnmarshalJSON decodes both strings and integers. +func (i *protoInt64) UnmarshalJSON(data []byte) error { + if data[0] == '"' { + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + parsedInt, err := strconv.ParseInt(str, 10, 64) + if err != nil { + return err + } + *i = protoInt64(parsedInt) + } else { + var parsedInt int64 + if err := json.Unmarshal(data, &parsedInt); err != nil { + return err + } + *i = protoInt64(parsedInt) + } + return nil +} + +// protoUint64 represents the protobuf encoding of integers which can be either +// strings or integers. +type protoUint64 uint64 + +// Int64 returns the protoUint64 as a uint64. +func (i *protoUint64) Uint64() uint64 { return uint64(*i) } + +// UnmarshalJSON decodes both strings and integers. +func (i *protoUint64) UnmarshalJSON(data []byte) error { + if data[0] == '"' { + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + parsedUint, err := strconv.ParseUint(str, 10, 64) + if err != nil { + return err + } + *i = protoUint64(parsedUint) + } else { + var parsedUint uint64 + if err := json.Unmarshal(data, &parsedUint); err != nil { + return err + } + *i = protoUint64(parsedUint) + } + return nil +} diff --git a/sdk/internal/telemetry/resource.go b/sdk/internal/telemetry/resource.go new file mode 100644 index 000000000..cecad8bae --- /dev/null +++ b/sdk/internal/telemetry/resource.go @@ -0,0 +1,66 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" +) + +// Resource information. +type Resource struct { + // Attrs are the set of attributes that describe the resource. Attribute + // keys MUST be unique (it is not allowed to have more than one attribute + // with the same key). + Attrs []Attr `json:"attributes,omitempty"` + // DroppedAttrs is the number of dropped attributes. If the value + // is 0, then no attributes were dropped. + DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into r. +func (r *Resource) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid Resource type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid Resource field: %#v", keyIface) + } + + switch key { + case "attributes": + err = decoder.Decode(&r.Attrs) + case "droppedAttributesCount", "dropped_attributes_count": + err = decoder.Decode(&r.DroppedAttrs) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} diff --git a/sdk/internal/telemetry/resource_test.go b/sdk/internal/telemetry/resource_test.go new file mode 100644 index 000000000..1be443d3a --- /dev/null +++ b/sdk/internal/telemetry/resource_test.go @@ -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/Unmarshal", runJSONUnmarshalTest(res, []byte(`{ + "attributes": [ + { + "key": "key", + "value": { + "string_value": "val" + } + } + ], + "dropped_attributes_count": 10 + }`))) +} diff --git a/sdk/internal/telemetry/scope.go b/sdk/internal/telemetry/scope.go new file mode 100644 index 000000000..b6f2e28d4 --- /dev/null +++ b/sdk/internal/telemetry/scope.go @@ -0,0 +1,67 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" +) + +// Scope is the identifying values of the instrumentation scope. +type Scope struct { + Name string `json:"name,omitempty"` + Version string `json:"version,omitempty"` + Attrs []Attr `json:"attributes,omitempty"` + DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into r. +func (s *Scope) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid Scope type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid Scope field: %#v", keyIface) + } + + switch key { + case "name": + err = decoder.Decode(&s.Name) + case "version": + err = decoder.Decode(&s.Version) + case "attributes": + err = decoder.Decode(&s.Attrs) + case "droppedAttributesCount", "dropped_attributes_count": + err = decoder.Decode(&s.DroppedAttrs) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} diff --git a/sdk/internal/telemetry/scope_test.go b/sdk/internal/telemetry/scope_test.go new file mode 100644 index 000000000..8c9527938 --- /dev/null +++ b/sdk/internal/telemetry/scope_test.go @@ -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/Unmarshal", 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 + }`))) +} diff --git a/sdk/internal/telemetry/span.go b/sdk/internal/telemetry/span.go new file mode 100644 index 000000000..a13a6b733 --- /dev/null +++ b/sdk/internal/telemetry/span.go @@ -0,0 +1,456 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "time" +) + +// A Span represents a single operation performed by a single component of the +// system. +type Span struct { + // A unique identifier for a trace. All spans from the same trace share + // the same `trace_id`. The ID is a 16-byte array. An ID with all zeroes OR + // of length other than 16 bytes is considered invalid (empty string in OTLP/JSON + // is zero-length and thus is also invalid). + // + // This field is required. + TraceID TraceID `json:"traceId,omitempty"` + // A unique identifier for a span within a trace, assigned when the span + // is created. The ID is an 8-byte array. An ID with all zeroes OR of length + // other than 8 bytes is considered invalid (empty string in OTLP/JSON + // is zero-length and thus is also invalid). + // + // This field is required. + SpanID SpanID `json:"spanId,omitempty"` + // trace_state conveys information about request position in multiple distributed tracing graphs. + // It is a trace_state in w3c-trace-context format: https://www.w3.org/TR/trace-context/#tracestate-header + // See also https://github.com/w3c/distributed-tracing for more details about this field. + TraceState string `json:"traceState,omitempty"` + // The `span_id` of this span's parent span. If this is a root span, then this + // field must be empty. The ID is an 8-byte array. + ParentSpanID SpanID `json:"parentSpanId,omitempty"` + // Flags, a bit field. + // + // Bits 0-7 (8 least significant bits) are the trace flags as defined in W3C Trace + // Context specification. To read the 8-bit W3C trace flag, use + // `flags & SPAN_FLAGS_TRACE_FLAGS_MASK`. + // + // See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. + // + // Bits 8 and 9 represent the 3 states of whether a span's parent + // is remote. The states are (unknown, is not remote, is remote). + // To read whether the value is known, use `(flags & SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK) != 0`. + // To read whether the span is remote, use `(flags & SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK) != 0`. + // + // When creating span messages, if the message is logically forwarded from another source + // with an equivalent flags fields (i.e., usually another OTLP span message), the field SHOULD + // be copied as-is. If creating from a source that does not have an equivalent flags field + // (such as a runtime representation of an OpenTelemetry span), the high 22 bits MUST + // be set to zero. + // Readers MUST NOT assume that bits 10-31 (22 most significant bits) will be zero. + // + // [Optional]. + Flags uint32 `json:"flags,omitempty"` + // A description of the span's operation. + // + // For example, the name can be a qualified method name or a file name + // and a line number where the operation is called. A best practice is to use + // the same display name at the same call point in an application. + // This makes it easier to correlate spans in different traces. + // + // This field is semantically required to be set to non-empty string. + // Empty value is equivalent to an unknown span name. + // + // This field is required. + Name string `json:"name"` + // Distinguishes between spans generated in a particular context. For example, + // two spans with the same name may be distinguished using `CLIENT` (caller) + // and `SERVER` (callee) to identify queueing latency associated with the span. + Kind SpanKind `json:"kind,omitempty"` + // start_time_unix_nano is the start time of the span. On the client side, this is the time + // kept by the local machine where the span execution starts. On the server side, this + // is the time when the server's application handler starts running. + // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. + // + // This field is semantically required and it is expected that end_time >= start_time. + StartTime time.Time `json:"startTimeUnixNano,omitempty"` + // end_time_unix_nano is the end time of the span. On the client side, this is the time + // kept by the local machine where the span execution ends. On the server side, this + // is the time when the server application handler stops running. + // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. + // + // This field is semantically required and it is expected that end_time >= start_time. + EndTime time.Time `json:"endTimeUnixNano,omitempty"` + // attributes is a collection of key/value pairs. Note, global attributes + // like server name can be set using the resource API. Examples of attributes: + // + // "/http/user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" + // "/http/server_latency": 300 + // "example.com/myattribute": true + // "example.com/score": 10.239 + // + // The OpenTelemetry API specification further restricts the allowed value types: + // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/README.md#attribute + // Attribute keys MUST be unique (it is not allowed to have more than one + // attribute with the same key). + Attrs []Attr `json:"attributes,omitempty"` + // dropped_attributes_count is the number of attributes that were discarded. Attributes + // can be discarded because their keys are too long or because there are too many + // attributes. If this value is 0, then no attributes were dropped. + DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"` + // events is a collection of Event items. + Events []*SpanEvent `json:"events,omitempty"` + // dropped_events_count is the number of dropped events. If the value is 0, then no + // events were dropped. + DroppedEvents uint32 `json:"droppedEventsCount,omitempty"` + // links is a collection of Links, which are references from this span to a span + // in the same or different trace. + Links []*SpanLink `json:"links,omitempty"` + // dropped_links_count is the number of dropped links after the maximum size was + // enforced. If this value is 0, then no links were dropped. + DroppedLinks uint32 `json:"droppedLinksCount,omitempty"` + // An optional final status for this span. Semantically when Status isn't set, it means + // span's status code is unset, i.e. assume STATUS_CODE_UNSET (code = 0). + Status *Status `json:"status,omitempty"` +} + +// MarshalJSON encodes s into OTLP formatted JSON. +func (s Span) MarshalJSON() ([]byte, error) { + startT := s.StartTime.UnixNano() + if s.StartTime.IsZero() || startT < 0 { + startT = 0 + } + + endT := s.EndTime.UnixNano() + if s.EndTime.IsZero() || endT < 0 { + endT = 0 + } + + // Override non-empty default SpanID marshal and omitempty. + var parentSpanId string + if !s.ParentSpanID.IsEmpty() { + b := make([]byte, hex.EncodedLen(spanIDSize)) + hex.Encode(b, s.ParentSpanID[:]) + parentSpanId = string(b) + } + + type Alias Span + return json.Marshal(struct { + Alias + ParentSpanID string `json:"parentSpanId,omitempty"` + StartTime uint64 `json:"startTimeUnixNano,omitempty"` + EndTime uint64 `json:"endTimeUnixNano,omitempty"` + }{ + Alias: Alias(s), + ParentSpanID: parentSpanId, + StartTime: uint64(startT), + EndTime: uint64(endT), + }) +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into s. +func (s *Span) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid Span type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid Span field: %#v", keyIface) + } + + switch key { + case "traceId", "trace_id": + err = decoder.Decode(&s.TraceID) + case "spanId", "span_id": + err = decoder.Decode(&s.SpanID) + case "traceState", "trace_state": + err = decoder.Decode(&s.TraceState) + case "parentSpanId", "parent_span_id": + err = decoder.Decode(&s.ParentSpanID) + case "flags": + err = decoder.Decode(&s.Flags) + case "name": + err = decoder.Decode(&s.Name) + case "kind": + err = decoder.Decode(&s.Kind) + case "startTimeUnixNano", "start_time_unix_nano": + var val protoUint64 + err = decoder.Decode(&val) + s.StartTime = time.Unix(0, int64(val.Uint64())) + case "endTimeUnixNano", "end_time_unix_nano": + var val protoUint64 + err = decoder.Decode(&val) + s.EndTime = time.Unix(0, int64(val.Uint64())) + case "attributes": + err = decoder.Decode(&s.Attrs) + case "droppedAttributesCount", "dropped_attributes_count": + err = decoder.Decode(&s.DroppedAttrs) + case "events": + err = decoder.Decode(&s.Events) + case "droppedEventsCount", "dropped_events_count": + err = decoder.Decode(&s.DroppedEvents) + case "links": + err = decoder.Decode(&s.Links) + case "droppedLinksCount", "dropped_links_count": + err = decoder.Decode(&s.DroppedLinks) + case "status": + err = decoder.Decode(&s.Status) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} + +// SpanFlags represents constants used to interpret the +// Span.flags field, which is protobuf 'fixed32' type and is to +// be used as bit-fields. Each non-zero value defined in this enum is +// a bit-mask. To extract the bit-field, for example, use an +// expression like: +// +// (span.flags & SPAN_FLAGS_TRACE_FLAGS_MASK) +// +// See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. +// +// Note that Span flags were introduced in version 1.1 of the +// OpenTelemetry protocol. Older Span producers do not set this +// field, consequently consumers should not rely on the absence of a +// particular flag bit to indicate the presence of a particular feature. +type SpanFlags int32 + +const ( + // Bits 0-7 are used for trace flags. + SpanFlagsTraceFlagsMask SpanFlags = 255 + // Bits 8 and 9 are used to indicate that the parent span or link span is remote. + // Bit 8 (`HAS_IS_REMOTE`) indicates whether the value is known. + // Bit 9 (`IS_REMOTE`) indicates whether the span or link is remote. + SpanFlagsContextHasIsRemoteMask SpanFlags = 256 + // SpanFlagsContextHasIsRemoteMask indicates the Span is remote. + SpanFlagsContextIsRemoteMask SpanFlags = 512 +) + +// SpanKind is the type of span. Can be used to specify additional relationships between spans +// in addition to a parent/child relationship. +type SpanKind int32 + +const ( + // Indicates that the span represents an internal operation within an application, + // as opposed to an operation happening at the boundaries. Default value. + SpanKindInternal SpanKind = 1 + // Indicates that the span covers server-side handling of an RPC or other + // remote network request. + SpanKindServer SpanKind = 2 + // Indicates that the span describes a request to some remote service. + SpanKindClient SpanKind = 3 + // Indicates that the span describes a producer sending a message to a broker. + // Unlike CLIENT and SERVER, there is often no direct critical path latency relationship + // between producer and consumer spans. A PRODUCER span ends when the message was accepted + // by the broker while the logical processing of the message might span a much longer time. + SpanKindProducer SpanKind = 4 + // Indicates that the span describes consumer receiving a message from a broker. + // Like the PRODUCER kind, there is often no direct critical path latency relationship + // between producer and consumer spans. + SpanKindConsumer SpanKind = 5 +) + +// Event is a time-stamped annotation of the span, consisting of user-supplied +// text description and key-value pairs. +type SpanEvent struct { + // time_unix_nano is the time the event occurred. + Time time.Time `json:"timeUnixNano,omitempty"` + // name of the event. + // This field is semantically required to be set to non-empty string. + Name string `json:"name,omitempty"` + // attributes is a collection of attribute key/value pairs on the event. + // Attribute keys MUST be unique (it is not allowed to have more than one + // attribute with the same key). + Attrs []Attr `json:"attributes,omitempty"` + // dropped_attributes_count is the number of dropped attributes. If the value is 0, + // then no attributes were dropped. + DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"` +} + +// MarshalJSON encodes e into OTLP formatted JSON. +func (e SpanEvent) MarshalJSON() ([]byte, error) { + t := e.Time.UnixNano() + if e.Time.IsZero() || t < 0 { + t = 0 + } + + type Alias SpanEvent + return json.Marshal(struct { + Alias + Time uint64 `json:"timeUnixNano,omitempty"` + }{ + Alias: Alias(e), + Time: uint64(t), + }) +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into se. +func (se *SpanEvent) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid SpanEvent type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid SpanEvent field: %#v", keyIface) + } + + switch key { + case "timeUnixNano", "time_unix_nano": + var val protoUint64 + err = decoder.Decode(&val) + se.Time = time.Unix(0, int64(val.Uint64())) + case "name": + err = decoder.Decode(&se.Name) + case "attributes": + err = decoder.Decode(&se.Attrs) + case "droppedAttributesCount", "dropped_attributes_count": + err = decoder.Decode(&se.DroppedAttrs) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} + +// A pointer from the current span to another span in the same trace or in a +// different trace. For example, this can be used in batching operations, +// where a single batch handler processes multiple requests from different +// traces or when the handler receives a request from a different project. +type SpanLink struct { + // A unique identifier of a trace that this linked span is part of. The ID is a + // 16-byte array. + TraceID TraceID `json:"traceId,omitempty"` + // A unique identifier for the linked span. The ID is an 8-byte array. + SpanID SpanID `json:"spanId,omitempty"` + // The trace_state associated with the link. + TraceState string `json:"traceState,omitempty"` + // attributes is a collection of attribute key/value pairs on the link. + // Attribute keys MUST be unique (it is not allowed to have more than one + // attribute with the same key). + Attrs []Attr `json:"attributes,omitempty"` + // dropped_attributes_count is the number of dropped attributes. If the value is 0, + // then no attributes were dropped. + DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"` + // Flags, a bit field. + // + // Bits 0-7 (8 least significant bits) are the trace flags as defined in W3C Trace + // Context specification. To read the 8-bit W3C trace flag, use + // `flags & SPAN_FLAGS_TRACE_FLAGS_MASK`. + // + // See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. + // + // Bits 8 and 9 represent the 3 states of whether the link is remote. + // The states are (unknown, is not remote, is remote). + // To read whether the value is known, use `(flags & SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK) != 0`. + // To read whether the link is remote, use `(flags & SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK) != 0`. + // + // Readers MUST NOT assume that bits 10-31 (22 most significant bits) will be zero. + // When creating new spans, bits 10-31 (most-significant 22-bits) MUST be zero. + // + // [Optional]. + Flags uint32 `json:"flags,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into sl. +func (sl *SpanLink) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid SpanLink type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid SpanLink field: %#v", keyIface) + } + + switch key { + case "traceId", "trace_id": + err = decoder.Decode(&sl.TraceID) + case "spanId", "span_id": + err = decoder.Decode(&sl.SpanID) + case "traceState", "trace_state": + err = decoder.Decode(&sl.TraceState) + case "attributes": + err = decoder.Decode(&sl.Attrs) + case "droppedAttributesCount", "dropped_attributes_count": + err = decoder.Decode(&sl.DroppedAttrs) + case "flags": + err = decoder.Decode(&sl.Flags) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} diff --git a/sdk/internal/telemetry/span_test.go b/sdk/internal/telemetry/span_test.go new file mode 100644 index 000000000..3192d15bb --- /dev/null +++ b/sdk/internal/telemetry/span_test.go @@ -0,0 +1,200 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "testing" + "time" +) + +func TestSpanEncoding(t *testing.T) { + span := &Span{ + TraceID: [16]byte{0x1}, + SpanID: [8]byte{0x2}, + TraceState: "test=a", + ParentSpanID: [8]byte{0x1}, + Flags: 1, + Name: "span.a", + Kind: SpanKindClient, + StartTime: y2k, + EndTime: y2k.Add(time.Second), + Attrs: []Attr{String("key", "val")}, + DroppedAttrs: 2, + Events: []*SpanEvent{{ + Name: "name", + }}, + DroppedEvents: 3, + Links: []*SpanLink{{ + TraceID: TraceID{0x2}, + SpanID: SpanID{0x1}, + }}, + DroppedLinks: 4, + Status: &Status{ + Message: "okay", + Code: StatusCodeOK, + }, + } + + t.Run("CamelCase", runJSONEncodingTests(span, []byte(`{ + "traceId": "01000000000000000000000000000000", + "spanId": "0200000000000000", + "traceState": "test=a", + "flags": 1, + "name": "span.a", + "kind": 3, + "attributes": [ + { + "key": "key", + "value": { + "stringValue": "val" + } + } + ], + "droppedAttributesCount": 2, + "events": [ + { + "name": "name" + } + ], + "droppedEventsCount": 3, + "links": [ + { + "traceId": "02000000000000000000000000000000", + "spanId": "0100000000000000" + } + ], + "droppedLinksCount": 4, + "status": { + "message": "okay", + "code": 1 + }, + "parentSpanId": "0100000000000000", + "startTimeUnixNano": 946684800000000000, + "endTimeUnixNano": 946684801000000000 + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(span, []byte(`{ + "trace_id": "01000000000000000000000000000000", + "span_id": "0200000000000000", + "trace_state": "test=a", + "flags": 1, + "name": "span.a", + "kind": 3, + "attributes": [ + { + "key": "key", + "value": { + "string_value": "val" + } + } + ], + "dropped_attributes_count": 2, + "events": [ + { + "name": "name" + } + ], + "dropped_events_count": 3, + "links": [ + { + "trace_id": "02000000000000000000000000000000", + "span_id": "0100000000000000" + } + ], + "dropped_links_count": 4, + "status": { + "message": "okay", + "code": 1 + }, + "parent_span_id": "0100000000000000", + "start_time_unix_nano": 946684800000000000, + "end_time_unix_nano": 946684801000000000 + }`))) + + t.Run("RequiredFields", runJSONMarshalTest(new(Span), []byte(`{ + "traceId": "", + "spanId": "", + "name": "" + }`))) +} + +func TestSpanEventEncoding(t *testing.T) { + event := &SpanEvent{ + Time: y2k.Add(10 * time.Microsecond), + Name: "span.event", + Attrs: []Attr{Float64("impact", 0.4372)}, + DroppedAttrs: 2, + } + + t.Run("CamelCase", runJSONEncodingTests(event, []byte(`{ + "name": "span.event", + "attributes": [ + { + "key": "impact", + "value": { + "doubleValue": 0.4372 + } + } + ], + "droppedAttributesCount": 2, + "timeUnixNano": 946684800000010000 + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(event, []byte(`{ + "name": "span.event", + "attributes": [ + { + "key": "impact", + "value": { + "double_value": 0.4372 + } + } + ], + "dropped_attributes_count": 2, + "time_unix_nano": 946684800000010000 + }`))) +} + +func TestSpanLinkEncoding(t *testing.T) { + link := &SpanLink{ + TraceID: TraceID{0x2}, + SpanID: SpanID{0x1}, + TraceState: "test=green", + Attrs: []Attr{Int("queue", 17)}, + DroppedAttrs: 8, + Flags: 1, + } + + t.Run("CamelCase", runJSONEncodingTests(link, []byte(`{ + "traceId": "02000000000000000000000000000000", + "spanId": "0100000000000000", + "traceState": "test=green", + "attributes": [ + { + "key": "queue", + "value": { + "intValue": "17" + } + } + ], + "droppedAttributesCount": 8, + "flags": 1 + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(link, []byte(`{ + "trace_id": "02000000000000000000000000000000", + "span_id": "0100000000000000", + "trace_state": "test=green", + "attributes": [ + { + "key": "queue", + "value": { + "int_value": "17" + } + } + ], + "dropped_attributes_count": 8, + "flags": 1 + }`))) +} diff --git a/sdk/internal/telemetry/status.go b/sdk/internal/telemetry/status.go new file mode 100644 index 000000000..1217776ea --- /dev/null +++ b/sdk/internal/telemetry/status.go @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +// For the semantics of status codes see +// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status +type StatusCode int32 + +const ( + // The default status. + StatusCodeUnset StatusCode = 0 + // The Span has been validated by an Application developer or Operator to + // have completed successfully. + StatusCodeOK StatusCode = 1 + // The Span contains an error. + StatusCodeError StatusCode = 2 +) + +var statusCodeStrings = []string{ + "Unset", + "OK", + "Error", +} + +func (s StatusCode) String() string { + if s >= 0 && int(s) < len(statusCodeStrings) { + return statusCodeStrings[s] + } + return "" +} + +// The Status type defines a logical error model that is suitable for different +// programming environments, including REST APIs and RPC APIs. +type Status struct { + // A developer-facing human readable error message. + Message string `json:"message,omitempty"` + // The status code. + Code StatusCode `json:"code,omitempty"` +} diff --git a/sdk/internal/telemetry/test/conversion_test.go b/sdk/internal/telemetry/test/conversion_test.go new file mode 100644 index 000000000..2a7cf597a --- /dev/null +++ b/sdk/internal/telemetry/test/conversion_test.go @@ -0,0 +1,243 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package test + +import ( + "bytes" + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/ptrace" + + "go.opentelemetry.io/auto/sdk/internal/telemetry" +) + +var ( + y2k = time.Unix(0, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano()) // No location. + + attrsA = []telemetry.Attr{ + telemetry.String("user", "Alice"), + telemetry.Bool("admin", true), + telemetry.Int64("floor", -2), + telemetry.Float64("impact", 0.21362), + telemetry.Slice( + "reports", + telemetry.StringValue("Bob"), + telemetry.StringValue("Dave"), + ), + telemetry.Map( + "favorites", + telemetry.String("food", "hot dog"), + telemetry.Int("number", 13), + ), + telemetry.Bytes( + "secret", + []byte("NUI4RUZGRjc5ODAzODEwM0QyNjlCNjMzODEzRkM2MEM="), + ), + } + pAttrsA = func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("user", "Alice") + m.PutBool("admin", true) + m.PutInt("floor", -2) + m.PutDouble("impact", 0.21362) + + s := m.PutEmptySlice("reports") + s.AppendEmpty().SetStr("Bob") + s.AppendEmpty().SetStr("Dave") + + fav := m.PutEmptyMap("favorites") + fav.PutStr("food", "hot dog") + fav.PutInt("number", 13) + + sec := m.PutEmptyBytes("secret") + sec.FromRaw([]byte("NUI4RUZGRjc5ODAzODEwM0QyNjlCNjMzODEzRkM2MEM=")) + + return m + }() + + link = &telemetry.SpanLink{ + TraceID: telemetry.TraceID{0x2}, + SpanID: telemetry.SpanID{0x1}, + TraceState: "test=green", + Attrs: []telemetry.Attr{telemetry.Int("queue", 17)}, + DroppedAttrs: 8, + Flags: 1, + } + pLink = func() ptrace.SpanLink { + l := ptrace.NewSpanLink() + l.SetTraceID(pcommon.TraceID{0x2}) + l.SetSpanID(pcommon.SpanID{0x1}) + l.TraceState().FromRaw("test=green") + l.Attributes().PutInt("queue", 17) + l.SetDroppedAttributesCount(8) + l.SetFlags(1) + return l + }() + + event = &telemetry.SpanEvent{ + Time: y2k.Add(10 * time.Microsecond), + Name: "span.event", + Attrs: []telemetry.Attr{telemetry.Float64("impact", 0.4372)}, + DroppedAttrs: 2, + } + prevent = func() ptrace.SpanEvent { + e := ptrace.NewSpanEvent() + e.SetTimestamp(pcommon.NewTimestampFromTime(y2k.Add(10 * time.Microsecond))) + e.SetName("span.event") + e.Attributes().PutDouble("impact", 0.4372) + e.SetDroppedAttributesCount(2) + return e + }() + + spanA = &telemetry.Span{ + TraceID: [16]byte{0x1}, + SpanID: [8]byte{0x2}, + TraceState: "test=a", + ParentSpanID: [8]byte{0x1}, + Flags: 1, + Name: "span.a", + Kind: telemetry.SpanKindClient, + StartTime: y2k, + EndTime: y2k.Add(time.Second), + Attrs: attrsA, + DroppedAttrs: 2, + Events: []*telemetry.SpanEvent{event}, + DroppedEvents: 3, + Links: []*telemetry.SpanLink{link}, + DroppedLinks: 4, + Status: &telemetry.Status{ + Message: "okay", + Code: telemetry.StatusCodeOK, + }, + } + pSpanA = func() ptrace.Span { + s := ptrace.NewSpan() + s.SetTraceID(pcommon.TraceID([16]byte{0x1})) + s.SetSpanID(pcommon.SpanID([8]byte{0x2})) + + ts := s.TraceState() + ts.FromRaw("test=a") + + s.SetParentSpanID(pcommon.SpanID([8]byte{0x1})) + s.SetFlags(1) + s.SetName("span.a") + s.SetKind(ptrace.SpanKindClient) + s.SetStartTimestamp(pcommon.NewTimestampFromTime(y2k)) + s.SetEndTimestamp(pcommon.NewTimestampFromTime(y2k.Add(time.Second))) + pAttrsA.CopyTo(s.Attributes()) + s.SetDroppedAttributesCount(2) + prevent.CopyTo(s.Events().AppendEmpty()) + s.SetDroppedEventsCount(3) + pLink.CopyTo(s.Links().AppendEmpty()) + s.SetDroppedLinksCount(4) + + stat := s.Status() + stat.SetMessage("okay") + stat.SetCode(ptrace.StatusCodeOk) + + return s + }() + schema100 = "http://go.opentelemetry.io/schema/v1.0.0" + + scope = &telemetry.Scope{ + Name: "go.opentelemetry.io/auto/sdk/telemetry/test", + Version: "v0.0.1", + Attrs: []telemetry.Attr{telemetry.String("department", "ops")}, + DroppedAttrs: 1, + } + pScope = func() pcommon.InstrumentationScope { + s := pcommon.NewInstrumentationScope() + s.SetName("go.opentelemetry.io/auto/sdk/telemetry/test") + s.SetVersion("v0.0.1") + s.Attributes().PutStr("department", "ops") + s.SetDroppedAttributesCount(1) + return s + }() + + scopeSpans = &telemetry.ScopeSpans{ + Scope: scope, + Spans: []*telemetry.Span{spanA}, + SchemaURL: schema100, + } + pScopeSpans = func() ptrace.ScopeSpans { + s := ptrace.NewScopeSpans() + pSpanA.CopyTo(s.Spans().AppendEmpty()) + pScope.CopyTo(s.Scope()) + s.SetSchemaUrl(schema100) + return s + }() + + res = telemetry.Resource{ + Attrs: []telemetry.Attr{ + telemetry.String("host", "hal"), + telemetry.Int("id", 42), + }, + DroppedAttrs: 100, + } + press = func() pcommon.Resource { + r := pcommon.NewResource() + r.Attributes().PutStr("host", "hal") + r.Attributes().PutInt("id", 42) + r.SetDroppedAttributesCount(100) + return r + }() + + resSpans = &telemetry.ResourceSpans{ + Resource: res, + SchemaURL: schema100, + ScopeSpans: []*telemetry.ScopeSpans{scopeSpans}, + } + pResSpans = func() ptrace.ResourceSpans { + rs := ptrace.NewResourceSpans() + press.CopyTo(rs.Resource()) + pScopeSpans.CopyTo(rs.ScopeSpans().AppendEmpty()) + rs.SetSchemaUrl(schema100) + return rs + }() + + traces = telemetry.Traces{ + ResourceSpans: []*telemetry.ResourceSpans{ + resSpans, + }, + } + pTraces = func() ptrace.Traces { + traces := ptrace.NewTraces() + pResSpans.CopyTo(traces.ResourceSpans().AppendEmpty()) + return traces + }() +) + +func TestDecode(t *testing.T) { + var enc ptrace.JSONMarshaler + b, err := enc.MarshalTraces(pTraces) + require.NoError(t, err) + + t.Log(string(b)) // This helps when test fails to understand what is being decoded. + + var got telemetry.Traces + dec := json.NewDecoder(bytes.NewReader(b)) + require.NoError(t, dec.Decode(&got)) + + assert.Equal(t, traces, got) +} + +func TestEncode(t *testing.T) { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + require.NoError(t, enc.Encode(traces)) + + data := buf.Bytes() + t.Log(string(data)) // This helps when test fails to understand what how the data has been encoded. + + var dec ptrace.JSONUnmarshaler + got, err := dec.UnmarshalTraces(data) + require.NoError(t, err) + + assert.Equal(t, pTraces, got) +} diff --git a/sdk/internal/telemetry/test/doc.go b/sdk/internal/telemetry/test/doc.go new file mode 100644 index 000000000..6143785f6 --- /dev/null +++ b/sdk/internal/telemetry/test/doc.go @@ -0,0 +1,7 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package test provides isolated testing for +// go.opentelemetry.io/auto/sdk/telemetry. It exists to ensure that package is +// tested without importing any OTLP or pdata dependencies. +package test diff --git a/sdk/internal/telemetry/test/go.mod b/sdk/internal/telemetry/test/go.mod new file mode 100644 index 000000000..839a98735 --- /dev/null +++ b/sdk/internal/telemetry/test/go.mod @@ -0,0 +1,29 @@ +module go.opentelemetry.io/auto/sdk/telemetry/test + +go 1.22.0 + +require ( + github.com/stretchr/testify v1.9.0 + go.opentelemetry.io/auto/sdk v0.1.0-alpha + go.opentelemetry.io/collector/pdata v1.18.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect + google.golang.org/grpc v1.67.1 // indirect + google.golang.org/protobuf v1.35.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace go.opentelemetry.io/auto/sdk => ../../.. diff --git a/sdk/internal/telemetry/test/go.sum b/sdk/internal/telemetry/test/go.sum new file mode 100644 index 000000000..9c58a0c3e --- /dev/null +++ b/sdk/internal/telemetry/test/go.sum @@ -0,0 +1,80 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opentelemetry.io/collector/pdata v1.18.0 h1:/yg2rO2dxqDM2p6GutsMCxXN6sKlXwyIz/ZYyUPONBg= +go.opentelemetry.io/collector/pdata v1.18.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= +google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= +google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= +google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/sdk/internal/telemetry/traces.go b/sdk/internal/telemetry/traces.go new file mode 100644 index 000000000..69a348f0f --- /dev/null +++ b/sdk/internal/telemetry/traces.go @@ -0,0 +1,189 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" +) + +// Traces represents the traces data that can be stored in a persistent storage, +// OR can be embedded by other protocols that transfer OTLP traces data but do +// not implement the OTLP protocol. +// +// The main difference between this message and collector protocol is that +// in this message there will not be any "control" or "metadata" specific to +// OTLP protocol. +// +// When new fields are added into this message, the OTLP request MUST be updated +// as well. +type Traces struct { + // An array of ResourceSpans. + // For data coming from a single resource this array will typically contain + // one element. Intermediary nodes that receive data from multiple origins + // typically batch the data before forwarding further and in that case this + // array will contain multiple elements. + ResourceSpans []*ResourceSpans `json:"resourceSpans,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into td. +func (td *Traces) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid TracesData type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid TracesData field: %#v", keyIface) + } + + switch key { + case "resourceSpans", "resource_spans": + err = decoder.Decode(&td.ResourceSpans) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} + +// A collection of ScopeSpans from a Resource. +type ResourceSpans struct { + // The resource for the spans in this message. + // If this field is not set then no resource info is known. + Resource Resource `json:"resource"` + // A list of ScopeSpans that originate from a resource. + ScopeSpans []*ScopeSpans `json:"scopeSpans,omitempty"` + // This schema_url applies to the data in the "resource" field. It does not apply + // to the data in the "scope_spans" field which have their own schema_url field. + SchemaURL string `json:"schemaUrl,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into rs. +func (rs *ResourceSpans) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid ResourceSpans type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid ResourceSpans field: %#v", keyIface) + } + + switch key { + case "resource": + err = decoder.Decode(&rs.Resource) + case "scopeSpans", "scope_spans": + err = decoder.Decode(&rs.ScopeSpans) + case "schemaUrl", "schema_url": + err = decoder.Decode(&rs.SchemaURL) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} + +// A collection of Spans produced by an InstrumentationScope. +type ScopeSpans struct { + // The instrumentation scope information for the spans in this message. + // Semantically when InstrumentationScope isn't set, it is equivalent with + // an empty instrumentation scope name (unknown). + Scope *Scope `json:"scope"` + // A list of Spans that originate from an instrumentation scope. + Spans []*Span `json:"spans,omitempty"` + // The Schema URL, if known. This is the identifier of the Schema that the span data + // is recorded in. To learn more about Schema URL see + // https://opentelemetry.io/docs/specs/otel/schemas/#schema-url + // This schema_url applies to all spans and span events in the "spans" field. + SchemaURL string `json:"schemaUrl,omitempty"` +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into ss. +func (ss *ScopeSpans) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid ScopeSpans type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid ScopeSpans field: %#v", keyIface) + } + + switch key { + case "scope": + err = decoder.Decode(&ss.Scope) + case "spans": + err = decoder.Decode(&ss.Spans) + case "schemaUrl", "schema_url": + err = decoder.Decode(&ss.SchemaURL) + default: + // Skip unknown. + } + + if err != nil { + return err + } + } + return nil +} diff --git a/sdk/internal/telemetry/traces_test.go b/sdk/internal/telemetry/traces_test.go new file mode 100644 index 000000000..7e247f92d --- /dev/null +++ b/sdk/internal/telemetry/traces_test.go @@ -0,0 +1,145 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry + +import ( + "testing" + "time" +) + +func TestTracesEncoding(t *testing.T) { + traces := &Traces{ + ResourceSpans: []*ResourceSpans{{}}, + } + + t.Run("CamelCase", runJSONEncodingTests(traces, []byte(`{ + "resourceSpans": [ + { + "resource": {} + } + ] + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(traces, []byte(`{ + "resource_spans": [ + { + "resource": {} + } + ] + }`))) +} + +func TestResourceSpansEncoding(t *testing.T) { + rs := &ResourceSpans{ + Resource: Resource{ + Attrs: []Attr{String("key", "val")}, + }, + ScopeSpans: []*ScopeSpans{{}}, + SchemaURL: schema100, + } + + t.Run("CamelCase", runJSONEncodingTests(rs, []byte(`{ + "resource": { + "attributes": [ + { + "key": "key", + "value": { + "stringValue": "val" + } + } + ] + }, + "scopeSpans": [ + { + "scope": null + } + ], + "schemaUrl": "http://go.opentelemetry.io/schema/v1.0.0" + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(rs, []byte(`{ + "resource": { + "attributes": [ + { + "key": "key", + "value": { + "string_value": "val" + } + } + ] + }, + "scope_spans": [ + { + "scope": null + } + ], + "schema_url": "http://go.opentelemetry.io/schema/v1.0.0" + }`))) +} + +func TestScopeSpansEncoding(t *testing.T) { + ss := &ScopeSpans{ + Scope: &Scope{Name: "scope"}, + Spans: []*Span{{ + TraceID: [16]byte{0x1}, + SpanID: [8]byte{0x2}, + Name: "A", + StartTime: y2k, + EndTime: y2k.Add(time.Second), + }, { + TraceID: [16]byte{0x1}, + SpanID: [8]byte{0x3}, + Name: "B", + StartTime: y2k.Add(time.Second), + EndTime: y2k.Add(2 * time.Second), + }}, + SchemaURL: schema100, + } + + t.Run("CamelCase", runJSONEncodingTests(ss, []byte(`{ + "scope": { + "name": "scope" + }, + "spans": [ + { + "traceId": "01000000000000000000000000000000", + "spanId": "0200000000000000", + "name": "A", + "startTimeUnixNano": 946684800000000000, + "endTimeUnixNano": 946684801000000000 + }, + { + "traceId": "01000000000000000000000000000000", + "spanId": "0300000000000000", + "name": "B", + "startTimeUnixNano": 946684801000000000, + "endTimeUnixNano": 946684802000000000 + } + ], + "schemaUrl": "http://go.opentelemetry.io/schema/v1.0.0" + }`))) + + t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(ss, []byte(`{ + "scope": { + "name": "scope" + }, + "spans": [ + { + "trace_id": "01000000000000000000000000000000", + "span_id": "0200000000000000", + "name": "A", + "start_time_unix_nano": 946684800000000000, + "end_time_unix_nano": 946684801000000000 + }, + { + "trace_id": "01000000000000000000000000000000", + "span_id": "0300000000000000", + "name": "B", + "start_time_unix_nano": 946684801000000000, + "end_time_unix_nano": 946684802000000000 + } + ], + "schema_url": "http://go.opentelemetry.io/schema/v1.0.0" + }`))) +} diff --git a/sdk/internal/telemetry/value.go b/sdk/internal/telemetry/value.go new file mode 100644 index 000000000..0dd01b063 --- /dev/null +++ b/sdk/internal/telemetry/value.go @@ -0,0 +1,452 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +//go:generate stringer -type=ValueKind -trimprefix=ValueKind + +package telemetry + +import ( + "bytes" + "cmp" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "math" + "slices" + "strconv" + "unsafe" +) + +// A Value represents a structured value. +// A zero value is valid and represents an empty value. +type Value struct { + // Ensure forward compatibility by explicitly making this not comparable. + noCmp [0]func() //nolint: unused // This is indeed used. + + // num holds the value for Int64, Float64, and Bool. It holds the length + // for String, Bytes, Slice, Map. + num uint64 + // any holds either the KindBool, KindInt64, KindFloat64, stringptr, + // bytesptr, sliceptr, or mapptr. If KindBool, KindInt64, or KindFloat64 + // then the value of Value is in num as described above. Otherwise, it + // contains the value wrapped in the appropriate type. + any any +} + +type ( + // sliceptr represents a value in Value.any for KindString Values. + stringptr *byte + // bytesptr represents a value in Value.any for KindBytes Values. + bytesptr *byte + // sliceptr represents a value in Value.any for KindSlice Values. + sliceptr *Value + // mapptr represents a value in Value.any for KindMap Values. + mapptr *Attr +) + +// ValueKind is the kind of a [Value]. +type ValueKind int + +// ValueKind values. +const ( + ValueKindEmpty ValueKind = iota + ValueKindBool + ValueKindFloat64 + ValueKindInt64 + ValueKindString + ValueKindBytes + ValueKindSlice + ValueKindMap +) + +var valueKindStrings = []string{ + "Empty", + "Bool", + "Float64", + "Int64", + "String", + "Bytes", + "Slice", + "Map", +} + +func (k ValueKind) String() string { + if k >= 0 && int(k) < len(valueKindStrings) { + return valueKindStrings[k] + } + return "" +} + +// StringValue returns a new [Value] for a string. +func StringValue(v string) Value { + return Value{ + num: uint64(len(v)), + any: stringptr(unsafe.StringData(v)), + } +} + +// IntValue returns a [Value] for an int. +func IntValue(v int) Value { return Int64Value(int64(v)) } + +// Int64Value returns a [Value] for an int64. +func Int64Value(v int64) Value { + return Value{num: uint64(v), any: ValueKindInt64} +} + +// Float64Value returns a [Value] for a float64. +func Float64Value(v float64) Value { + return Value{num: math.Float64bits(v), any: ValueKindFloat64} +} + +// BoolValue returns a [Value] for a bool. +func BoolValue(v bool) Value { //nolint:revive // Not a control flag. + var n uint64 + if v { + n = 1 + } + return Value{num: n, any: ValueKindBool} +} + +// BytesValue returns a [Value] for a byte slice. The passed slice must not be +// changed after it is passed. +func BytesValue(v []byte) Value { + return Value{ + num: uint64(len(v)), + any: bytesptr(unsafe.SliceData(v)), + } +} + +// SliceValue returns a [Value] for a slice of [Value]. The passed slice must +// not be changed after it is passed. +func SliceValue(vs ...Value) Value { + return Value{ + num: uint64(len(vs)), + any: sliceptr(unsafe.SliceData(vs)), + } +} + +// MapValue returns a new [Value] for a slice of key-value pairs. The passed +// slice must not be changed after it is passed. +func MapValue(kvs ...Attr) Value { + return Value{ + num: uint64(len(kvs)), + any: mapptr(unsafe.SliceData(kvs)), + } +} + +// AsString returns the value held by v as a string. +func (v Value) AsString() string { + if sp, ok := v.any.(stringptr); ok { + return unsafe.String(sp, v.num) + } + // TODO: error handle + return "" +} + +// asString returns the value held by v as a string. It will panic if the Value +// is not KindString. +func (v Value) asString() string { + return unsafe.String(v.any.(stringptr), v.num) +} + +// AsInt64 returns the value held by v as an int64. +func (v Value) AsInt64() int64 { + if v.Kind() != ValueKindInt64 { + // TODO: error handle + return 0 + } + return v.asInt64() +} + +// asInt64 returns the value held by v as an int64. If v is not of KindInt64, +// this will return garbage. +func (v Value) asInt64() int64 { + // Assumes v.num was a valid int64 (overflow not checked). + return int64(v.num) // nolint: gosec +} + +// AsBool returns the value held by v as a bool. +func (v Value) AsBool() bool { + if v.Kind() != ValueKindBool { + // TODO: error handle + return false + } + return v.asBool() +} + +// asBool returns the value held by v as a bool. If v is not of KindBool, this +// will return garbage. +func (v Value) asBool() bool { return v.num == 1 } + +// AsFloat64 returns the value held by v as a float64. +func (v Value) AsFloat64() float64 { + if v.Kind() != ValueKindFloat64 { + // TODO: error handle + return 0 + } + return v.asFloat64() +} + +// asFloat64 returns the value held by v as a float64. If v is not of +// KindFloat64, this will return garbage. +func (v Value) asFloat64() float64 { return math.Float64frombits(v.num) } + +// AsBytes returns the value held by v as a []byte. +func (v Value) AsBytes() []byte { + if sp, ok := v.any.(bytesptr); ok { + return unsafe.Slice((*byte)(sp), v.num) + } + // TODO: error handle + return nil +} + +// asBytes returns the value held by v as a []byte. It will panic if the Value +// is not KindBytes. +func (v Value) asBytes() []byte { + return unsafe.Slice((*byte)(v.any.(bytesptr)), v.num) +} + +// AsSlice returns the value held by v as a []Value. +func (v Value) AsSlice() []Value { + if sp, ok := v.any.(sliceptr); ok { + return unsafe.Slice((*Value)(sp), v.num) + } + // TODO: error handle + return nil +} + +// asSlice returns the value held by v as a []Value. It will panic if the Value +// is not KindSlice. +func (v Value) asSlice() []Value { + return unsafe.Slice((*Value)(v.any.(sliceptr)), v.num) +} + +// AsMap returns the value held by v as a []Attr. +func (v Value) AsMap() []Attr { + if sp, ok := v.any.(mapptr); ok { + return unsafe.Slice((*Attr)(sp), v.num) + } + // TODO: error handle + return nil +} + +// asMap returns the value held by v as a []Attr. It will panic if the +// Value is not KindMap. +func (v Value) asMap() []Attr { + return unsafe.Slice((*Attr)(v.any.(mapptr)), v.num) +} + +// Kind returns the Kind of v. +func (v Value) Kind() ValueKind { + switch x := v.any.(type) { + case ValueKind: + return x + case stringptr: + return ValueKindString + case bytesptr: + return ValueKindBytes + case sliceptr: + return ValueKindSlice + case mapptr: + return ValueKindMap + default: + return ValueKindEmpty + } +} + +// Empty returns if v does not hold any value. +func (v Value) Empty() bool { return v.Kind() == ValueKindEmpty } + +// Equal returns if v is equal to w. +func (v Value) Equal(w Value) bool { + k1 := v.Kind() + k2 := w.Kind() + if k1 != k2 { + return false + } + switch k1 { + case ValueKindInt64, ValueKindBool: + return v.num == w.num + case ValueKindString: + return v.asString() == w.asString() + case ValueKindFloat64: + return v.asFloat64() == w.asFloat64() + case ValueKindSlice: + return slices.EqualFunc(v.asSlice(), w.asSlice(), Value.Equal) + case ValueKindMap: + sv := sortMap(v.asMap()) + sw := sortMap(w.asMap()) + return slices.EqualFunc(sv, sw, Attr.Equal) + case ValueKindBytes: + return bytes.Equal(v.asBytes(), w.asBytes()) + case ValueKindEmpty: + return true + default: + // TODO: error handle + return false + } +} + +func sortMap(m []Attr) []Attr { + sm := make([]Attr, len(m)) + copy(sm, m) + slices.SortFunc(sm, func(a, b Attr) int { + return cmp.Compare(a.Key, b.Key) + }) + + return sm +} + +// String returns Value's value as a string, formatted like [fmt.Sprint]. +// +// The returned string is meant for debugging; +// the string representation is not stable. +func (v Value) String() string { + switch v.Kind() { + case ValueKindString: + return v.asString() + case ValueKindInt64: + // Assumes v.num was a valid int64 (overflow not checked). + return strconv.FormatInt(int64(v.num), 10) // nolint: gosec + case ValueKindFloat64: + return strconv.FormatFloat(v.asFloat64(), 'g', -1, 64) + case ValueKindBool: + return strconv.FormatBool(v.asBool()) + case ValueKindBytes: + return fmt.Sprint(v.asBytes()) + case ValueKindMap: + return fmt.Sprint(v.asMap()) + case ValueKindSlice: + return fmt.Sprint(v.asSlice()) + case ValueKindEmpty: + return "" + default: + // Try to handle this as gracefully as possible. + // + // Don't panic here. The goal here is to have developers find this + // first if a slog.Kind is is not handled. It is + // preferable to have user's open issue asking why their attributes + // have a "unhandled: " prefix than say that their code is panicking. + return fmt.Sprintf("", v.Kind()) + } +} + +// MarshalJSON encodes v into OTLP formatted JSON. +func (v *Value) MarshalJSON() ([]byte, error) { + switch v.Kind() { + case ValueKindString: + return json.Marshal(struct { + Value string `json:"stringValue"` + }{v.asString()}) + case ValueKindInt64: + return json.Marshal(struct { + Value string `json:"intValue"` + }{strconv.FormatInt(int64(v.num), 10)}) + case ValueKindFloat64: + return json.Marshal(struct { + Value float64 `json:"doubleValue"` + }{v.asFloat64()}) + case ValueKindBool: + return json.Marshal(struct { + Value bool `json:"boolValue"` + }{v.asBool()}) + case ValueKindBytes: + return json.Marshal(struct { + Value []byte `json:"bytesValue"` + }{v.asBytes()}) + case ValueKindMap: + return json.Marshal(struct { + Value struct { + Values []Attr `json:"values"` + } `json:"kvlistValue"` + }{struct { + Values []Attr `json:"values"` + }{v.asMap()}}) + case ValueKindSlice: + return json.Marshal(struct { + Value struct { + Values []Value `json:"values"` + } `json:"arrayValue"` + }{struct { + Values []Value `json:"values"` + }{v.asSlice()}}) + case ValueKindEmpty: + return nil, nil + default: + return nil, fmt.Errorf("unknown Value kind: %s", v.Kind().String()) + } +} + +// UnmarshalJSON decodes the OTLP formatted JSON contained in data into v. +func (v *Value) UnmarshalJSON(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + + t, err := decoder.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return errors.New("invalid Value type") + } + + for decoder.More() { + keyIface, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + // Empty. + return nil + } + return err + } + + key, ok := keyIface.(string) + if !ok { + return fmt.Errorf("invalid Value key: %#v", keyIface) + } + + switch key { + case "stringValue", "string_value": + var val string + err = decoder.Decode(&val) + *v = StringValue(val) + case "boolValue", "bool_value": + var val bool + err = decoder.Decode(&val) + *v = BoolValue(val) + case "intValue", "int_value": + var val protoInt64 + err = decoder.Decode(&val) + *v = Int64Value(val.Int64()) + case "doubleValue", "double_value": + var val float64 + err = decoder.Decode(&val) + *v = Float64Value(val) + case "bytesValue", "bytes_value": + var val64 string + if err := decoder.Decode(&val64); err != nil { + return err + } + var val []byte + val, err = base64.StdEncoding.DecodeString(val64) + *v = BytesValue(val) + case "arrayValue", "array_value": + var val struct{ Values []Value } + err = decoder.Decode(&val) + *v = SliceValue(val.Values...) + case "kvlistValue", "kvlist_value": + var val struct{ Values []Attr } + err = decoder.Decode(&val) + *v = MapValue(val.Values...) + default: + // Skip unknown. + continue + } + // Use first valid. Ignore the rest. + return err + } + + // Only unknown fields. Return nil without unmarshaling any value. + return nil +} diff --git a/sdk/telemetry/doc.go b/sdk/telemetry/doc.go index 949e2165c..308c5a747 100644 --- a/sdk/telemetry/doc.go +++ b/sdk/telemetry/doc.go @@ -4,5 +4,7 @@ /* Package telemetry provides a lightweight representations of OpenTelemetry telemetry that is compatible with the OTLP JSON protobuf encoding. + +Deprecated: This module is no longer supported. */ package telemetry diff --git a/sdk/telemetry/test/conversion_test.go b/sdk/telemetry/test/conversion_test.go index ff5a2e4e0..67618b578 100644 --- a/sdk/telemetry/test/conversion_test.go +++ b/sdk/telemetry/test/conversion_test.go @@ -14,7 +14,7 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" - "go.opentelemetry.io/auto/sdk/telemetry" + "go.opentelemetry.io/auto/sdk/telemetry" //nolint:staticcheck // Removing in next release. ) var ( diff --git a/sdk/telemetry/test/doc.go b/sdk/telemetry/test/doc.go index 6143785f6..59feabb94 100644 --- a/sdk/telemetry/test/doc.go +++ b/sdk/telemetry/test/doc.go @@ -4,4 +4,6 @@ // Package test provides isolated testing for // go.opentelemetry.io/auto/sdk/telemetry. It exists to ensure that package is // tested without importing any OTLP or pdata dependencies. +// +// Deprecated: This module is no longer supported. package test diff --git a/sdk/telemetry/test/go.mod b/sdk/telemetry/test/go.mod index 334ddb884..3816e7c5f 100644 --- a/sdk/telemetry/test/go.mod +++ b/sdk/telemetry/test/go.mod @@ -1,3 +1,4 @@ +// Deprecated: This module is no longer supported. module go.opentelemetry.io/auto/sdk/telemetry/test go 1.22.0 diff --git a/sdk/trace.go b/sdk/trace.go index 1b9508b7d..491dff0a6 100644 --- a/sdk/trace.go +++ b/sdk/trace.go @@ -17,7 +17,7 @@ import ( "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace/noop" - "go.opentelemetry.io/auto/sdk/telemetry" + "go.opentelemetry.io/auto/sdk/internal/telemetry" ) // TracerProvider returns an auto-instrumentable [trace.TracerProvider]. diff --git a/sdk/trace_test.go b/sdk/trace_test.go index 87815d4f5..c97cd25b9 100644 --- a/sdk/trace_test.go +++ b/sdk/trace_test.go @@ -18,7 +18,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.26.0" "go.opentelemetry.io/otel/trace" - "go.opentelemetry.io/auto/sdk/telemetry" + "go.opentelemetry.io/auto/sdk/internal/telemetry" ) var ( diff --git a/versions.yaml b/versions.yaml index 0ea9380b2..ab7bd0092 100644 --- a/versions.yaml +++ b/versions.yaml @@ -25,4 +25,5 @@ excluded-modules: - go.opentelemetry.io/auto/internal/test/e2e/nethttp - go.opentelemetry.io/auto/internal/test/e2e/otelglobal - go.opentelemetry.io/auto/internal/tools + - go.opentelemetry.io/auto/sdk/internal/telemetry/test - go.opentelemetry.io/auto/sdk/telemetry/test