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

[receiver/sshcheck] Include SSH endpoint resource attribute #24676

20 changes: 20 additions & 0 deletions .chloggen/add-ssh-endpoint-resource-attribute.yaml
Original file line number Diff line number Diff line change
@@ -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:
5 changes: 4 additions & 1 deletion receiver/sshcheckreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type sshcheckScraper struct {
*configssh.Client
*Config
settings component.TelemetrySettings
rb *metadata.ResourceBuilder
mb *metadata.MetricsBuilder
}

Expand Down Expand Up @@ -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)
DewaldDeJager marked this conversation as resolved.
Show resolved Hide resolved
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),
}
}
Expand Down
36 changes: 36 additions & 0 deletions receiver/sshcheckreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down