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

[chore][receiver/zookeeper] Refactor to separate connection/request from metric processing #22752

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions receiver/zookeeperreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,57 @@ func (z *zookeeperMetricsScraper) scrape(ctx context.Context) (pmetric.Metrics,
var ctxWithTimeout context.Context
ctxWithTimeout, z.cancel = context.WithTimeout(ctx, z.config.Timeout)

response, err := z.runCommand(ctxWithTimeout, "mntr")
if err != nil {
return pmetric.NewMetrics(), err
songy23 marked this conversation as resolved.
Show resolved Hide resolved
}

return z.processMntr(response)
}

func (z *zookeeperMetricsScraper) runCommand(ctx context.Context, command string) ([]string, error) {
conn, err := z.config.Dial()
if err != nil {
z.logger.Error("failed to establish connection",
zap.String("endpoint", z.config.Endpoint),
zap.Error(err),
)
return pmetric.NewMetrics(), err
return nil, err
}
defer func() {
if closeErr := z.closeConnection(conn); closeErr != nil {
z.logger.Warn("failed to shutdown connection", zap.Error(closeErr))
}
}()

deadline, ok := ctxWithTimeout.Deadline()
deadline, ok := ctx.Deadline()
if ok {
if err := z.setConnectionDeadline(conn, deadline); err != nil {
if err = z.setConnectionDeadline(conn, deadline); err != nil {
z.logger.Warn("failed to set deadline on connection", zap.Error(err))
}
}

return z.getResourceMetrics(conn)
}

func (z *zookeeperMetricsScraper) getResourceMetrics(conn net.Conn) (pmetric.Metrics, error) {
scanner, err := z.sendCmd(conn, mntrCommand)
scanner, err := z.sendCmd(conn, command)
if err != nil {
z.logger.Error("failed to send command",
zap.Error(err),
zap.String("command", mntrCommand),
zap.String("command", command),
)
return pmetric.NewMetrics(), err
return nil, err
}

var response []string
for scanner.Scan() {
response = append(response, scanner.Text())
}
return response, nil
}

func (z *zookeeperMetricsScraper) processMntr(response []string) (pmetric.Metrics, error) {
creator := newMetricCreator(z.mb)
now := pcommon.NewTimestampFromTime(time.Now())
resourceOpts := make([]metadata.ResourceMetricsOption, 0, 2)
for scanner.Scan() {
line := scanner.Text()
for _, line := range response {
parts := zookeeperFormatRE.FindStringSubmatch(line)
if len(parts) != 3 {
z.logger.Warn("unexpected line in response",
Expand Down Expand Up @@ -155,7 +167,6 @@ func (z *zookeeperMetricsScraper) getResourceMetrics(conn net.Conn) (pmetric.Met

// Generate computed metrics
creator.generateComputedMetrics(z.logger, now)

return z.mb.Emit(resourceOpts...), nil
}

Expand Down
8 changes: 4 additions & 4 deletions receiver/zookeeperreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func TestZookeeperMetricsScraperScrape(t *testing.T) {
name: "Error closing connection",
mockedZKOutputSourceFilename: "mntr-3.4.14",
expectedLogs: []logMsg{
{
msg: "metric computation failed",
level: zapcore.DebugLevel,
},
{
msg: "failed to shutdown connection",
level: zapcore.WarnLevel,
},
{
msg: "metric computation failed",
level: zapcore.DebugLevel,
},
},
expectedMetricsFilename: "error-closing-connection",
expectedResourceAttributes: map[string]string{
Expand Down