Skip to content

Commit 30c0f3f

Browse files
authored
sdk/instrumentation: Add Attributes to Scope (#5903)
Towards #3368
1 parent 078b2dd commit 30c0f3f

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717
- Add `ReservoirProvider`, `HistogramReservoirProvider` and `FixedSizeReservoirProvider` to `go.opentelemetry.io/otel/sdk/metric/exemplar` to make it convenient to use providers of Reservoirs. (#5861)
1818
- The `go.opentelemetry.io/otel/semconv/v1.27.0` package.
1919
The package contains semantic conventions from the `v1.27.0` version of the OpenTelemetry Semantic Conventions. (#5894)
20+
- Add `Attributes attribute.Set` field to `Scope` in `go.opentelemetry.io/otel/sdk/instrumentation`. (#5903)
2021

2122
### Fixed
2223

exporters/stdout/stdoutlog/exporter_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func getJSON(now *time.Time) string {
183183
timestamps = "\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ","
184184
}
185185

186-
return "{" + timestamps + "\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{\"Type\":\"String\",\"Value\":\"test\"},\"Attributes\":[{\"Key\":\"key\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key2\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key3\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key4\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key5\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"bool\",\"Value\":{\"Type\":\"Bool\",\"Value\":true}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":[{\"Key\":\"foo\",\"Value\":{\"Type\":\"STRING\",\"Value\":\"bar\"}}],\"Scope\":{\"Name\":\"name\",\"Version\":\"version\",\"SchemaURL\":\"https://example.com/custom-schema\"},\"DroppedAttributes\":10}\n"
186+
return "{" + timestamps + "\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{\"Type\":\"String\",\"Value\":\"test\"},\"Attributes\":[{\"Key\":\"key\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key2\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key3\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key4\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"key5\",\"Value\":{\"Type\":\"String\",\"Value\":\"value\"}},{\"Key\":\"bool\",\"Value\":{\"Type\":\"Bool\",\"Value\":true}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":[{\"Key\":\"foo\",\"Value\":{\"Type\":\"STRING\",\"Value\":\"bar\"}}],\"Scope\":{\"Name\":\"name\",\"Version\":\"version\",\"SchemaURL\":\"https://example.com/custom-schema\",\"Attributes\":{}},\"DroppedAttributes\":10}\n"
187187
}
188188

189189
func getJSONs(now *time.Time) string {
@@ -263,7 +263,8 @@ func getPrettyJSON(now *time.Time) string {
263263
"Scope": {
264264
"Name": "name",
265265
"Version": "version",
266-
"SchemaURL": "https://example.com/custom-schema"
266+
"SchemaURL": "https://example.com/custom-schema",
267+
"Attributes": {}
267268
},
268269
"DroppedAttributes": 10
269270
}

exporters/stdout/stdoutmetric/example_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ func Example() {
184184
// "Scope": {
185185
// "Name": "example",
186186
// "Version": "0.0.1",
187-
// "SchemaURL": ""
187+
// "SchemaURL": "",
188+
// "Attributes": null
188189
// },
189190
// "Metrics": [
190191
// {

exporters/stdout/stdouttrace/trace_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ func expectedJSON(now time.Time) string {
189189
"InstrumentationScope": {
190190
"Name": "",
191191
"Version": "",
192-
"SchemaURL": ""
192+
"SchemaURL": "",
193+
"Attributes": null
193194
},
194195
"InstrumentationLibrary": {
195196
"Name": "",
196197
"Version": "",
197-
"SchemaURL": ""
198+
"SchemaURL": "",
199+
"Attributes": null
198200
}
199201
}
200202
`

sdk/instrumentation/scope.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package instrumentation // import "go.opentelemetry.io/otel/sdk/instrumentation"
55

6+
import "go.opentelemetry.io/otel/attribute"
7+
68
// Scope represents the instrumentation scope.
79
type Scope struct {
810
// Name is the name of the instrumentation scope. This should be the
@@ -12,4 +14,6 @@ type Scope struct {
1214
Version string
1315
// SchemaURL of the telemetry emitted by the scope.
1416
SchemaURL string
17+
// Attributes of the telemetry emitted by the scope.
18+
Attributes attribute.Set
1519
}

sdk/log/provider_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,15 @@ func TestLoggerProviderLogger(t *testing.T) {
300300
t.Run("SameLoggers", func(t *testing.T) {
301301
p := NewLoggerProvider()
302302

303-
l0, l1 := p.Logger("l0"), p.Logger("l1")
304-
l2, l3 := p.Logger("l0"), p.Logger("l1")
305-
306-
assert.Same(t, l0, l2)
307-
assert.Same(t, l1, l3)
303+
l0, l1, l2 := p.Logger("l0"), p.Logger("l1"), p.Logger("l0", log.WithInstrumentationAttributes(attribute.String("foo", "bar")))
304+
assert.NotSame(t, l0, l1)
305+
assert.Same(t, l0, l2) // TODO (#3368): Change to assert.NotSame.
306+
assert.NotSame(t, l1, l2)
307+
308+
l3, l4, l5 := p.Logger("l0"), p.Logger("l1"), p.Logger("l0", log.WithInstrumentationAttributes(attribute.String("foo", "bar")))
309+
assert.Same(t, l0, l3)
310+
assert.Same(t, l1, l4)
311+
assert.Same(t, l2, l5)
308312
})
309313
}
310314

sdk/metric/provider_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stretchr/testify/require"
1616

1717
"go.opentelemetry.io/otel"
18+
"go.opentelemetry.io/otel/attribute"
1819
api "go.opentelemetry.io/otel/metric"
1920
"go.opentelemetry.io/otel/metric/noop"
2021
"go.opentelemetry.io/otel/sdk/metric/metricdata"
@@ -95,6 +96,7 @@ func TestMeterProviderReturnsSameMeter(t *testing.T) {
9596

9697
assert.Same(t, mtr, mp.Meter(""))
9798
assert.NotSame(t, mtr, mp.Meter("diff"))
99+
assert.Same(t, mtr, mp.Meter("", api.WithInstrumentationAttributes(attribute.String("k", "v")))) // TODO (#3368): Change to assert.NotSame.
98100
}
99101

100102
func TestEmptyMeterName(t *testing.T) {

sdk/trace/provider_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
1515

16+
"go.opentelemetry.io/otel/attribute"
1617
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
1718
"go.opentelemetry.io/otel/trace"
1819
)
@@ -380,3 +381,17 @@ func testStoredError(t *testing.T, target interface{}) {
380381
assert.ErrorAs(t, err, target)
381382
}
382383
}
384+
385+
func TestTracerProviderReturnsSameTracer(t *testing.T) {
386+
p := NewTracerProvider()
387+
388+
t0, t1, t2 := p.Tracer("t0"), p.Tracer("t1"), p.Tracer("t0", trace.WithInstrumentationAttributes(attribute.String("foo", "bar")))
389+
assert.NotSame(t, t0, t1)
390+
assert.Same(t, t0, t2) // TODO (#3368): Change to assert.NotSame.
391+
assert.NotSame(t, t1, t2)
392+
393+
t3, t4, t5 := p.Tracer("t0"), p.Tracer("t1"), p.Tracer("t0", trace.WithInstrumentationAttributes(attribute.String("foo", "bar")))
394+
assert.Same(t, t0, t3)
395+
assert.Same(t, t1, t4)
396+
assert.Same(t, t2, t5)
397+
}

sdk/trace/trace_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,11 @@ func cmpDiff(x, y interface{}) string {
900900
cmp.AllowUnexported(snapshot{}),
901901
cmp.AllowUnexported(attribute.Value{}),
902902
cmp.AllowUnexported(Event{}),
903-
cmp.AllowUnexported(trace.TraceState{}))
903+
cmp.AllowUnexported(trace.TraceState{}),
904+
cmp.Comparer(func(x, y attribute.Set) bool {
905+
return x.Equals(&y)
906+
}),
907+
)
904908
}
905909

906910
// checkChild is test utility function that tests that c has fields set appropriately,

0 commit comments

Comments
 (0)