mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-07 23:02:15 +02:00
8e97011ea8
* Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package resource provides functionality for resource, which capture
|
|
// identifying information about the entities for which signals are exported.
|
|
package resource
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
)
|
|
|
|
// Resource describes an entity about which identifying information and metadata is exposed.
|
|
type Resource struct {
|
|
sorted []core.KeyValue
|
|
keySet map[core.Key]struct{}
|
|
}
|
|
|
|
// New creates a resource from a set of attributes.
|
|
// If there are duplicates keys then the first value of the key is preserved.
|
|
func New(kvs ...core.KeyValue) *Resource {
|
|
res := &Resource{keySet: make(map[core.Key]struct{})}
|
|
for _, kv := range kvs {
|
|
// First key wins.
|
|
if _, ok := res.keySet[kv.Key]; !ok {
|
|
res.keySet[kv.Key] = struct{}{}
|
|
res.sorted = append(res.sorted, kv)
|
|
}
|
|
}
|
|
sort.Slice(res.sorted, func(i, j int) bool {
|
|
return res.sorted[i].Key < res.sorted[j].Key
|
|
})
|
|
return res
|
|
}
|
|
|
|
// String implements the Stringer interface and provides a reproducibly
|
|
// hashable representation of a Resource.
|
|
func (r Resource) String() string {
|
|
// Ensure unique strings if key/value contains '=', ',', or '\'.
|
|
escaper := strings.NewReplacer("=", `\=`, ",", `\,`, `\`, `\\`)
|
|
|
|
var b strings.Builder
|
|
// Note: this could be further optimized by precomputing the size of
|
|
// the resulting buffer and adding a call to b.Grow
|
|
b.WriteString("Resource(")
|
|
if len(r.sorted) > 0 {
|
|
b.WriteString(escaper.Replace(string(r.sorted[0].Key)))
|
|
b.WriteRune('=')
|
|
b.WriteString(escaper.Replace(r.sorted[0].Value.Emit()))
|
|
for _, s := range r.sorted[1:] {
|
|
b.WriteRune(',')
|
|
b.WriteString(escaper.Replace(string(s.Key)))
|
|
b.WriteRune('=')
|
|
b.WriteString(escaper.Replace(s.Value.Emit()))
|
|
}
|
|
|
|
}
|
|
b.WriteRune(')')
|
|
|
|
return b.String()
|
|
}
|
|
|
|
// Attributes returns a copy of attributes from the resource in a sorted order.
|
|
func (r Resource) Attributes() []core.KeyValue {
|
|
return append([]core.KeyValue(nil), r.sorted...)
|
|
}
|
|
|
|
// Iter returns an interator of the Resource attributes.
|
|
//
|
|
// This is ideal to use if you do not want a copy of the attributes.
|
|
func (r Resource) Iter() AttributeIterator {
|
|
return NewAttributeIterator(r.sorted)
|
|
}
|
|
|
|
// Equal returns true if other Resource is equal to r.
|
|
func (r Resource) Equal(other Resource) bool {
|
|
return r.String() == other.String()
|
|
}
|
|
|
|
// Merge creates a new resource by combining resource a and b.
|
|
// If there are common key between resource a and b then value from resource a is preserved.
|
|
// If one of the resources is nil then the other resource is returned without creating a new one.
|
|
func Merge(a, b *Resource) *Resource {
|
|
if a == nil {
|
|
return b
|
|
}
|
|
if b == nil {
|
|
return a
|
|
}
|
|
|
|
// Note: the following could be optimized by implementing a dedicated merge sort.
|
|
|
|
kvs := make([]core.KeyValue, 0, len(a.sorted)+len(b.sorted))
|
|
kvs = append(kvs, a.sorted...)
|
|
// a overwrites b, so b needs to be at the end.
|
|
kvs = append(kvs, b.sorted...)
|
|
return New(kvs...)
|
|
}
|