Skip to content

Commit

Permalink
Change resource.New() to use functional options; add builtin attribut…
Browse files Browse the repository at this point in the history
…es for (host.*, telemetry.sdk.*) (#1235)

* Add a resource.Configure() with functional options

* Add a changelog

* Add tests for builtin resources

* Rename to WithoutBuiltin

* Add new test; restore environment after tests

* Apply feedback

* Apply suggestions from code review

:heart:

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Comment edits

* Rename New, former method NewFromAttributes

* NewFromAttributes->NewWithAttributes

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
jmacd and MrAlias authored Oct 31, 2020
1 parent 0341956 commit 187adeb
Show file tree
Hide file tree
Showing 36 changed files with 553 additions and 140 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Move the `go.opentelemetry.io/otel/api/global` package to `go.opentelemetry.io/otel/global`. (#1262)
- Rename correlation context header from `"otcorrelations"` to `"baggage"` to match the OpenTelemetry specification. (#1267)
- Fix `Code.UnmarshalJSON` to work with valid json only. (#1276)
- The `resource.New()` method changes signature to support builtin attributes and functional options, including `telemetry.sdk.*` and
`host.name` semantic conventions; the former method is renamed `resource.NewWithAttributes`. (#1235)

### Removed

Expand Down
15 changes: 10 additions & 5 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
func initProvider() func() {
ctx := context.Background()

// If the OpenTelemetry Collector is running on a local cluster (minikube or
// microk8s), it should be accessible through the NodePort service at the
Expand All @@ -54,13 +55,18 @@ func initProvider() func() {
)
handleErr(err, "failed to create exporter")

res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceNameKey.String("test-service"),
),
)
handleErr(err, "failed to create resource")

bsp := sdktrace.NewBatchSpanProcessor(exp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithResource(resource.New(
// the service name used to display traces in backends
semconv.ServiceNameKey.String("test-service"),
)),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)

Expand All @@ -80,7 +86,6 @@ func initProvider() func() {
pusher.Start()

return func() {
ctx := context.Background()
handleErr(tracerProvider.Shutdown(ctx), "failed to shutdown provider")
handleErr(exp.Shutdown(ctx), "failed to stop exporter")
pusher.Stop() // pushes any last exports to the receiver
Expand Down
12 changes: 11 additions & 1 deletion exporters/metric/prometheus/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ import (
// TODO: Address this issue.

func ExampleNewExportPipeline() {
// Create a resource, with builtin attributes plus R=V.
res, err := resource.New(
context.Background(),
resource.WithoutBuiltin(), // Test-only!
resource.WithAttributes(label.String("R", "V")),
)
if err != nil {
panic(err)
}

// Create a meter
exporter, err := prometheus.NewExportPipeline(
prometheus.Config{},
pull.WithResource(resource.New(label.String("R", "V"))),
pull.WithResource(res),
)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion exporters/metric/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestPrometheusExporter(t *testing.T) {
DefaultHistogramBoundaries: []float64{-0.5, 1},
},
pull.WithCachePeriod(0),
pull.WithResource(resource.New(label.String("R", "V"))),
pull.WithResource(resource.NewWithAttributes(label.String("R", "V"))),
)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/internal/transform/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestRecordAggregatorIncompatibleErrors(t *testing.T) {
makeMpb := func(kind aggregation.Kind, agg aggregation.Aggregation) (*metricpb.Metric, error) {
desc := otel.NewDescriptor("things", otel.CounterInstrumentKind, otel.Int64NumberKind)
labels := label.NewSet()
res := resource.New()
res := resource.Empty()
test := &testAgg{
kind: kind,
agg: agg,
Expand Down Expand Up @@ -357,7 +357,7 @@ func TestRecordAggregatorUnexpectedErrors(t *testing.T) {
makeMpb := func(kind aggregation.Kind, agg aggregation.Aggregation) (*metricpb.Metric, error) {
desc := otel.NewDescriptor("things", otel.CounterInstrumentKind, otel.Int64NumberKind)
labels := label.NewSet()
res := resource.New()
res := resource.Empty()
return Record(export.NewRecord(&desc, &labels, res, agg, intervalStart, intervalEnd))
}

Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/internal/transform/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestEmptyResource(t *testing.T) {
func TestResourceAttributes(t *testing.T) {
attrs := []label.KeyValue{label.Int("one", 1), label.Int("two", 2)}

got := Resource(resource.New(attrs...)).GetAttributes()
got := Resource(resource.NewWithAttributes(attrs...)).GetAttributes()
if !assert.Len(t, attrs, 2) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/internal/transform/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestSpanData(t *testing.T) {
DroppedAttributeCount: 1,
DroppedMessageEventCount: 2,
DroppedLinkCount: 3,
Resource: resource.New(label.String("rk1", "rv1"), label.Int64("rk2", 5)),
Resource: resource.NewWithAttributes(label.String("rk1", "rv1"), label.Int64("rk2", 5)),
InstrumentationLibrary: instrumentation.Library{
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption)
),
}
tp1 := sdktrace.NewTracerProvider(append(pOpts,
sdktrace.WithResource(resource.New(
sdktrace.WithResource(resource.NewWithAttributes(
label.String("rk1", "rv11)"),
label.Int64("rk2", 5),
)))...)

tp2 := sdktrace.NewTracerProvider(append(pOpts,
sdktrace.WithResource(resource.New(
sdktrace.WithResource(resource.NewWithAttributes(
label.String("rk1", "rv12)"),
label.Float32("rk3", 6.5),
)))...)
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlp_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ var (
baseKeyValues = []label.KeyValue{label.String("host", "test.com")}
cpuKey = label.Key("CPU")

testInstA = resource.New(label.String("instance", "tester-a"))
testInstB = resource.New(label.String("instance", "tester-b"))
testInstA = resource.NewWithAttributes(label.String("instance", "tester-a"))
testInstB = resource.NewWithAttributes(label.String("instance", "tester-b"))

testHistogramBoundaries = []float64{2.0, 4.0, 8.0}

Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/otlp_span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestExportSpans(t *testing.T) {
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.New(label.String("instance", "tester-a")),
Resource: resource.NewWithAttributes(label.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v0.1.0",
Expand All @@ -119,7 +119,7 @@ func TestExportSpans(t *testing.T) {
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.New(label.String("instance", "tester-a")),
Resource: resource.NewWithAttributes(label.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-b",
Version: "v0.1.0",
Expand All @@ -142,7 +142,7 @@ func TestExportSpans(t *testing.T) {
},
StatusCode: codes.Ok,
StatusMessage: "Ok",
Resource: resource.New(label.String("instance", "tester-a")),
Resource: resource.NewWithAttributes(label.String("instance", "tester-a")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v0.1.0",
Expand All @@ -164,7 +164,7 @@ func TestExportSpans(t *testing.T) {
},
StatusCode: codes.Error,
StatusMessage: "Unauthenticated",
Resource: resource.New(label.String("instance", "tester-b")),
Resource: resource.NewWithAttributes(label.String("instance", "tester-b")),
InstrumentationLibrary: instrumentation.Library{
Name: "lib-a",
Version: "v1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions exporters/stdout/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type testFixture struct {
output *bytes.Buffer
}

var testResource = resource.New(label.String("R", "V"))
var testResource = resource.NewWithAttributes(label.String("R", "V"))

func newFixture(t *testing.T, opts ...stdout.Option) testFixture {
buf := &bytes.Buffer{}
Expand Down Expand Up @@ -290,11 +290,11 @@ func TestStdoutResource(t *testing.T) {
}
testCases := []testCase{
newCase("R1=V1,R2=V2,A=B,C=D",
resource.New(label.String("R1", "V1"), label.String("R2", "V2")),
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
label.String("A", "B"),
label.String("C", "D")),
newCase("R1=V1,R2=V2",
resource.New(label.String("R1", "V1"), label.String("R2", "V2")),
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
),
newCase("A=B,C=D",
nil,
Expand All @@ -304,7 +304,7 @@ func TestStdoutResource(t *testing.T) {
// We explicitly do not de-duplicate between resources
// and metric labels in this exporter.
newCase("R1=V1,R2=V2,R1=V3,R2=V4",
resource.New(label.String("R1", "V1"), label.String("R2", "V2")),
resource.NewWithAttributes(label.String("R1", "V1"), label.String("R2", "V2")),
label.String("R1", "V3"),
label.String("R2", "V4")),
}
Expand Down
2 changes: 1 addition & 1 deletion exporters/stdout/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExporter_ExportSpan(t *testing.T) {
spanID, _ := otel.SpanIDFromHex("0102030405060708")
keyValue := "value"
doubleValue := 123.456
resource := resource.New(label.String("rk1", "rv11"))
resource := resource.NewWithAttributes(label.String("rk1", "rv11"))

testSpan := &export.SpanData{
SpanContext: otel.SpanContext{
Expand Down
2 changes: 1 addition & 1 deletion exporters/trace/jaeger/jaeger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func Test_spanDataToThrift(t *testing.T) {
StatusCode: codes.Error,
StatusMessage: statusMessage,
SpanKind: otel.SpanKindClient,
Resource: resource.New(label.String("rk1", rv1), label.Int64("rk2", rv2)),
Resource: resource.NewWithAttributes(label.String("rk1", rv1), label.Int64("rk2", rv2)),
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newFixture(b *testing.B) *benchFixture {
AggregatorSelector: processortest.AggregatorSelector(),
}

bf.accumulator = sdk.NewAccumulator(bf)
bf.accumulator = sdk.NewAccumulator(bf, nil)
bf.meter = otel.WrapMeterImpl(bf.accumulator, "benchmarks")
return bf
}
Expand Down
45 changes: 0 additions & 45 deletions sdk/metric/config.go

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/metric/controller/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func New(checkpointer export.Checkpointer, options ...Option) *Controller {
}
accum := sdk.NewAccumulator(
checkpointer,
sdk.WithResource(config.Resource),
config.Resource,
)
return &Controller{
accumulator: accum,
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/controller/push/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestWithResource(t *testing.T) {
r := resource.New(label.String("A", "a"))
r := resource.NewWithAttributes(label.String("A", "a"))

c := &Config{}
WithResource(r).Apply(c)
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/controller/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func New(checkpointer export.Checkpointer, exporter export.Exporter, opts ...Opt

impl := sdk.NewAccumulator(
checkpointer,
sdk.WithResource(c.Resource),
c.Resource,
)
return &Controller{
provider: registry.NewMeterProvider(impl),
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/controller/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
)

var testResource = resource.New(label.String("R", "V"))
var testResource = resource.NewWithAttributes(label.String("R", "V"))

type handler struct {
sync.Mutex
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/correct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

var Must = otel.Must
var testResource = resource.New(label.String("R", "V"))
var testResource = resource.NewWithAttributes(label.String("R", "V"))

type handler struct {
sync.Mutex
Expand Down Expand Up @@ -96,7 +96,7 @@ func newSDK(t *testing.T) (otel.Meter, *metricsdk.Accumulator, *correctnessProce
}
accum := metricsdk.NewAccumulator(
processor,
metricsdk.WithResource(testResource),
testResource,
)
meter := otel.WrapMeterImpl(accum, "test")
return meter, accum, processor
Expand Down
8 changes: 4 additions & 4 deletions sdk/metric/processor/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func testProcessor(
// Note: this selector uses the instrument name to dictate
// aggregation kind.
selector := processorTest.AggregatorSelector()
res := resource.New(label.String("R", "V"))
res := resource.NewWithAttributes(label.String("R", "V"))

labs1 := []label.KeyValue{label.String("L1", "V")}
labs2 := []label.KeyValue{label.String("L2", "V")}
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestBasicTimestamps(t *testing.T) {
}

func TestStatefulNoMemoryCumulative(t *testing.T) {
res := resource.New(label.String("R", "V"))
res := resource.NewWithAttributes(label.String("R", "V"))
ekind := export.CumulativeExporter

desc := otel.NewDescriptor("inst.sum", otel.CounterInstrumentKind, otel.Int64NumberKind)
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestStatefulNoMemoryCumulative(t *testing.T) {
}

func TestStatefulNoMemoryDelta(t *testing.T) {
res := resource.New(label.String("R", "V"))
res := resource.NewWithAttributes(label.String("R", "V"))
ekind := export.DeltaExporter

desc := otel.NewDescriptor("inst.sum", otel.SumObserverInstrumentKind, otel.Int64NumberKind)
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestMultiObserverSum(t *testing.T) {
export.DeltaExporter,
} {

res := resource.New(label.String("R", "V"))
res := resource.NewWithAttributes(label.String("R", "V"))
desc := otel.NewDescriptor("observe.sum", otel.SumObserverInstrumentKind, otel.Int64NumberKind)
selector := processorTest.AggregatorSelector()

Expand Down
4 changes: 1 addition & 3 deletions sdk/metric/processor/processortest/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func generateTestData(proc export.Processor) {
ctx := context.Background()
accum := metricsdk.NewAccumulator(
proc,
metricsdk.WithResource(
resource.New(label.String("R", "V")),
),
resource.NewWithAttributes(label.String("R", "V")),
)
meter := otel.WrapMeterImpl(accum, "testing")

Expand Down
Loading

0 comments on commit 187adeb

Please sign in to comment.