Skip to content

Commit

Permalink
Add copywrite header and fix for Go 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Nov 24, 2023
1 parent 441cde8 commit de09ce6
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions log/record.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package log // import "go.opentelemetry.io/otel/log"

import (
"errors"
"slices"
"time"

"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -117,12 +120,12 @@ func (r *Record) AddAttributes(attrs ...attribute.KeyValue) {
end := r.back[:len(r.back)+1][len(r.back)]
if end.Valid() {
// Don't panic; copy and muddle through.
r.back = slices.Clip(r.back)
r.back = sliceClip(r.back)
otel.Handle(errUnsafeAddAttrs)
}

Check warning on line 125 in log/record.go

View check run for this annotation

Codecov / codecov/patch

log/record.go#L120-L125

Added lines #L120 - L125 were not covered by tests
}
ne := countInvalidAttrs(attrs[i:])
r.back = slices.Grow(r.back, len(attrs[i:])-ne)
r.back = sliceGrow(r.back, len(attrs[i:])-ne)
for _, a := range attrs[i:] {
if a.Valid() {
r.back = append(r.back, a)
Expand All @@ -134,7 +137,7 @@ func (r *Record) AddAttributes(attrs ...attribute.KeyValue) {
// The original record and the clone can both be modified
// without interfering with each other.
func (r Record) Clone() Record {
r.back = slices.Clip(r.back) // prevent append from mutating shared array
r.back = sliceClip(r.back) // prevent append from mutating shared array
return r

Check warning on line 141 in log/record.go

View check run for this annotation

Codecov / codecov/patch

log/record.go#L139-L141

Added lines #L139 - L141 were not covered by tests
}

Expand All @@ -153,3 +156,26 @@ func countInvalidAttrs(as []attribute.KeyValue) int {
}
return n
}

// sliceGrow increases the slice's capacity, if necessary, to guarantee space for
// another n elements. After Grow(n), at least n elements can be appended
// to the slice without another allocation. If n is negative or too large to
// allocate the memory, Grow panics.
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceGrow[S ~[]E, E any](s S, n int) S {
if n < 0 {
panic("cannot be negative")

Check warning on line 168 in log/record.go

View check run for this annotation

Codecov / codecov/patch

log/record.go#L168

Added line #L168 was not covered by tests
}
if n -= cap(s) - len(s); n > 0 {
s = append(s[:cap(s)], make([]E, n)...)[:len(s)]
}

Check warning on line 172 in log/record.go

View check run for this annotation

Codecov / codecov/patch

log/record.go#L171-L172

Added lines #L171 - L172 were not covered by tests
return s
}

// sliceClip removes unused capacity from the slice, returning s[:len(s):len(s)].
//
// This is a copy from https://pkg.go.dev/slices as it is not available in Go 1.20.
func sliceClip[S ~[]E, E any](s S) S {
return s[:len(s):len(s)]

Check warning on line 180 in log/record.go

View check run for this annotation

Codecov / codecov/patch

log/record.go#L179-L180

Added lines #L179 - L180 were not covered by tests
}

0 comments on commit de09ce6

Please sign in to comment.