Skip to content

Commit 238e7c6

Browse files
mrveeraMrAlias
andauthored
Add non-empty string check for attribute keys (#1659)
* Fixes #1657 Add non-empty string check for attribute keys * Update PR number in changelog * Update CHANGELOG.md Co-authored-by: Tyler Yahn <[email protected]> * Update sdk/trace/span.go Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent e9b9aca commit 238e7c6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1111
## Added
1212

1313
- Added `Marshler` config option to `otlphttp` to enable otlp over json or protobufs. (#1586)
14+
- Added non-empty string check for trace `Attribute` keys. (#1659)
15+
1416
### Removed
1517

1618
- Removed the exported `SimpleSpanProcessor` and `BatchSpanProcessor` structs.

sdk/trace/span.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,9 @@ func (s *span) copyToCappedAttributes(attributes ...attribute.KeyValue) {
491491
s.mu.Lock()
492492
defer s.mu.Unlock()
493493
for _, a := range attributes {
494-
if a.Value.Type() != attribute.INVALID {
494+
// Ensure attributes conform to the specification:
495+
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.0.1/specification/common/common.md#attributes
496+
if a.Value.Type() != attribute.INVALID && a.Key != "" {
495497
s.attributes.add(a)
496498
}
497499
}

sdk/trace/trace_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,41 @@ func TestSetSpanAttributesOverLimit(t *testing.T) {
520520
}
521521
}
522522

523+
func TestSetSpanAttributesWithInvalidKey(t *testing.T) {
524+
te := NewTestExporter()
525+
cfg := Config{SpanLimits: SpanLimits{}}
526+
tp := NewTracerProvider(WithConfig(cfg), WithSyncer(te), WithResource(resource.Empty()))
527+
528+
span := startSpan(tp, "SpanToSetInvalidKeyOrValue")
529+
span.SetAttributes(
530+
attribute.Bool("", true),
531+
attribute.Bool("key1", false),
532+
)
533+
got, err := endSpan(te, span)
534+
if err != nil {
535+
t.Fatal(err)
536+
}
537+
538+
want := &export.SpanSnapshot{
539+
SpanContext: trace.SpanContext{
540+
TraceID: tid,
541+
TraceFlags: 0x1,
542+
},
543+
ParentSpanID: sid,
544+
Name: "span0",
545+
Attributes: []attribute.KeyValue{
546+
attribute.Bool("key1", false),
547+
},
548+
SpanKind: trace.SpanKindInternal,
549+
HasRemoteParent: true,
550+
DroppedAttributeCount: 0,
551+
InstrumentationLibrary: instrumentation.Library{Name: "SpanToSetInvalidKeyOrValue"},
552+
}
553+
if diff := cmpDiff(got, want); diff != "" {
554+
t.Errorf("SetSpanAttributesWithInvalidKey: -got +want %s", diff)
555+
}
556+
}
557+
523558
func TestEvents(t *testing.T) {
524559
te := NewTestExporter()
525560
tp := NewTracerProvider(WithSyncer(te), WithResource(resource.Empty()))

0 commit comments

Comments
 (0)