1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-20 03:30:02 +02:00

Update trace API config creation functions (#2212)

* Update trace API config creation funcs

Follow our style guide and return the config struct instead of pointers.

* Update changelog with changes

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
This commit is contained in:
Tyler Yahn 2021-09-01 12:44:13 -07:00 committed by GitHub
parent 361a209680
commit 1f527a52ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 43 deletions

View File

@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Metric SDK/API implementation type `InstrumentKind` moves into `sdkapi` sub-package. (#2091) - Metric SDK/API implementation type `InstrumentKind` moves into `sdkapi` sub-package. (#2091)
- The Metrics SDK export record no longer contains a Resource pointer, the SDK `"go.opentelemetry.io/otel/sdk/trace/export/metric".Exporter.Export()` function for push-based exporters now takes a single Resource argument, pull-based exporters use `"go.opentelemetry.io/otel/sdk/metric/controller/basic".Controller.Resource()`. (#2120) - The Metrics SDK export record no longer contains a Resource pointer, the SDK `"go.opentelemetry.io/otel/sdk/trace/export/metric".Exporter.Export()` function for push-based exporters now takes a single Resource argument, pull-based exporters use `"go.opentelemetry.io/otel/sdk/metric/controller/basic".Controller.Resource()`. (#2120)
- The JSON output of the `go.opentelemetry.io/otel/exporters/stdout/stdouttrace` is harmonized now such that the output is "plain" JSON objects after each other of the form `{ ... } { ... } { ... }`. Earlier the JSON objects describing a span were wrapped in a slice for each `Exporter.ExportSpans` call, like `[ { ... } ][ { ... } { ... } ]`. Outputting JSON object directly after each other is consistent with JSON loggers, and a bit easier to parse and read. (#2196) - The JSON output of the `go.opentelemetry.io/otel/exporters/stdout/stdouttrace` is harmonized now such that the output is "plain" JSON objects after each other of the form `{ ... } { ... } { ... }`. Earlier the JSON objects describing a span were wrapped in a slice for each `Exporter.ExportSpans` call, like `[ { ... } ][ { ... } { ... } ]`. Outputting JSON object directly after each other is consistent with JSON loggers, and a bit easier to parse and read. (#2196)
- Update the `NewTracerConfig`, `NewSpanStartConfig`, `NewSpanEndConfig`, and `NewEventConfig` function in the `go.opentelemetry.io/otel/trace` package to return their respective configurations as structs instead of pointers to the struct. (#2212)
### Deprecated ### Deprecated

View File

@ -151,7 +151,8 @@ func TestSpanAnnotate(t *testing.T) {
t.Error("span.Annotate did not set event name") t.Error("span.Annotate did not set event name")
} }
got := trace.NewEventConfig(s.eOpts...).Attributes() config := trace.NewEventConfig(s.eOpts...)
got := config.Attributes()
if len(want) != len(got) || want[0] != got[0] { if len(want) != len(got) || want[0] != got[0] {
t.Error("span.Annotate did not set event options") t.Error("span.Annotate did not set event options")
} }
@ -174,7 +175,8 @@ func TestSpanAnnotatef(t *testing.T) {
t.Error("span.Annotatef did not set event name") t.Error("span.Annotatef did not set event name")
} }
got := trace.NewEventConfig(s.eOpts...).Attributes() config := trace.NewEventConfig(s.eOpts...)
got := config.Attributes()
if len(want) != len(got) || want[0] != got[0] { if len(want) != len(got) || want[0] != got[0] {
t.Error("span.Annotatef did not set event options") t.Error("span.Annotatef did not set event options")
} }
@ -192,7 +194,8 @@ func TestSpanAddMessageSendEvent(t *testing.T) {
t.Error("span.AddMessageSendEvent did not set event name") t.Error("span.AddMessageSendEvent did not set event name")
} }
got := trace.NewEventConfig(s.eOpts...).Attributes() config := trace.NewEventConfig(s.eOpts...)
got := config.Attributes()
if len(got) != 2 { if len(got) != 2 {
t.Fatalf("span.AddMessageSendEvent set %d attributes, want 2", len(got)) t.Fatalf("span.AddMessageSendEvent set %d attributes, want 2", len(got))
} }
@ -220,7 +223,8 @@ func TestSpanAddMessageReceiveEvent(t *testing.T) {
t.Error("span.AddMessageReceiveEvent did not set event name") t.Error("span.AddMessageReceiveEvent did not set event name")
} }
got := trace.NewEventConfig(s.eOpts...).Attributes() config := trace.NewEventConfig(s.eOpts...)
got := config.Attributes()
if len(got) != 2 { if len(got) != 2 {
t.Fatalf("span.AddMessageReceiveEvent set %d attributes, want 2", len(got)) t.Fatalf("span.AddMessageReceiveEvent set %d attributes, want 2", len(got))
} }

View File

@ -75,7 +75,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...trace.SpanS
startTime = time.Now() startTime = time.Now()
} }
spanContext := trace.NewSpanContext(trace.SpanContextConfig{ spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: t.getTraceID(ctx, config), TraceID: t.getTraceID(ctx, &config),
SpanID: t.getSpanID(), SpanID: t.getSpanID(),
TraceFlags: 0, TraceFlags: 0,
}) })
@ -86,7 +86,7 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...trace.SpanS
Attributes: config.Attributes(), Attributes: config.Attributes(),
StartTime: startTime, StartTime: startTime,
EndTime: time.Time{}, EndTime: time.Time{},
ParentSpanID: t.getParentSpanID(ctx, config), ParentSpanID: t.getParentSpanID(ctx, &config),
Events: nil, Events: nil,
SpanKind: trace.ValidateSpanKind(config.SpanKind()), SpanKind: trace.ValidateSpanKind(config.SpanKind()),
} }

