mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-04-15 11:36:44 +02:00
Rename resource pkg label vars and methods (#1692)
* Rename resource pkg label vars and methods The former `labels` package is now named `attributes` to conform with the specification requirement. This removes the lingering `label` term from the `resource` package. Resolve https://github.com/open-telemetry/opentelemetry-go/issues/1691 * Update PR number in CHANGELOG * Propagate rename to the prometheus exporter pkg
This commit is contained in:
parent
a1539d4485
commit
8b1be11a54
@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
- Add `description` to SpanStatus only when `StatusCode` is set to error. (#1662)
|
||||
- `trace.SpanContext` is now immutable and has no exported fields. (#1573)
|
||||
- `trace.NewSpanContext()` can be used in conjunction with the `trace.SpanContextConfig` struct to initialize a new `SpanContext` where all values are known.
|
||||
- Renamed the `LabelSet` method of `"go.opentelemetry.io/otel/sdk/resource".Resource` to `Set`. (#1692)
|
||||
|
||||
### Removed
|
||||
|
||||
|
@ -349,7 +349,7 @@ func mergeLabels(record export.Record, keys, values *[]string) {
|
||||
|
||||
// Duplicate keys are resolved by taking the record label value over
|
||||
// the resource value.
|
||||
mi := attribute.NewMergeIterator(record.Labels(), record.Resource().LabelSet())
|
||||
mi := attribute.NewMergeIterator(record.Labels(), record.Resource().Set())
|
||||
for mi.Next() {
|
||||
label := mi.Label()
|
||||
if keys != nil {
|
||||
|
@ -43,17 +43,17 @@ var _ Detector = FromEnv{}
|
||||
|
||||
// Detect collects resources from environment
|
||||
func (FromEnv) Detect(context.Context) (*Resource, error) {
|
||||
labels := strings.TrimSpace(os.Getenv(envVar))
|
||||
attrs := strings.TrimSpace(os.Getenv(envVar))
|
||||
|
||||
if labels == "" {
|
||||
if attrs == "" {
|
||||
return Empty(), nil
|
||||
}
|
||||
return constructOTResources(labels)
|
||||
return constructOTResources(attrs)
|
||||
}
|
||||
|
||||
func constructOTResources(s string) (*Resource, error) {
|
||||
pairs := strings.Split(s, ",")
|
||||
labels := []attribute.KeyValue{}
|
||||
attrs := []attribute.KeyValue{}
|
||||
var invalid []string
|
||||
for _, p := range pairs {
|
||||
field := strings.SplitN(p, "=", 2)
|
||||
@ -62,11 +62,11 @@ func constructOTResources(s string) (*Resource, error) {
|
||||
continue
|
||||
}
|
||||
k, v := strings.TrimSpace(field[0]), strings.TrimSpace(field[1])
|
||||
labels = append(labels, attribute.String(k, v))
|
||||
attrs = append(attrs, attribute.String(k, v))
|
||||
}
|
||||
var err error
|
||||
if len(invalid) > 0 {
|
||||
err = fmt.Errorf("%w: %v", errMissingValue, invalid)
|
||||
}
|
||||
return NewWithAttributes(labels...), err
|
||||
return NewWithAttributes(attrs...), err
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ import (
|
||||
// (`*resource.Resource`). The `nil` value is equivalent to an empty
|
||||
// Resource.
|
||||
type Resource struct {
|
||||
labels attribute.Set
|
||||
attrs attribute.Set
|
||||
}
|
||||
|
||||
var (
|
||||
@ -48,7 +48,7 @@ var (
|
||||
// value found for the key is preserved.
|
||||
func NewWithAttributes(kvs ...attribute.KeyValue) *Resource {
|
||||
return &Resource{
|
||||
labels: attribute.NewSet(kvs...),
|
||||
attrs: attribute.NewSet(kvs...),
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ func (r *Resource) String() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
return r.labels.Encoded(attribute.DefaultEncoder())
|
||||
return r.attrs.Encoded(attribute.DefaultEncoder())
|
||||
}
|
||||
|
||||
// Attributes returns a copy of attributes from the resource in a sorted order.
|
||||
@ -70,7 +70,7 @@ func (r *Resource) Attributes() []attribute.KeyValue {
|
||||
if r == nil {
|
||||
r = Empty()
|
||||
}
|
||||
return r.labels.ToSlice()
|
||||
return r.attrs.ToSlice()
|
||||
}
|
||||
|
||||
// Iter returns an interator of the Resource attributes.
|
||||
@ -79,7 +79,7 @@ func (r *Resource) Iter() attribute.Iterator {
|
||||
if r == nil {
|
||||
r = Empty()
|
||||
}
|
||||
return r.labels.Iter()
|
||||
return r.attrs.Iter()
|
||||
}
|
||||
|
||||
// Equal returns true when a Resource is equivalent to this Resource.
|
||||
@ -109,9 +109,9 @@ func Merge(a, b *Resource) *Resource {
|
||||
return a
|
||||
}
|
||||
|
||||
// Note: 'b' labels will overwrite 'a' with last-value-wins in attribute.Key()
|
||||
// Note: 'b' attributes will overwrite 'a' with last-value-wins in attribute.Key()
|
||||
// Meaning this is equivalent to: append(a.Attributes(), b.Attributes()...)
|
||||
mi := attribute.NewMergeIterator(b.LabelSet(), a.LabelSet())
|
||||
mi := attribute.NewMergeIterator(b.Set(), a.Set())
|
||||
combine := make([]attribute.KeyValue, 0, a.Len()+b.Len())
|
||||
for mi.Next() {
|
||||
combine = append(combine, mi.Label())
|
||||
@ -135,24 +135,24 @@ func Default() *Resource {
|
||||
// between two resources. This value is suitable for use as a key in
|
||||
// a map.
|
||||
func (r *Resource) Equivalent() attribute.Distinct {
|
||||
return r.LabelSet().Equivalent()
|
||||
return r.Set().Equivalent()
|
||||
}
|
||||
|
||||
// LabelSet returns the equivalent *attribute.Set.
|
||||
func (r *Resource) LabelSet() *attribute.Set {
|
||||
// Set returns the equivalent *attribute.Set of this resources attributes.
|
||||
func (r *Resource) Set() *attribute.Set {
|
||||
if r == nil {
|
||||
r = Empty()
|
||||
}
|
||||
return &r.labels
|
||||
return &r.attrs
|
||||
}
|
||||
|
||||
// MarshalJSON encodes labels as a JSON list of { "Key": "...", "Value": ... }
|
||||
// pairs in order sorted by key.
|
||||
// MarshalJSON encodes the resource attributes as a JSON list of { "Key":
|
||||
// "...", "Value": ... } pairs in order sorted by key.
|
||||
func (r *Resource) MarshalJSON() ([]byte, error) {
|
||||
if r == nil {
|
||||
r = Empty()
|
||||
}
|
||||
return r.labels.MarshalJSON()
|
||||
return r.attrs.MarshalJSON()
|
||||
}
|
||||
|
||||
// Len returns the number of unique key-values in this Resource.
|
||||
@ -160,15 +160,13 @@ func (r *Resource) Len() int {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
return r.labels.Len()
|
||||
return r.attrs.Len()
|
||||
}
|
||||
|
||||
// Encoded returns an encoded representation of the resource by
|
||||
// applying a label encoder. The result is cached by the underlying
|
||||
// label set.
|
||||
// Encoded returns an encoded representation of the resource.
|
||||
func (r *Resource) Encoded(enc attribute.Encoder) string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
return r.labels.Encoded(enc)
|
||||
return r.attrs.Encoded(enc)
|
||||
}
|
||||
|
@ -168,9 +168,9 @@ func TestMerge(t *testing.T) {
|
||||
func TestDefault(t *testing.T) {
|
||||
res := resource.Default()
|
||||
require.False(t, res.Equal(resource.Empty()))
|
||||
require.True(t, res.LabelSet().HasValue(semconv.ServiceNameKey))
|
||||
require.True(t, res.Set().HasValue(semconv.ServiceNameKey))
|
||||
|
||||
serviceName, _ := res.LabelSet().Value(semconv.ServiceNameKey)
|
||||
serviceName, _ := res.Set().Value(semconv.ServiceNameKey)
|
||||
require.True(t, strings.HasPrefix(serviceName.AsString(), "unknown_service:"))
|
||||
require.Greaterf(t, len(serviceName.AsString()), len("unknown_service:"),
|
||||
"default service.name should include executable name")
|
||||
|
Loading…
x
Reference in New Issue
Block a user