diff --git a/.chloggen/add-ssh-endpoint-resource-attribute.yaml b/.chloggen/add-ssh-endpoint-resource-attribute.yaml new file mode 100644 index 000000000000..632723f70fa2 --- /dev/null +++ b/.chloggen/add-ssh-endpoint-resource-attribute.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: receiver/sshcheck + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add the SSH endpoint as a resource attribute + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [24441] + +# (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: diff --git a/receiver/sshcheckreceiver/scraper.go b/receiver/sshcheckreceiver/scraper.go index 25131dc367ad..bfc95407c944 100644 --- a/receiver/sshcheckreceiver/scraper.go +++ b/receiver/sshcheckreceiver/scraper.go @@ -24,6 +24,7 @@ type sshcheckScraper struct { *configssh.Client *Config settings component.TelemetrySettings + rb *metadata.ResourceBuilder mb *metadata.MetricsBuilder } @@ -124,13 +125,15 @@ func (s *sshcheckScraper) scrape(ctx context.Context) (_ pmetric.Metrics, err er } } - return s.mb.Emit(), nil + s.rb.SetSSHEndpoint(s.Config.SSHClientSettings.Endpoint) + return s.mb.Emit(metadata.WithResource(s.rb.Emit())), nil } func newScraper(conf *Config, settings receiver.CreateSettings) *sshcheckScraper { return &sshcheckScraper{ Config: conf, settings: settings.TelemetrySettings, + rb: metadata.NewResourceBuilder(conf.MetricsBuilderConfig.ResourceAttributes), mb: metadata.NewMetricsBuilder(conf.MetricsBuilderConfig, settings), } } diff --git a/receiver/sshcheckreceiver/scraper_test.go b/receiver/sshcheckreceiver/scraper_test.go index 43dd3789e7fe..81f6deb5e9e8 100644 --- a/receiver/sshcheckreceiver/scraper_test.go +++ b/receiver/sshcheckreceiver/scraper_test.go @@ -183,6 +183,42 @@ func TestScraper(t *testing.T) { } } +func TestScraperPropagatesResourceAttributes(t *testing.T) { + if !supportedOS() { + t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]") + } + endpoint := setupSSHServer(t) + require.NotEmpty(t, endpoint) + + f := NewFactory() + cfg := f.CreateDefaultConfig().(*Config) + cfg.MetricsBuilderConfig.ResourceAttributes.SSHEndpoint.Enabled = true + cfg.ScraperControllerSettings.CollectionInterval = 100 * time.Millisecond + cfg.Username = "otelu" + cfg.Password = "otelp" + cfg.Endpoint = endpoint + cfg.IgnoreHostKey = true + + settings := receivertest.NewNopCreateSettings() + + scraper := newScraper(cfg, settings) + require.NoError(t, scraper.start(context.Background(), componenttest.NewNopHost()), "failed starting scraper") + + actualMetrics, err := scraper.scrape(context.Background()) + require.NoError(t, err, "failed scrape") + + resourceMetrics := actualMetrics.ResourceMetrics() + expectedResourceAttributes := map[string]any{"ssh.endpoint": endpoint} + for i := 0; i < resourceMetrics.Len(); i++ { + resourceAttributes := resourceMetrics.At(i).Resource().Attributes() + for name, value := range expectedResourceAttributes { + actualAttributeValue, ok := resourceAttributes.Get(name) + require.True(t, ok) + require.Equal(t, value, actualAttributeValue.Str()) + } + } +} + func TestScraperDoesNotErrForSSHErr(t *testing.T) { if !supportedOS() { t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]")