Skip to content

Commit

Permalink
Replace immutable TraceState with a mutable struct (#6052)
Browse files Browse the repository at this point in the history
* Replace immutable TraceState with a mutable struct

Fixes #6022

Main motivation is that we may want to allow mutating (adding an entry to the state).

Signed-off-by: Bogdan <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Dmitrii Anoshin <[email protected]>

Signed-off-by: Bogdan <[email protected]>
Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
bogdandrutu and dmitryax authored Sep 13, 2022
1 parent 38826ae commit a95999c
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 57 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
- `SummaryDataPoint.Flags` -> `SummaryDataPoint.FlagsImmutable`
- `MetricDataPointFlags` -> `MetricDataPointFlagsImmutable`
- `NewMetricDataPointFlags` -> `MetricDataPointFlagsImmutable`
- Deprecate `ptrace.TraceState` in favor of `pcommon.TraceState`. (#6052)
- `ptrace.Span.TraceState` in favor of `ptrace.Span.TraceStateStruct().AsRaw()`
- `ptrace.Span.SetTraceState` in favor of `ptrace.Span.TraceStateStruct().FromRaw`
- `ptrace.SpanLink.TraceState` in favor of `ptrace.SpanLink.TraceStateStruct().AsRaw()`
- `ptrace.SpanLink.SetTraceState` in favor of `ptrace.SpanLink.TraceStateStruct().FromRaw`
- `TraceStateStruct` is a temporary name that will be replaced back to `TraceState` in the next release.

### 💡 Enhancements 💡

Expand Down
2 changes: 1 addition & 1 deletion exporter/loggingexporter/internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) {
b.logEntry("SpanLink #%d", i)
b.logEntry(" -> Trace ID: %s", l.TraceID().HexString())
b.logEntry(" -> ID: %s", l.SpanID().HexString())
b.logEntry(" -> TraceState: %s", l.TraceState())
b.logEntry(" -> TraceState: %s", l.TraceStateStruct().AsRaw())
b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount())
if l.Attributes().Len() == 0 {
continue
Expand Down
5 changes: 5 additions & 0 deletions pdata/internal/cmd/pdatagen/internal/common_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ var scopeField = &messageValueField{
returnMessage: scope,
}

var traceState = &messageValueStruct{
structName: "TraceState",
packageName: "pcommon",
}

var timestampType = &primitiveType{
structName: "Timestamp",
packageName: "pcommon",
Expand Down
11 changes: 3 additions & 8 deletions pdata/internal/cmd/pdatagen/internal/trace_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,10 @@ var spanStatus = &messageValueStruct{
},
}

var traceStateField = &primitiveTypedField{
fieldName: "TraceState",
var traceStateField = &messageValueField{
fieldName: "TraceStateStruct",
originFieldName: "TraceState",
returnType: &primitiveType{
structName: "TraceState",
rawType: "string",
defaultVal: `""`,
testVal: `"congo=congos"`,
},
returnMessage: traceState,
}

var droppedAttributesCount = &primitiveField{
Expand Down
4 changes: 2 additions & 2 deletions pdata/internal/generated_wrapper_traces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 37 additions & 14 deletions pdata/internal/wrapper_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ func NewValue(orig *otlpcommon.AnyValue) Value {
return Value{orig: orig}
}

func FillTestValue(dest Value) {
dest.orig.Value = &otlpcommon.AnyValue_StringValue{StringValue: "v"}
}

func GenerateTestValue() Value {
var orig otlpcommon.AnyValue
ms := NewValue(&orig)
FillTestValue(ms)
return ms
}

type Map struct {
orig *[]otlpcommon.KeyValue
}
Expand All @@ -42,25 +53,37 @@ func NewMap(orig *[]otlpcommon.KeyValue) Map {
return Map{orig: orig}
}

func FillTestValue(dest Value) {
dest.orig.Value = &otlpcommon.AnyValue_StringValue{StringValue: "v"}
}

func GenerateTestValue() Value {
var orig otlpcommon.AnyValue
av := NewValue(&orig)
FillTestValue(av)
return av
}

func GenerateTestMap() Map {
var orig []otlpcommon.KeyValue
am := NewMap(&orig)
FillTestMap(am)
return am
ms := NewMap(&orig)
FillTestMap(ms)
return ms
}

func FillTestMap(dest Map) {
*dest.orig = nil
*dest.orig = append(*dest.orig, otlpcommon.KeyValue{Key: "k", Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "v"}}})
}

type TraceState struct {
orig *string
}

func GetOrigTraceState(ms TraceState) *string {
return ms.orig
}

func NewTraceState(orig *string) TraceState {
return TraceState{orig: orig}
}

func GenerateTestTraceState() TraceState {
var orig string
ms := NewTraceState(&orig)
FillTestTraceState(ms)
return ms
}

func FillTestTraceState(dest TraceState) {
*dest.orig = "rojo=00f067aa0ba902b7"
}
51 changes: 51 additions & 0 deletions pdata/pcommon/trace_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon"

import (
"go.opentelemetry.io/collector/pdata/internal"
)

// TraceState represents the trace state from the w3c-trace-context.
type TraceState internal.TraceState

func NewTraceState() TraceState {
return TraceState(internal.NewTraceState(new(string)))
}

func (ms TraceState) getOrig() *string {
return internal.GetOrigTraceState(internal.TraceState(ms))
}

// AsRaw returns the string representation of the tracestate in w3c-trace-context format: https://www.w3.org/TR/trace-context/#tracestate-header
func (ms TraceState) AsRaw() string {
return *ms.getOrig()
}

// FromRaw copies the string representation in w3c-trace-context format of the tracestate into this TraceState.
func (ms TraceState) FromRaw(v string) {
*ms.getOrig() = v
}

// MoveTo moves TraceState to another instance.
func (ms TraceState) MoveTo(dest TraceState) {
*dest.getOrig() = *ms.getOrig()
*ms.getOrig() = ""
}

// CopyTo copies TraceState to another instance.
func (ms TraceState) CopyTo(dest TraceState) {
*dest.getOrig() = *ms.getOrig()
}
48 changes: 48 additions & 0 deletions pdata/pcommon/trace_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pcommon

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/pdata/internal"
)

func TestTraceState_MoveTo(t *testing.T) {
ms := TraceState(internal.GenerateTestTraceState())
dest := NewTraceState()
ms.MoveTo(dest)
assert.Equal(t, NewTraceState(), ms)
assert.Equal(t, TraceState(internal.GenerateTestTraceState()), dest)
}

func TestTraceState_CopyTo(t *testing.T) {
ms := NewTraceState()
orig := NewTraceState()
orig.CopyTo(ms)
assert.Equal(t, orig, ms)
orig = TraceState(internal.GenerateTestTraceState())
orig.CopyTo(ms)
assert.Equal(t, orig, ms)
}

func TestTraceState_FromRaw_AsRaw(t *testing.T) {
ms := NewTraceState()
assert.Equal(t, "", ms.AsRaw())
ms.FromRaw("congo=t61rcWkgMzE")
assert.Equal(t, "congo=t61rcWkgMzE", ms.AsRaw())
}
26 changes: 8 additions & 18 deletions pdata/ptrace/generated_traces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions pdata/ptrace/generated_traces_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pdata/ptrace/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var tracesOTLPFull = func() Traces {
sp.SetDroppedLinksCount(1)
sp.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
sp.SetParentSpanID(spanID)
sp.SetTraceState("state")
sp.TraceStateStruct().FromRaw("state")
sp.Status().SetCode(StatusCodeOk)
sp.Status().SetMessage("message")
// Add attributes.
Expand All @@ -111,7 +111,7 @@ var tracesOTLPFull = func() Traces {
event.Attributes().UpsertEmptyBytes("bytes").FromRaw([]byte("foo"))
// Add links.
link := sp.Links().AppendEmpty()
link.SetTraceState("state")
link.TraceStateStruct().FromRaw("state")
link.SetTraceID(traceID)
link.SetSpanID(spanID)
link.SetDroppedAttributesCount(1)
Expand Down
24 changes: 22 additions & 2 deletions pdata/ptrace/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,34 @@ func (ms Traces) ResourceSpans() ResourceSpansSlice {
return newResourceSpansSlice(&ms.getOrig().ResourceSpans)
}

// TraceState is a string representing the tracestate in w3c-trace-context format: https://www.w3.org/TR/trace-context/#tracestate-header
// Deprecated: [v0.60.0] use pcommon.TraceState.
type TraceState string

const (
// TraceStateEmpty represents the empty TraceState.
// Deprecated: [v0.60.0] use pcommon.TraceState.AsRaw() and compare with empty string.
TraceStateEmpty TraceState = ""
)

// Deprecated: [v0.60.0] use TraceStateStruct().AsRaw().
func (ms Span) TraceState() TraceState {
return TraceState(ms.getOrig().TraceState)
}

// Deprecated: [v0.60.0] use TraceStateStruct().FromRaw(val).
func (ms Span) SetTraceState(v TraceState) {
ms.getOrig().TraceState = string(v)
}

// Deprecated: [v0.60.0] use TraceStateStruct().AsRaw().
func (ms SpanLink) TraceState() TraceState {
return TraceState(ms.getOrig().TraceState)
}

// Deprecated: [v0.60.0] use TraceStateStruct().FromRaw(val).
func (ms SpanLink) SetTraceState(v TraceState) {
ms.getOrig().TraceState = string(v)
}

// 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
Expand Down

0 comments on commit a95999c

Please sign in to comment.