Skip to content

Commit f08ebd6

Browse files
authored
Merge branch 'main' into sdk-enabled-param
2 parents 7e9e7ce + 80e18a5 commit f08ebd6

File tree

29 files changed

+230
-92
lines changed

29 files changed

+230
-92
lines changed

attribute/value_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func TestValue(t *testing.T) {
3838
wantType: attribute.INT64,
3939
wantValue: int64(42),
4040
},
41+
{
42+
name: "Key.Int64() correctly returns negative keys's internal int64 value",
43+
value: k.Int64(-42).Value,
44+
wantType: attribute.INT64,
45+
wantValue: int64(-42),
46+
},
4147
{
4248
name: "Key.Int64Slice() correctly returns keys's internal []int64 value",
4349
value: k.Int64Slice([]int64{42, -3, 12}).Value,

bridge/opencensus/internal/ocmetric/metric.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func convertHistogram(labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.Time
144144
Attributes: attrs,
145145
StartTime: t.StartTime,
146146
Time: p.Time,
147-
Count: uint64(dist.Count),
147+
Count: uint64(max(0, dist.Count)), // nolint:gosec // A count should never be negative.
148148
Sum: dist.Sum,
149149
Bounds: dist.BucketOptions.Bounds,
150150
BucketCounts: bucketCounts,
@@ -166,7 +166,7 @@ func convertBuckets(buckets []ocmetricdata.Bucket) ([]uint64, []metricdata.Exemp
166166
err = errors.Join(err, fmt.Errorf("%w: %q", errNegativeBucketCount, bucket.Count))
167167
continue
168168
}
169-
bucketCounts[i] = uint64(bucket.Count)
169+
bucketCounts[i] = uint64(max(0, bucket.Count)) // nolint:gosec // A count should never be negative.
170170

171171
if bucket.Exemplar != nil {
172172
exemplar, exemplarErr := convertExemplar(bucket.Exemplar)
@@ -357,7 +357,7 @@ func convertSummary(labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.TimeSe
357357
Attributes: attrs,
358358
StartTime: t.StartTime,
359359
Time: p.Time,
360-
Count: uint64(summary.Count),
360+
Count: uint64(max(0, summary.Count)), // nolint:gosec // A count should never be negative.
361361
QuantileValues: convertQuantiles(summary.Snapshot),
362362
Sum: summary.Sum,
363363
}

bridge/opencensus/internal/span.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *Span) SetName(name string) {
6161

6262
// SetStatus sets the status of this span, if it is recording events.
6363
func (s *Span) SetStatus(status octrace.Status) {
64-
s.otelSpan.SetStatus(codes.Code(status.Code), status.Message)
64+
s.otelSpan.SetStatus(codes.Code(max(0, status.Code)), status.Message) // nolint:gosec // Overflow checked.
6565
}
6666

6767
// AddAttributes sets attributes in this span.

bridge/opencensus/internal/span_test.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,40 @@ func TestSpanSetStatus(t *testing.T) {
9696
s := &span{recording: true}
9797
ocS := internal.NewSpan(s)
9898

99-
c, d := codes.Error, "error"
100-
status := octrace.Status{Code: int32(c), Message: d}
101-
ocS.SetStatus(status)
99+
for _, tt := range []struct {
100+
name string
102101

103-
if s.sCode != c {
104-
t.Error("span.SetStatus failed to set OpenTelemetry status code")
105-
}
106-
if s.sMsg != d {
107-
t.Error("span.SetStatus failed to set OpenTelemetry status description")
102+
code int32
103+
message string
104+
105+
wantCode codes.Code
106+
}{
107+
{
108+
name: "with an error code",
109+
code: int32(codes.Error),
110+
message: "error",
111+
112+
wantCode: codes.Error,
113+
},
114+
{
115+
name: "with a negative/invalid code",
116+
code: -42,
117+
message: "error",
118+
119+
wantCode: codes.Unset,
120+
},
121+
} {
122+
t.Run(tt.name, func(t *testing.T) {
123+
status := octrace.Status{Code: tt.code, Message: tt.message}
124+
ocS.SetStatus(status)
125+
126+
if s.sCode != tt.wantCode {
127+
t.Errorf("span.SetStatus failed to set OpenTelemetry status code. Expected %d, got %d", tt.wantCode, s.sCode)
128+
}
129+
if s.sMsg != tt.message {
130+
t.Errorf("span.SetStatus failed to set OpenTelemetry status description. Expected %s, got %s", tt.message, s.sMsg)
131+
}
132+
})
108133
}
109134
}
110135

exporters/otlp/otlplog/otlploggrpc/internal/transform/log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ func LogRecord(record log.Record) *lpb.LogRecord {
139139
// year 1678 or after 2262). timeUnixNano on the zero Time returns 0. The
140140
// result does not depend on the location associated with t.
141141
func timeUnixNano(t time.Time) uint64 {
142-
if t.IsZero() {
142+
nano := t.UnixNano()
143+
if nano < 0 {
143144
return 0
144145
}
145-
return uint64(t.UnixNano())
146+
return uint64(nano) // nolint:gosec // Overflow checked.
146147
}
147148

148149
// AttrIter transforms an [attribute.Iterator] into OTLP key-values.

exporters/otlp/otlplog/otlploggrpc/internal/transform/log_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var (
3030
ts = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0))
3131
obs = ts.Add(30 * time.Second)
3232

33+
// A time before unix 0.
34+
negativeTs = time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC)
35+
3336
alice = api.String("user", "alice")
3437
bob = api.String("user", "bob")
3538

@@ -158,6 +161,20 @@ var (
158161
Resource: res,
159162
}.NewRecord())
160163

164+
out = append(out, logtest.RecordFactory{
165+
Timestamp: negativeTs,
166+
ObservedTimestamp: obs,
167+
Severity: sevB,
168+
SeverityText: "B",
169+
Body: bodyB,
170+
Attributes: []api.KeyValue{bob},
171+
TraceID: trace.TraceID(traceIDB),
172+
SpanID: trace.SpanID(spanIDB),
173+
TraceFlags: trace.TraceFlags(flagsB),
174+
InstrumentationScope: &scope,
175+
Resource: res,
176+
}.NewRecord())
177+
161178
return out
162179
}()
163180

@@ -206,6 +223,17 @@ var (
206223
TraceId: traceIDB,
207224
SpanId: spanIDB,
208225
},
226+
{
227+
TimeUnixNano: 0,
228+
ObservedTimeUnixNano: uint64(obs.UnixNano()),
229+
SeverityNumber: pbSevB,
230+
SeverityText: "B",
231+
Body: pbBodyB,
232+
Attributes: []*cpb.KeyValue{pbBob},
233+
Flags: uint32(flagsB),
234+
TraceId: traceIDB,
235+
SpanId: spanIDB,
236+
},
209237
}
210238

211239
pbScopeLogs = &lpb.ScopeLogs{

exporters/otlp/otlplog/otlploghttp/internal/transform/log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ func LogRecord(record log.Record) *lpb.LogRecord {
139139
// year 1678 or after 2262). timeUnixNano on the zero Time returns 0. The
140140
// result does not depend on the location associated with t.
141141
func timeUnixNano(t time.Time) uint64 {
142-
if t.IsZero() {
142+
nano := t.UnixNano()
143+
if nano < 0 {
143144
return 0
144145
}
145-
return uint64(t.UnixNano())
146+
return uint64(nano) // nolint:gosec // Overflow checked.
146147
}
147148

148149
// AttrIter transforms an [attribute.Iterator] into OTLP key-values.

exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var (
3030
ts = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0))
3131
obs = ts.Add(30 * time.Second)
3232

33+
// A time before unix 0.
34+
negativeTs = time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC)
35+
3336
alice = api.String("user", "alice")
3437
bob = api.String("user", "bob")
3538

@@ -158,6 +161,20 @@ var (
158161
Resource: res,
159162
}.NewRecord())
160163

164+
out = append(out, logtest.RecordFactory{
165+
Timestamp: negativeTs,
166+
ObservedTimestamp: obs,
167+
Severity: sevB,
168+
SeverityText: "B",
169+
Body: bodyB,
170+
Attributes: []api.KeyValue{bob},
171+
TraceID: trace.TraceID(traceIDB),
172+
SpanID: trace.SpanID(spanIDB),
173+
TraceFlags: trace.TraceFlags(flagsB),
174+
InstrumentationScope: &scope,
175+
Resource: res,
176+
}.NewRecord())
177+
161178
return out
162179
}()
163180

