Skip to content

Commit

Permalink
[processor/resourcedetectionprocessor] handle partial presence of her…
Browse files Browse the repository at this point in the history
…oku metadata (#25059)

**Description:**
Currently, heroku metadata is only collected if the `HEROKU_DYNO_ID`
environment variable is present.

It turns that under some circumstances this environment variable may be
missing, but the rest of the variables may still be present. We collect
what we can and offer to log at debug level if some or all metadata is
missing to avoid cluttering logs.

**Testing:**
Unit tests.

**Documentation:**
No changes.
  • Loading branch information
atoulme authored Aug 8, 2023
1 parent 64142a8 commit f70c6b0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
27 changes: 27 additions & 0 deletions .chloggen/log-partial-metadata-heroku.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Collect heroku metadata available instead of exiting early. Log at debug level if metadata is missing to help troubleshooting.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25059]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
25 changes: 19 additions & 6 deletions processor/resourcedetectionprocessor/internal/heroku/heroku.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,29 @@ type detector struct {

// Detect detects heroku metadata and returns a resource with the available ones
func (d *detector) Detect(_ context.Context) (resource pcommon.Resource, schemaURL string, err error) {
dynoID, ok := os.LookupEnv("HEROKU_DYNO_ID")
if !ok {
d.logger.Debug("heroku metadata unavailable", zap.Error(err))
return pcommon.NewResource(), "", nil
dynoIDMissing := false
if dynoID, ok := os.LookupEnv("HEROKU_DYNO_ID"); ok {
d.rb.SetServiceInstanceID(dynoID)
} else {
dynoIDMissing = true
}

d.rb.SetCloudProvider("heroku")
d.rb.SetServiceInstanceID(dynoID)
herokuAppIDMissing := false
if v, ok := os.LookupEnv("HEROKU_APP_ID"); ok {
d.rb.SetHerokuAppID(v)
} else {
herokuAppIDMissing = true
}
if dynoIDMissing {
if herokuAppIDMissing {
d.logger.Debug("Heroku metadata is missing. Please check metadata is enabled.")
} else {
// some heroku deployments will enable some of the metadata.
d.logger.Debug("Partial Heroku metadata is missing. Please check metadata is supported.")
}
}
if !herokuAppIDMissing {
d.rb.SetCloudProvider("heroku")
}
if v, ok := os.LookupEnv("HEROKU_APP_NAME"); ok {
d.rb.SetServiceName(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/processor/processortest"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
)
Expand Down Expand Up @@ -62,13 +61,31 @@ func TestDetectTruePartial(t *testing.T) {
res.Attributes().AsRaw())
}

func TestDetectTruePartialMissingDynoId(t *testing.T) {
t.Setenv("HEROKU_APP_ID", "appid")
t.Setenv("HEROKU_APP_NAME", "appname")
t.Setenv("HEROKU_RELEASE_VERSION", "v1")

detector, err := NewDetector(processortest.NewNopCreateSettings(), CreateDefaultConfig())
require.NoError(t, err)
res, schemaURL, err := detector.Detect(context.Background())
assert.Equal(t, conventions.SchemaURL, schemaURL)
require.NoError(t, err)
assert.Equal(t, map[string]any{
"heroku.app.id": "appid",
"service.name": "appname",
"service.version": "v1",
"cloud.provider": "heroku",
},
res.Attributes().AsRaw())
}

func TestDetectFalse(t *testing.T) {

detector := &detector{
logger: zap.NewNop(),
}
detector, err := NewDetector(processortest.NewNopCreateSettings(), CreateDefaultConfig())
require.NoError(t, err)
res, schemaURL, err := detector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, "", schemaURL)
assert.Equal(t, "https://opentelemetry.io/schemas/1.6.1", schemaURL)
assert.True(t, internal.IsEmptyResource(res))
}

0 comments on commit f70c6b0

Please sign in to comment.