| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" |
| 5 |
|
| 6 |
import ( |
| 7 |
"go.opentelemetry.io/collector/pdata/internal" |
| 8 |
) |
| 9 |
|
| 10 |
// TraceState represents the trace state from the w3c-trace-context. |
| 11 |
// |
| 12 |
// Must use NewTraceState function to create new instances. |
| 13 |
// Important: zero-initialized instance is not valid for use. |
| 14 |
type TraceState internal.TraceStateWrapper |
| 15 |
|
| 16 |
func NewTraceState() TraceState { |
| 17 |
return TraceState(internal.NewTraceStateWrapper(new(string), internal.NewState())) |
| 18 |
} |
| 19 |
|
| 20 |
func (ms TraceState) getOrig() *string { |
| 21 |
return internal.GetTraceStateOrig(internal.TraceStateWrapper(ms)) |
| 22 |
} |
| 23 |
|
| 24 |
func (ms TraceState) getState() *internal.State { |
| 25 |
return internal.GetTraceStateState(internal.TraceStateWrapper(ms)) |
| 26 |
} |
| 27 |
|
| 28 |
// AsRaw returns the string representation of the tracestate in w3c-trace-context format: https://www.w3.org/TR/trace-context/#tracestate-header |
| 29 |
func (ms TraceState) AsRaw() string { |
| 30 |
return *ms.getOrig() |
| 31 |
} |
| 32 |
|
| 33 |
// FromRaw copies the string representation in w3c-trace-context format of the tracestate into this TraceState. |
| 34 |
func (ms TraceState) FromRaw(v string) { |
| 35 |
ms.getState().AssertMutable() |
| 36 |
*ms.getOrig() = v |
| 37 |
} |
| 38 |
|
| 39 |
// MoveTo moves the TraceState instance overriding the destination |
| 40 |
// and resetting the current instance to its zero value. |
| 41 |
func (ms TraceState) MoveTo(dest TraceState) { |
| 42 |
ms.getState().AssertMutable() |
| 43 |
dest.getState().AssertMutable() |
| 44 |
// If they point to the same data, they are the same, nothing to do. |
| 45 |
if ms.getOrig() == dest.getOrig() { |
| 46 |
return |
| 47 |
} |
| 48 |
*dest.getOrig() = *ms.getOrig() |
| 49 |
*ms.getOrig() = "" |
| 50 |
} |
| 51 |
|
| 52 |
// CopyTo copies the TraceState instance overriding the destination. |
| 53 |
func (ms TraceState) CopyTo(dest TraceState) { |
| 54 |
dest.getState().AssertMutable() |
| 55 |
*dest.getOrig() = *ms.getOrig() |
| 56 |
} |