@@ -206,6 +223,17 @@ var (
206223
TraceId: traceIDB,
207224
SpanId: spanIDB,
208225
},
226+
{
227+
TimeUnixNano: 0,
228+
ObservedTimeUnixNano: uint64(obs.UnixNano()),
229+
SeverityNumber: pbSevB,
230+
SeverityText: "B",
231+
Body: pbBodyB,
232+
Attributes: []*cpb.KeyValue{pbBob},
233+
Flags: uint32(flagsB),
234+
TraceId: traceIDB,
235+
SpanId: spanIDB,
236+
},
209237
}
210238

211239
pbScopeLogs = &lpb.ScopeLogs{

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/transform/metricdata.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,7 @@ func Temporality(t metricdata.Temporality) (mpb.AggregationTemporality, error) {
279279
// timeUnixNano on the zero Time returns 0.
280280
// The result does not depend on the location associated with t.
281281
func timeUnixNano(t time.Time) uint64 {
282-
if t.IsZero() {
283-
return 0
284-
}
285-
return uint64(t.UnixNano())
282+
return uint64(max(0, t.UnixNano())) // nolint:gosec // Overflow checked.
286283
}
287284

288285
// Exemplars returns a slice of OTLP Exemplars generated from exemplars.

exporters/otlp/otlpmetric/otlpmetrichttp/internal/transform/metricdata.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,7 @@ func Temporality(t metricdata.Temporality) (mpb.AggregationTemporality, error) {
279279
// timeUnixNano on the zero Time returns 0.
280280
// The result does not depend on the location associated with t.
281281
func timeUnixNano(t time.Time) uint64 {
282-
if t.IsZero() {
283-
return 0
284-
}
285-
return uint64(t.UnixNano())
282+
return uint64(max(0, t.UnixNano())) // nolint:gosec // Overflow checked.
286283
}
287284

288285
// Exemplars returns a slice of OTLP Exemplars generated from exemplars.

exporters/otlp/otlptrace/internal/tracetransform/span.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func span(sd tracesdk.ReadOnlySpan) *tracepb.Span {
9797
SpanId: sid[:],
9898
TraceState: sd.SpanContext().TraceState().String(),
9999
Status: status(sd.Status().Code, sd.Status().Description),
100-
StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
101-
EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
100+
StartTimeUnixNano: uint64(max(0, sd.StartTime().UnixNano())), // nolint:gosec // Overflow checked.
101+
EndTimeUnixNano: uint64(max(0, sd.EndTime().UnixNano())), // nolint:gosec // Overflow checked.
102102
Links: links(sd.Links()),
103103
Kind: spanKind(sd.SpanKind()),
104104
Name: sd.Name(),
@@ -178,7 +178,7 @@ func buildSpanFlags(sc trace.SpanContext) uint32 {
178178
flags |= tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
179179
}
180180

181-
return uint32(flags)
181+
return uint32(flags) // nolint:gosec // Flags is a bitmask and can't be negative
182182
}
183183

184184
// spanEvents transforms span Events to an OTLP span events.
@@ -192,7 +192,7 @@ func spanEvents(es []tracesdk.Event) []*tracepb.Span_Event {
192192
for i := 0; i < len(es); i++ {
193193
events[i] = &tracepb.Span_Event{
194194
Name: es[i].Name,
195-
TimeUnixNano: uint64(es[i].Time.UnixNano()),
195+
TimeUnixNano: uint64(max(0, es[i].Time.UnixNano())), // nolint:gosec // Overflow checked.
196196
Attributes: KeyValues(es[i].Attributes),
197197
DroppedAttributesCount: clampUint32(es[i].DroppedAttributeCount),
198198
}

exporters/otlp/otlptrace/internal/tracetransform/span_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestEmptySpanEvent(t *testing.T) {
6868
func TestSpanEvent(t *testing.T) {
6969
attrs := []attribute.KeyValue{attribute.Int("one", 1), attribute.Int("two", 2)}
7070
eventTime := time.Date(2020, 5, 20, 0, 0, 0, 0, time.UTC)
71+
negativeEventTime := time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC)
7172
got := spanEvents([]tracesdk.Event{
7273
{
7374
Name: "test 1",
@@ -80,14 +81,21 @@ func TestSpanEvent(t *testing.T) {
8081
Time: eventTime,
8182
DroppedAttributeCount: 2,
8283
},
84+
{
85+
Name: "test 3",
86+
Attributes: attrs,
87+
Time: negativeEventTime,
88+
DroppedAttributeCount: 2,
89+
},
8390
})
84-
if !assert.Len(t, got, 2) {
91+
if !assert.Len(t, got, 3) {
8592
return
8693
}
8794
eventTimestamp := uint64(1589932800 * 1e9)
8895
assert.Equal(t, &tracepb.Span_Event{Name: "test 1", Attributes: nil, TimeUnixNano: eventTimestamp}, got[0])
8996
// Do not test Attributes directly, just that the return value goes to the correct field.
9097
assert.Equal(t, &tracepb.Span_Event{Name: "test 2", Attributes: KeyValues(attrs), TimeUnixNano: eventTimestamp, DroppedAttributesCount: 2}, got[1])
98+
assert.Equal(t, &tracepb.Span_Event{Name: "test 3", Attributes: KeyValues(attrs), TimeUnixNano: 0, DroppedAttributesCount: 2}, got[2])
9199
}
92100

93101
func TestNilLinks(t *testing.T) {

exporters/zipkin/internal/internaltest/text_map_propagator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.Text
7575
}
7676

7777
// InjectedN tests if p has made n injections to carrier.
78-
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n int) bool {
79-
if actual := p.stateFromCarrier(carrier).Injections; actual != uint64(n) {
78+
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n uint64) bool {
79+
if actual := p.stateFromCarrier(carrier).Injections; actual != n {
8080
t.Errorf("TextMapPropagator{%q} injected %d times, not %d", p.name, actual, n)
8181
return false
8282
}

internal/internaltest/text_map_propagator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.Text
7575
}
7676

7777
// InjectedN tests if p has made n injections to carrier.
78-
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n int) bool {
79-
if actual := p.stateFromCarrier(carrier).Injections; actual != uint64(n) {
78+
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n uint64) bool {
79+
if actual := p.stateFromCarrier(carrier).Injections; actual != n {
8080
t.Errorf("TextMapPropagator{%q} injected %d times, not %d", p.name, actual, n)
8181
return false
8282
}

internal/rawhelpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func RawToBool(r uint64) bool {
2020
}
2121

2222
func Int64ToRaw(i int64) uint64 {
23-
return uint64(i)
23+
// Assumes original was a valid int64 (overflow not checked).
24+
return uint64(i) // nolint: gosec
2425
}
2526

2627
func RawToInt64(r uint64) int64 {

internal/shared/internaltest/text_map_propagator.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (p *TextMapPropagator) Inject(ctx context.Context, carrier propagation.Text
7575
}
7676

7777
// InjectedN tests if p has made n injections to carrier.
78-
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n int) bool {
79-
if actual := p.stateFromCarrier(carrier).Injections; actual != uint64(n) {
78+
func (p *TextMapPropagator) InjectedN(t *testing.T, carrier *TextMapCarrier, n uint64) bool {
79+
if actual := p.stateFromCarrier(carrier).Injections; actual != n {
8080
t.Errorf("TextMapPropagator{%q} injected %d times, not %d", p.name, actual, n)
8181
return false
8282
}

internal/shared/otlp/otlplog/transform/log.go.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ func LogRecord(record log.Record) *lpb.LogRecord {
139139
// year 1678 or after 2262). timeUnixNano on the zero Time returns 0. The
140140
// result does not depend on the location associated with t.
141141
func timeUnixNano(t time.Time) uint64 {
142-
if t.IsZero() {
142+
nano := t.UnixNano()
143+
if nano < 0 {
143144
return 0
144145
}
145-
return uint64(t.UnixNano())
146+
return uint64(nano) // nolint:gosec // Overflow checked.
146147
}
147148

148149
// AttrIter transforms an [attribute.Iterator] into OTLP key-values.

0 commit comments

Comments
 (0)