Skip to content

fromEnv detector throws an error when OTEL_RESOURCE_ATTRIBUTES is not set. #2138

@seanschade

Description

@seanschade

Description

If you set OTEL_SERVICE_NAME, but do not set OTEL_RESOURCE_ATTRIBUTES an errMissingValue occurs.

"error": "partial resource: missing value: []"

Environment

  • OS: Darwin
  • Architecture: amd64
  • Go Version: 1.16.3
  • opentelemetry-go version: v1.0.0-RC1 and v1.0.0-RC2

Steps To Reproduce

Add the following test case to env_test.go, and it will fail.

func TestNoResourceAttributesSet(t *testing.T) {
	store, err := ottest.SetEnvVariables(map[string]string{
		svcNameKey:      "bar",
	})
	require.NoError(t, err)
	defer func() { require.NoError(t, store.Restore()) }()
	detector := &fromEnv{}
	res, err := detector.Detect(context.Background())
	require.NoError(t, err)
	assert.Equal(t, res, NewSchemaless(
		semconv.ServiceNameKey.String("bar"),
	))
}

constructOTResources is being called with an empty string. We should check the input, and return an empty resource if no attributes are provided.

func constructOTResources(s string) (*Resource, error) {
        // len(pairs) will equal 1 with an empty string
        if s == "" {
            return Empty(), nil
        }
	pairs := strings.Split(s, ",") // len(pairs) will equal 1 with an empty string
	attrs := []attribute.KeyValue{}
	var invalid []string
	for _, p := range pairs {
		field := strings.SplitN(p, "=", 2)
		if len(field) != 2 {
			invalid = append(invalid, p)
			continue
		}
		k, v := strings.TrimSpace(field[0]), strings.TrimSpace(field[1])
		attrs = append(attrs, attribute.String(k, v))
	}
	var err error
	if len(invalid) > 0 {
		err = fmt.Errorf("%w: %v", errMissingValue, invalid)
	}
	return NewSchemaless(attrs...), err
}

Expected behavior

No error should occur.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions