Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
seanschade opened this issue Jul 28, 2021 · 1 comment · Fixed by #2139
Closed

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

seanschade opened this issue Jul 28, 2021 · 1 comment · Fixed by #2139
Labels
bug Something isn't working

Comments

@seanschade
Copy link
Contributor

seanschade commented Jul 28, 2021

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.

@seanschade seanschade added the bug Something isn't working label Jul 28, 2021
seanschade added a commit to seanschade/opentelemetry-go that referenced this issue Jul 28, 2021
@seanschade
Copy link
Contributor Author

Pull request to fix the bug.

#2139

@seanschade seanschade changed the title The fromEnv detector throws an error is OTEL_RESOURCE_ATTRIBUTES is not set. The fromEnv detector throws an error when OTEL_RESOURCE_ATTRIBUTES is not set. Jul 28, 2021
@seanschade seanschade changed the title The fromEnv detector throws an error when OTEL_RESOURCE_ATTRIBUTES is not set. fromEnv detector throws an error when OTEL_RESOURCE_ATTRIBUTES is not set. Jul 28, 2021
seanschade added a commit to seanschade/opentelemetry-go that referenced this issue Jul 28, 2021
MrAlias added a commit that referenced this issue Jul 29, 2021
…rce (#2139)

* fix(2138): add guard to constructOTResources to return an empty resource when attributes are not supplied

Fixes: #2138

* Update CHANGELOG.md

Co-authored-by: Robert Pająk <pellared@hotmail.com>

Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
shbieng added a commit to shbieng/opentelemetry-go that referenced this issue Aug 26, 2022
…rce (#2139)

* fix(2138): add guard to constructOTResources to return an empty resource when attributes are not supplied

Fixes: open-telemetry/opentelemetry-go#2138

* Update CHANGELOG.md

Co-authored-by: Robert Pająk <pellared@hotmail.com>

Co-authored-by: Robert Pająk <pellared@hotmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant