Skip to content

Commit 8b1be11

Browse files
authored
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 #1691 * Update PR number in CHANGELOG * Propagate rename to the prometheus exporter pkg
1 parent a1539d4 commit 8b1be11

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2424
- Add `description` to SpanStatus only when `StatusCode` is set to error. (#1662)
2525
- `trace.SpanContext` is now immutable and has no exported fields. (#1573)
2626
- `trace.NewSpanContext()` can be used in conjunction with the `trace.SpanContextConfig` struct to initialize a new `SpanContext` where all values are known.
27+
- Renamed the `LabelSet` method of `"go.opentelemetry.io/otel/sdk/resource".Resource` to `Set`. (#1692)
2728

2829
### Removed
2930

exporters/metric/prometheus/prometheus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func mergeLabels(record export.Record, keys, values *[]string) {
349349

350350
// Duplicate keys are resolved by taking the record label value over
351351
// the resource value.
352-
mi := attribute.NewMergeIterator(record.Labels(), record.Resource().LabelSet())
352+
mi := attribute.NewMergeIterator(record.Labels(), record.Resource().Set())
353353
for mi.Next() {
354354
label := mi.Label()
355355
if keys != nil {

sdk/resource/env.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ var _ Detector = FromEnv{}
4343

4444
// Detect collects resources from environment
4545
func (FromEnv) Detect(context.Context) (*Resource, error) {
46-
labels := strings.TrimSpace(os.Getenv(envVar))
46+
attrs := strings.TrimSpace(os.Getenv(envVar))
4747

48-
if labels == "" {
48+
if attrs == "" {
4949
return Empty(), nil
5050
}
51-
return constructOTResources(labels)
51+
return constructOTResources(attrs)
5252
}
5353

5454
func constructOTResources(s string) (*Resource, error) {
5555
pairs := strings.Split(s, ",")
56-
labels := []attribute.KeyValue{}
56+
attrs := []attribute.KeyValue{}
5757
var invalid []string
5858
for _, p := range pairs {
5959
field := strings.SplitN(p, "=", 2)
@@ -62,11 +62,11 @@ func constructOTResources(s string) (*Resource, error) {
6262
continue
6363
}
6464
k, v := strings.TrimSpace(field[0]), strings.TrimSpace(field[1])
65-
labels = append(labels, attribute.String(k, v))
65+
attrs = append(attrs, attribute.String(k, v))
6666
}
6767
var err error
6868
if len(invalid) > 0 {
6969
err = fmt.Errorf("%w: %v", errMissingValue, invalid)
7070
}
71-
return NewWithAttributes(labels...), err
71+
return NewWithAttributes(attrs...), err
7272
}

sdk/resource/resource.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// (`*resource.Resource`). The `nil` value is equivalent to an empty
3030
// Resource.
3131
type Resource struct {
32-
labels attribute.Set
32+
attrs attribute.Set
3333
}
3434

3535
var (
@@ -48,7 +48,7 @@ var (
4848
// value found for the key is preserved.
4949
func NewWithAttributes(kvs ...attribute.KeyValue) *Resource {
5050
return &Resource{
51-
labels: attribute.NewSet(kvs...),
51+
attrs: attribute.NewSet(kvs...),
5252
}
5353
}
5454

@@ -61,7 +61,7 @@ func (r *Resource) String() string {
6161
if r == nil {
6262
return ""
6363
}
64-
return r.labels.Encoded(attribute.DefaultEncoder())
64+
return r.attrs.Encoded(attribute.DefaultEncoder())
6565
}
6666

6767
// Attributes returns a copy of attributes from the resource in a sorted order.
@@ -70,7 +70,7 @@ func (r *Resource) Attributes() []attribute.KeyValue {
7070
if r == nil {
7171
r = Empty()
7272
}
73-
return r.labels.ToSlice()
73+
return r.attrs.ToSlice()
7474
}
7575

7676
// Iter returns an interator of the Resource attributes.
@@ -79,7 +79,7 @@ func (r *Resource) Iter() attribute.Iterator {
7979
if r == nil {
8080
r = Empty()
8181
}
82-
return r.labels.Iter()
82+
return r.attrs.Iter()
8383
}
8484

8585
// Equal returns true when a Resource is equivalent to this Resource.
@@ -109,9 +109,9 @@ func Merge(a, b *Resource) *Resource {
109109
return a
110110
}
111111

112-
// Note: 'b' labels will overwrite 'a' with last-value-wins in attribute.Key()
112+
// Note: 'b' attributes will overwrite 'a' with last-value-wins in attribute.Key()
113113
// Meaning this is equivalent to: append(a.Attributes(), b.Attributes()...)
114-
mi := attribute.NewMergeIterator(b.LabelSet(), a.LabelSet())
114+
mi := attribute.NewMergeIterator(b.Set(), a.Set())
115115
combine := make([]attribute.KeyValue, 0, a.Len()+b.Len())
116116
for mi.Next() {
117117
combine = append(combine, mi.Label())
@@ -135,40 +135,38 @@ func Default() *Resource {
135135
// between two resources. This value is suitable for use as a key in
136136
// a map.
137137
func (r *Resource) Equivalent() attribute.Distinct {
138-
return r.LabelSet().Equivalent()
138+
return r.Set().Equivalent()
139139
}
140140

141-
// LabelSet returns the equivalent *attribute.Set.
142-
func (r *Resource) LabelSet() *attribute.Set {
141+
// Set returns the equivalent *attribute.Set of this resources attributes.
142+
func (r *Resource) Set() *attribute.Set {
143143
if r == nil {
144144
r = Empty()
145145
}
146-
return &r.labels
146+
return &r.attrs
147147
}
148148

149-
// MarshalJSON encodes labels as a JSON list of { "Key": "...", "Value": ... }
150-
// pairs in order sorted by key.
149+
// MarshalJSON encodes the resource attributes as a JSON list of { "Key":
150+
// "...", "Value": ... } pairs in order sorted by key.
151151
func (r *Resource) MarshalJSON() ([]byte, error) {
152152
if r == nil {
153153
r = Empty()
154154
}
155-
return r.labels.MarshalJSON()
155+
return r.attrs.MarshalJSON()
156156
}
157157

158158
// Len returns the number of unique key-values in this Resource.
159159
func (r *Resource) Len() int {
160160
if r == nil {
161161
return 0
162162
}
163-
return r.labels.Len()
163+
return r.attrs.Len()
164164
}
165165

166-
// Encoded returns an encoded representation of the resource by
167-
// applying a label encoder. The result is cached by the underlying
168-
// label set.
166+
// Encoded returns an encoded representation of the resource.
169167
func (r *Resource) Encoded(enc attribute.Encoder) string {
170168
if r == nil {
171169
return ""
172170
}
173-
return r.labels.Encoded(enc)
171+
return r.attrs.Encoded(enc)
174172
}

sdk/resource/resource_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ func TestMerge(t *testing.T) {
168168
func TestDefault(t *testing.T) {
169169
res := resource.Default()
170170
require.False(t, res.Equal(resource.Empty()))
171-
require.True(t, res.LabelSet().HasValue(semconv.ServiceNameKey))
171+
require.True(t, res.Set().HasValue(semconv.ServiceNameKey))
172172

173-
serviceName, _ := res.LabelSet().Value(semconv.ServiceNameKey)
173+
serviceName, _ := res.Set().Value(semconv.ServiceNameKey)
174174
require.True(t, strings.HasPrefix(serviceName.AsString(), "unknown_service:"))
175175
require.Greaterf(t, len(serviceName.AsString()), len("unknown_service:"),
176176
"default service.name should include executable name")

0 commit comments

Comments
 (0)