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

Use DdSite or DdUrl for MetricsForwarder API client #92

Merged
merged 1 commit into from
May 25, 2020

Conversation

mantoine96
Copy link
Contributor

@mantoine96 mantoine96 commented May 22, 2020

What does this PR do?

This PR:

  • Adds a field to the metricsForwarder struct: baseURL
  • Adds a getBaseURL method that returns the appropriate base URL based on the ddUrl or site fields in the definition of the agent
  • Sets the base URL after the client has been initialized
  • Adds the base URL used when error-ing out on invalid credentials.

Motivation

After submitting #90 and testing it out, I realized that the operator was also trying to push metrics, grabbing the credentials from the agent. Unfortunately the metrics forwarder wasn't using the DD_SITE / DD_DD_URL setting and would use the default of the client SDK, which is https://api.datadoghq.com

Additional Notes

I've tested this successfully in a 1.16 EKS cluster, using site: datadoghq.eu

PS: The validation build failure doesn't seem to be related to my changes.

@mantoine96 mantoine96 force-pushed the Use-Site-for-metrics-forwarder branch from faf3254 to b62612d Compare May 22, 2020 09:30
@mantoine96 mantoine96 marked this pull request as ready for review May 22, 2020 11:29
@mantoine96 mantoine96 requested a review from a team as a code owner May 22, 2020 11:29
@@ -653,3 +659,12 @@ func (mf *metricsForwarder) isErrChanFull() bool {
func (mf *metricsForwarder) isEventChanFull() bool {
return len(mf.eventChan) == cap(mf.eventChan)
}

func getbaseURL(dda *datadoghqv1alpha1.DatadogAgent) string {
if dda.Spec.Agent.Config.DDUrl != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

this dda.Spec.Agent can be nil

Copy link
Contributor

@ahmed-mez ahmed-mez left a comment

Choose a reason for hiding this comment

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

Thanks @thehunt33r for the contribution, I added a comment about a potential nil pointer, could you also add a unit test for the changes?

@mantoine96
Copy link
Contributor Author

@ahmed-mez Will get to it now :)

@mantoine96 mantoine96 requested a review from ahmed-mez May 22, 2020 14:03
},
want: "https://test.url.com",
},
// TODO: Add test cases.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we add a test case where both Site and DDUrl are explicitly set to test the order of precedence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -103,7 +103,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler, registerFunc func(datadog.
// Watch for changes to primary resource DatadogAgent
err = c.Watch(&source.Kind{Type: &datadoghqv1alpha1.DatadogAgent{}}, &handler.EnqueueRequestForObject{}, onCreate)
if err != nil {
return err
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how that happened. Anyhow, I undid the change. Good spot!

} else if dda.Spec.Site != "" {
return fmt.Sprintf("https://api.%s", dda.Spec.Site)
}
return "https://api.datadoghq.com"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return "https://api.datadoghq.com"
return defaultbaseURL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done :)

@mantoine96 mantoine96 force-pushed the Use-Site-for-metrics-forwarder branch 2 times, most recently from 9e6136c to 755890c Compare May 25, 2020 09:21
@mantoine96 mantoine96 requested a review from ahmed-mez May 25, 2020 09:22
@ahmed-mez
Copy link
Contributor

thanks @thehunt33r
happy to approve and merge this once the CI is all green, the current problem seems caused by a missing } in the metrics forwarder file

@mantoine96 mantoine96 force-pushed the Use-Site-for-metrics-forwarder branch from c515df2 to e1fdfbc Compare May 25, 2020 10:17
@mantoine96
Copy link
Contributor Author

🤦 whoops. Addressed.

@@ -86,6 +87,7 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
Log: datadoghqv1alpha1.LogSpec{},
Process: datadoghqv1alpha1.ProcessSpec{},
},
Site: options.Site,
Copy link
Contributor

Choose a reason for hiding this comment

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

this causes a panic when options is nil
it should be moved to the block below when we check if options != nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 Should have caught it earlier. Thank you!

Make golint happy by renaming baseUrl baseURL

Fix site reference

Log the base URL that was fetched

Co-authored-by: Ahmed Mezghani <38987709+ahmed-mez@users.noreply.github.com>

Add unit test for getbaseURL

re-fix the potential nil Agent

Add test case for testing precedence of DDUrl over Site

Use defaultBaseURL const in return

Undo change on datadogagent_controller.go

Remove Todo

forgot a }

Fix test
@mantoine96 mantoine96 force-pushed the Use-Site-for-metrics-forwarder branch from e1fdfbc to 6f9db62 Compare May 25, 2020 11:02
@@ -104,6 +105,7 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
}

ad.Spec.Agent.DaemonsetName = options.AgentDaemonsetName
ad.Spec.Site = options.Site
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ahmed-mez Addressed here

@mantoine96 mantoine96 requested a review from ahmed-mez May 25, 2020 11:08
@codecov-commenter
Copy link

Codecov Report

Merging #92 into master will increase coverage by 0.00%.
The diff coverage is 50.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #92   +/-   ##
=======================================
  Coverage   57.23%   57.24%           
=======================================
  Files          31       31           
  Lines        4165     4175   +10     
=======================================
+ Hits         2384     2390    +6     
- Misses       1600     1604    +4     
  Partials      181      181           
Flag Coverage Δ
#unittests 57.24% <50.00%> (+<0.01%) ⬆️
Impacted Files Coverage Δ
pkg/controller/utils/datadog/metrics_forwarder.go 42.30% <50.00%> (+0.53%) ⬆️

Continue to review full report at Codecov.

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

Copy link
Contributor

@ahmed-mez ahmed-mez left a comment

Choose a reason for hiding this comment

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

💯

@ahmed-mez ahmed-mez merged commit e44716e into DataDog:master May 25, 2020
@clamoriniere clamoriniere added this to the v0.3 milestone Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants