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

Add metadata forwarder #1485

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Add metadata forwarder #1485

wants to merge 1 commit into from

Conversation

khewonc
Copy link
Contributor

@khewonc khewonc commented Oct 24, 2024

What does this PR do?

A brief description of the change being made with this pull request.

Motivation

What inspired you to submit this pull request?

Additional Notes

Anything else we should know when reviewing?

Minimum Agent Versions

Are there minimum versions of the Datadog Agent and/or Cluster Agent required?

  • Agent: vX.Y.Z
  • Cluster Agent: vX.Y.Z

Describe your test plan

Write there any instructions and details you may have to test your PR.

Checklist

  • PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies
  • PR has a milestone or the qa/skip-qa label

@khewonc khewonc added enhancement New feature or request qa/skip-qa labels Oct 24, 2024
func getHeaders() http.Header {
header := http.Header{}
header.Set(apiHTTPHeaderKey, os.Getenv("DD_API_KEY"))
header.Set(useragentHTTPHeaderKey, fmt.Sprintf("datadog-operator/%s", "test")) // TODO: use operator version

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

Suggested change
header.Set(useragentHTTPHeaderKey, fmt.Sprintf("datadog-operator/%s", "test")) // TODO: use operator version
header.Set(useragentHTTPHeaderKey, "datadog-operator/test") // TODO: use operator version
No need to use fmt.Sprintf("%s", var) when var is a string. (...read more)

In Go, it is recommended to avoid using fmt.Sprintf() unnecessarily when printing a string and instead use the string directly. This guideline promotes code simplicity, readability, and execution efficiency. Here are a few reasons why using the string directly is preferred over fmt.Sprintf() for simple string printing:

  1. Readability: Using the string directly is more concise and easier to read, especially when the format specifiers offered by fmt.Sprintf() are not needed. By avoiding the additional fmt.Sprintf() call, the code becomes cleaner and more straightforward, conveying its intention more effectively.
  2. Performance: Invoking fmt.Sprintf() requires additional CPU cycles and memory allocation. Although the overhead may seem insignificant in isolation, repeated usage or in performance-critical code paths can impact the overall runtime efficiency of your program. By directly printing the string, you eliminate the unnecessary overhead of formatting and allocation associated with fmt.Sprintf().
  3. Type safety: When using fmt.Sprintf(), the compiler cannot check the correctness of the format specifiers and arguments. This can potentially lead to runtime errors or incorrect output if the format specifiers or arguments do not match. By directly using the string, you avoid the risk of format string-related errors and ensure type safety.

That said, fmt.Sprintf() can still be useful in scenarios where formatting is needed, such as when building complex strings or including variable values within the output. fmt.Sprintf() offers powerful formatting options and is essential for more advanced string formatting requirements.

In summary, when it comes to simple string printing without any formatting needs or complex variable substitutions, it is best to use the string directly instead of fmt.Sprintf(). This approach promotes code simplicity, better performance, and improved code readability. However, when formatting is necessary, fmt.Sprintf() remains a powerful tool to handle more intricate string construction.

View in Datadog  Leave us feedback  Documentation

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 1.52672% with 129 lines in your changes missing coverage. Please review.

Project coverage is 48.40%. Comparing base (734366f) to head (689ea00).

Files with missing lines Patch % Lines
pkg/controller/utils/metadata/metadata.go 0.00% 84 Missing ⚠️
cmd/main.go 0.00% 23 Missing ⚠️
pkg/controller/utils/metadata/payload.go 0.00% 20 Missing ⚠️
pkg/version/version.go 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1485      +/-   ##
==========================================
- Coverage   48.72%   48.40%   -0.32%     
==========================================
  Files         224      226       +2     
  Lines       19845    19976     +131     
==========================================
+ Hits         9669     9670       +1     
- Misses       9668     9798     +130     
  Partials      508      508              
Flag Coverage Δ
unittests 48.40% <1.52%> (-0.32%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
internal/controller/setup.go 53.38% <100.00%> (+0.35%) ⬆️
pkg/version/version.go 0.00% <0.00%> (ø)
pkg/controller/utils/metadata/payload.go 0.00% <0.00%> (ø)
cmd/main.go 0.00% <0.00%> (ø)
pkg/controller/utils/metadata/metadata.go 0.00% <0.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 734366f...689ea00. Read the comment docs.

@@ -92,6 +93,7 @@ func SetupControllers(logger logr.Logger, mgr manager.Manager, options SetupOpti
}

if versionInfo != nil {
mf.OperatorMetadata.KubernetesVersion = versionInfo.String()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could define discoveryClient one level up and pass it to SetupControllers, instead of passing mf? (since mf isn't related to controller setup.) then we could declare versionInfo one level up too and pass it to setupMetadataForwarder

Path: defaultURLPath,
}
// prioritize URL set in env var
if mdURLFromEnvVar := os.Getenv("METADATA_URL"); mdURLFromEnvVar != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want this to be configurable?


func getTickerInterval(logger logr.Logger) time.Duration {
interval := defaultInterval
if s := os.Getenv("METADATA_INTERVAL"); s != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we need this to be configurable as well

now := time.Now().Unix()

// prioritize custom payload from env var
if payloadFromEnvVar := os.Getenv("PAYLOAD"); payloadFromEnvVar != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume this was for testing - do you plan to keep it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request qa/skip-qa
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants