Skip to content

Commit f70c6b0

Browse files
authored
[processor/resourcedetectionprocessor] handle partial presence of heroku 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.
1 parent 64142a8 commit f70c6b0

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: resourcedetectionprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Collect heroku metadata available instead of exiting early. Log at debug level if metadata is missing to help troubleshooting.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [25059]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/resourcedetectionprocessor/internal/heroku/heroku.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,29 @@ type detector struct {
3737

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

46-
d.rb.SetCloudProvider("heroku")
47-
d.rb.SetServiceInstanceID(dynoID)
47+
herokuAppIDMissing := false
4848
if v, ok := os.LookupEnv("HEROKU_APP_ID"); ok {
4949
d.rb.SetHerokuAppID(v)
50+
} else {
51+
herokuAppIDMissing = true
52+
}
53+
if dynoIDMissing {
54+
if herokuAppIDMissing {
55+
d.logger.Debug("Heroku metadata is missing. Please check metadata is enabled.")
56+
} else {
57+
// some heroku deployments will enable some of the metadata.
58+
d.logger.Debug("Partial Heroku metadata is missing. Please check metadata is supported.")
59+
}
60+
}
61+
if !herokuAppIDMissing {
62+
d.rb.SetCloudProvider("heroku")
5063
}
5164
if v, ok := os.LookupEnv("HEROKU_APP_NAME"); ok {
5265
d.rb.SetServiceName(v)

processor/resourcedetectionprocessor/internal/heroku/heroku_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/stretchr/testify/require"
1212
"go.opentelemetry.io/collector/processor/processortest"
1313
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
14-
"go.uber.org/zap"
1514

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

64+
func TestDetectTruePartialMissingDynoId(t *testing.T) {
65+
t.Setenv("HEROKU_APP_ID", "appid")
66+
t.Setenv("HEROKU_APP_NAME", "appname")
67+
t.Setenv("HEROKU_RELEASE_VERSION", "v1")
68+
69+
detector, err := NewDetector(processortest.NewNopCreateSettings(), CreateDefaultConfig())
70+
require.NoError(t, err)
71+
res, schemaURL, err := detector.Detect(context.Background())
72+
assert.Equal(t, conventions.SchemaURL, schemaURL)
73+
require.NoError(t, err)
74+
assert.Equal(t, map[string]any{
75+
"heroku.app.id": "appid",
76+
"service.name": "appname",
77+
"service.version": "v1",
78+
"cloud.provider": "heroku",
79+
},
80+
res.Attributes().AsRaw())
81+
}
82+
6583
func TestDetectFalse(t *testing.T) {
6684

67-
detector := &detector{
68-
logger: zap.NewNop(),
69-
}
85+
detector, err := NewDetector(processortest.NewNopCreateSettings(), CreateDefaultConfig())
86+
require.NoError(t, err)
7087
res, schemaURL, err := detector.Detect(context.Background())
7188
require.NoError(t, err)
72-
assert.Equal(t, "", schemaURL)
89+
assert.Equal(t, "https://opentelemetry.io/schemas/1.6.1", schemaURL)
7390
assert.True(t, internal.IsEmptyResource(res))
7491
}

0 commit comments

Comments
 (0)