-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[ASCII-145] Extract collector to a subpackage of status #20718
Merged
GustavoCaso
merged 6 commits into
main
from
extract-collector-to-a-subpackage-of-status
Nov 20, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63a99fa
extract collector to its own sub-package
GustavoCaso 1f0cee2
use collector package to just collect check information
GustavoCaso 07b47ac
remove indirection for rendering the collector template
GustavoCaso 0d9e71a
apply feedback
GustavoCaso d8c61b3
apply feedback
GustavoCaso 52e7931
remove agent_metadata key from collector subpackage
GustavoCaso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2023-present Datadog, Inc. | ||
|
||
// Package collector fetch information needed to render the 'collector' section of the status page. | ||
// This will, in time, be migrated to the collector package/comp. | ||
package collector | ||
|
||
import ( | ||
"encoding/json" | ||
"expvar" | ||
) | ||
|
||
// GetStatusInfo retrives collector information | ||
func GetStatusInfo() map[string]interface{} { | ||
stats := make(map[string]interface{}) | ||
|
||
PopulateStatus(stats) | ||
|
||
return stats | ||
} | ||
|
||
// PopulateStatus populates stats with collector information | ||
func PopulateStatus(stats map[string]interface{}) { | ||
runnerStatsJSON := []byte(expvar.Get("runner").String()) | ||
runnerStats := make(map[string]interface{}) | ||
json.Unmarshal(runnerStatsJSON, &runnerStats) //nolint:errcheck | ||
stats["runnerStats"] = runnerStats | ||
|
||
autoConfigStatsJSON := []byte(expvar.Get("autoconfig").String()) | ||
autoConfigStats := make(map[string]interface{}) | ||
json.Unmarshal(autoConfigStatsJSON, &autoConfigStats) //nolint:errcheck | ||
stats["autoConfigStats"] = autoConfigStats | ||
|
||
checkSchedulerStatsJSON := []byte(expvar.Get("CheckScheduler").String()) | ||
checkSchedulerStats := make(map[string]interface{}) | ||
json.Unmarshal(checkSchedulerStatsJSON, &checkSchedulerStats) //nolint:errcheck | ||
stats["checkSchedulerStats"] = checkSchedulerStats | ||
|
||
pyLoaderData := expvar.Get("pyLoader") | ||
if pyLoaderData != nil { | ||
pyLoaderStatsJSON := []byte(pyLoaderData.String()) | ||
pyLoaderStats := make(map[string]interface{}) | ||
json.Unmarshal(pyLoaderStatsJSON, &pyLoaderStats) //nolint:errcheck | ||
stats["pyLoaderStats"] = pyLoaderStats | ||
} else { | ||
stats["pyLoaderStats"] = nil | ||
} | ||
|
||
pythonInitData := expvar.Get("pythonInit") | ||
if pythonInitData != nil { | ||
pythonInitJSON := []byte(pythonInitData.String()) | ||
pythonInit := make(map[string]interface{}) | ||
json.Unmarshal(pythonInitJSON, &pythonInit) //nolint:errcheck | ||
stats["pythonInit"] = pythonInit | ||
} else { | ||
stats["pythonInit"] = nil | ||
} | ||
|
||
inventories := expvar.Get("inventories") | ||
var inventoriesStats map[string]interface{} | ||
if inventories != nil { | ||
inventoriesStatsJSON := []byte(inventories.String()) | ||
json.Unmarshal(inventoriesStatsJSON, &inventoriesStats) //nolint:errcheck | ||
} | ||
|
||
checkMetadata := map[string]map[string]string{} | ||
if data, ok := inventoriesStats["check_metadata"]; ok { | ||
for _, instances := range data.(map[string]interface{}) { | ||
for _, instance := range instances.([]interface{}) { | ||
metadata := map[string]string{} | ||
checkHash := "" | ||
for k, v := range instance.(map[string]interface{}) { | ||
if vStr, ok := v.(string); ok { | ||
if k == "config.hash" { | ||
checkHash = vStr | ||
} else if k != "config.provider" { | ||
metadata[k] = vStr | ||
} | ||
} | ||
} | ||
if checkHash != "" && len(metadata) != 0 { | ||
checkMetadata[checkHash] = metadata | ||
} | ||
} | ||
} | ||
} | ||
stats["inventories"] = checkMetadata | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For each of these vars,
runner
,autoconfig
,CheckScheduler
,pyLoader
, andpythonInit
, how about extracting out a helper function that handles the json unmarshaling. So instead these variables can be handled like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that we could extract the functionality of collecting the expvar. I would suggest delaying it until we have extracted all status sub packages, as we might find better helper functions or even sub packages to deal with expvars parsing.