View File

@ -90,9 +90,10 @@ func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
// At this moment it is guaranteed that no sdk is installed, save the tracer in the tracers map. // At this moment it is guaranteed that no sdk is installed, save the tracer in the tracers map.
c := trace.NewTracerConfig(opts...)
key := il{ key := il{
name: name, name: name,
version: trace.NewTracerConfig(opts...).InstrumentationVersion(), version: c.InstrumentationVersion(),
} }
if p.tracers == nil { if p.tracers == nil {

View File

@ -46,7 +46,7 @@ func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanS
} }
} }
span := startSpanInternal(ctx, tr, name, config) span := startSpanInternal(ctx, tr, name, &config)
for _, l := range config.Links() { for _, l := range config.Links() {
span.addLink(l) span.addLink(l)
} }

View File

@ -38,10 +38,10 @@ func (t *TracerConfig) SchemaURL() string {
} }
// NewTracerConfig applies all the options to a returned TracerConfig. // NewTracerConfig applies all the options to a returned TracerConfig.
func NewTracerConfig(options ...TracerOption) *TracerConfig { func NewTracerConfig(options ...TracerOption) TracerConfig {
config := new(TracerConfig) var config TracerConfig
for _, option := range options { for _, option := range options {
option.apply(config) option.apply(&config)
} }
return config return config
} }
@ -103,10 +103,10 @@ func (cfg *SpanConfig) SpanKind() SpanKind {
// No validation is performed on the returned SpanConfig (e.g. no uniqueness // No validation is performed on the returned SpanConfig (e.g. no uniqueness
// checking or bounding of data), it is left to the SDK to perform this // checking or bounding of data), it is left to the SDK to perform this
// action. // action.
func NewSpanStartConfig(options ...SpanStartOption) *SpanConfig { func NewSpanStartConfig(options ...SpanStartOption) SpanConfig {
c := new(SpanConfig) var c SpanConfig
for _, option := range options { for _, option := range options {
option.applySpanStart(c) option.applySpanStart(&c)
} }
return c return c
} }
@ -115,10 +115,10 @@ func NewSpanStartConfig(options ...SpanStartOption) *SpanConfig {
// No validation is performed on the returned SpanConfig (e.g. no uniqueness // No validation is performed on the returned SpanConfig (e.g. no uniqueness
// checking or bounding of data), it is left to the SDK to perform this // checking or bounding of data), it is left to the SDK to perform this
// action. // action.
func NewSpanEndConfig(options ...SpanEndOption) *SpanConfig { func NewSpanEndConfig(options ...SpanEndOption) SpanConfig {
c := new(SpanConfig) var c SpanConfig
for _, option := range options { for _, option := range options {
option.applySpanEnd(c) option.applySpanEnd(&c)
} }
return c return c
} }
@ -167,10 +167,10 @@ func (cfg *EventConfig) StackTrace() bool {
// timestamp option is passed, the returned EventConfig will have a Timestamp // timestamp option is passed, the returned EventConfig will have a Timestamp
// set to the call time, otherwise no validation is performed on the returned // set to the call time, otherwise no validation is performed on the returned
// EventConfig. // EventConfig.
func NewEventConfig(options ...EventOption) *EventConfig { func NewEventConfig(options ...EventOption) EventConfig {
c := new(EventConfig) var c EventConfig
for _, option := range options { for _, option := range options {
option.applyEvent(c) option.applyEvent(&c)
} }
if c.timestamp.IsZero() { if c.timestamp.IsZero() {
c.timestamp = time.Now() c.timestamp = time.Now()

View File

@ -42,18 +42,18 @@ func TestNewSpanConfig(t *testing.T) {
tests := []struct { tests := []struct {
options []SpanStartOption options []SpanStartOption
expected *SpanConfig expected SpanConfig
}{ }{
{ {
// No non-zero-values should be set. // No non-zero-values should be set.
[]SpanStartOption{}, []SpanStartOption{},
new(SpanConfig), SpanConfig{},
}, },
{ {
[]SpanStartOption{ []SpanStartOption{
WithAttributes(k1v1), WithAttributes(k1v1),
}, },
&SpanConfig{ SpanConfig{
attributes: []attribute.KeyValue{k1v1}, attributes: []attribute.KeyValue{k1v1},
}, },
}, },
@ -64,7 +64,7 @@ func TestNewSpanConfig(t *testing.T) {
WithAttributes(k1v2), WithAttributes(k1v2),
WithAttributes(k2v2), WithAttributes(k2v2),
}, },
&SpanConfig{ SpanConfig{
// No uniqueness is guaranteed by the API. // No uniqueness is guaranteed by the API.
attributes: []attribute.KeyValue{k1v1, k1v2, k2v2}, attributes: []attribute.KeyValue{k1v1, k1v2, k2v2},
}, },
@ -73,7 +73,7 @@ func TestNewSpanConfig(t *testing.T) {
[]SpanStartOption{ []SpanStartOption{
WithAttributes(k1v1, k1v2, k2v2), WithAttributes(k1v1, k1v2, k2v2),
}, },
&SpanConfig{ SpanConfig{
// No uniqueness is guaranteed by the API. // No uniqueness is guaranteed by the API.
attributes: []attribute.KeyValue{k1v1, k1v2, k2v2}, attributes: []attribute.KeyValue{k1v1, k1v2, k2v2},
}, },
@ -82,7 +82,7 @@ func TestNewSpanConfig(t *testing.T) {
[]SpanStartOption{ []SpanStartOption{
WithTimestamp(timestamp0), WithTimestamp(timestamp0),
}, },
&SpanConfig{ SpanConfig{
timestamp: timestamp0, timestamp: timestamp0,
}, },
}, },
@ -92,7 +92,7 @@ func TestNewSpanConfig(t *testing.T) {
WithTimestamp(timestamp0), WithTimestamp(timestamp0),
WithTimestamp(timestamp1), WithTimestamp(timestamp1),
}, },
&SpanConfig{ SpanConfig{
timestamp: timestamp1, timestamp: timestamp1,
}, },
}, },
@ -100,7 +100,7 @@ func TestNewSpanConfig(t *testing.T) {
[]SpanStartOption{ []SpanStartOption{
WithLinks(link1), WithLinks(link1),
}, },
&SpanConfig{ SpanConfig{
links: []Link{link1}, links: []Link{link1},
}, },
}, },
@ -110,7 +110,7 @@ func TestNewSpanConfig(t *testing.T) {
WithLinks(link1), WithLinks(link1),
WithLinks(link1, link2), WithLinks(link1, link2),
}, },
&SpanConfig{ SpanConfig{
// No uniqueness is guaranteed by the API. // No uniqueness is guaranteed by the API.
links: []Link{link1, link1, link2}, links: []Link{link1, link1, link2},
}, },
@ -119,7 +119,7 @@ func TestNewSpanConfig(t *testing.T) {
[]SpanStartOption{ []SpanStartOption{
WithNewRoot(), WithNewRoot(),
}, },
&SpanConfig{ SpanConfig{
newRoot: true, newRoot: true,
}, },
}, },
@ -129,7 +129,7 @@ func TestNewSpanConfig(t *testing.T) {
WithNewRoot(), WithNewRoot(),
WithNewRoot(), WithNewRoot(),
}, },
&SpanConfig{ SpanConfig{
newRoot: true, newRoot: true,
}, },
}, },
@ -137,7 +137,7 @@ func TestNewSpanConfig(t *testing.T) {
[]SpanStartOption{ []SpanStartOption{
WithSpanKind(SpanKindConsumer), WithSpanKind(SpanKindConsumer),
}, },
&SpanConfig{ SpanConfig{
spanKind: SpanKindConsumer, spanKind: SpanKindConsumer,
}, },
}, },
@ -147,7 +147,7 @@ func TestNewSpanConfig(t *testing.T) {
WithSpanKind(SpanKindClient), WithSpanKind(SpanKindClient),
WithSpanKind(SpanKindConsumer), WithSpanKind(SpanKindConsumer),
}, },
&SpanConfig{ SpanConfig{
spanKind: SpanKindConsumer, spanKind: SpanKindConsumer,
}, },
}, },
@ -160,7 +160,7 @@ func TestNewSpanConfig(t *testing.T) {
WithNewRoot(), WithNewRoot(),
WithSpanKind(SpanKindConsumer), WithSpanKind(SpanKindConsumer),
}, },
&SpanConfig{ SpanConfig{
attributes: []attribute.KeyValue{k1v1}, attributes: []attribute.KeyValue{k1v1},
timestamp: timestamp0, timestamp: timestamp0,
links: []Link{link1, link2}, links: []Link{link1, link2},
@ -179,17 +179,17 @@ func TestEndSpanConfig(t *testing.T) {
tests := []struct { tests := []struct {
options []SpanEndOption options []SpanEndOption
expected *SpanConfig expected SpanConfig
}{ }{
{ {
[]SpanEndOption{}, []SpanEndOption{},
new(SpanConfig), SpanConfig{},
}, },
{ {
[]SpanEndOption{ []SpanEndOption{
WithStackTrace(true), WithStackTrace(true),
}, },
&SpanConfig{ SpanConfig{
stackTrace: true, stackTrace: true,
}, },
}, },
@ -197,7 +197,7 @@ func TestEndSpanConfig(t *testing.T) {
[]SpanEndOption{ []SpanEndOption{
WithTimestamp(timestamp), WithTimestamp(timestamp),
}, },
&SpanConfig{ SpanConfig{
timestamp: timestamp, timestamp: timestamp,
}, },
}, },
@ -213,18 +213,18 @@ func TestTracerConfig(t *testing.T) {
schemaURL := "https://opentelemetry.io/schemas/1.2.0" schemaURL := "https://opentelemetry.io/schemas/1.2.0"
tests := []struct { tests := []struct {
options []TracerOption options []TracerOption
expected *TracerConfig expected TracerConfig
}{ }{
{ {
// No non-zero-values should be set. // No non-zero-values should be set.
[]TracerOption{}, []TracerOption{},
new(TracerConfig), TracerConfig{},
}, },
{ {
[]TracerOption{ []TracerOption{
WithInstrumentationVersion(v1), WithInstrumentationVersion(v1),
}, },
&TracerConfig{ TracerConfig{
instrumentationVersion: v1, instrumentationVersion: v1,
}, },
}, },
@ -234,7 +234,7 @@ func TestTracerConfig(t *testing.T) {
WithInstrumentationVersion(v1), WithInstrumentationVersion(v1),
WithInstrumentationVersion(v2), WithInstrumentationVersion(v2),
}, },
&TracerConfig{ TracerConfig{
instrumentationVersion: v2, instrumentationVersion: v2,
}, },
}, },
@ -242,7 +242,7 @@ func TestTracerConfig(t *testing.T) {
[]TracerOption{ []TracerOption{
WithSchemaURL(schemaURL), WithSchemaURL(schemaURL),
}, },
&TracerConfig{ TracerConfig{
schemaURL: schemaURL, schemaURL: schemaURL,
}, },
}, },