Skip to content

Commit

Permalink
PMM-12473 do not mangle the query string for external exporters (#2636)
Browse files Browse the repository at this point in the history
* PMM-12473 do not mangle the query string  for external exporters

* PMM-12473 fix setup.py

* PMM-12473 get away with less variables
  • Loading branch information
ademidoff authored Dec 22, 2023
1 parent 9ef767c commit 6ed69a8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
5 changes: 2 additions & 3 deletions .devcontainer/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def setup():
# Having fsync off in dev environment is fine.
"sed -i -e \"s/#fsync = on/fsync = off/\" /srv/postgres14/postgresql.conf",
"echo 'host all all 0.0.0.0/0 trust' >> /srv/postgres14/pg_hba.conf",
"supervisorctl restart postgresql",
# "supervisorctl restart postgresql",
])


Expand All @@ -99,8 +99,7 @@ def main():
make_init()

# do basic setup
# TODO: fix the setup and revert
# setup()
setup()


MARKER = "/tmp/devcontainer-setup-done"
Expand Down
17 changes: 13 additions & 4 deletions managed/models/agent_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ type DSNParams struct {
PostgreSQLSupportsSSLSNI bool
}

// DSN returns DSN string for accessing given Service with this Agent (and implicit driver).
// DSN returns a DSN string for accessing a given Service with this Agent (and an implicit driver).
func (s *Agent) DSN(service *Service, dsnParams DSNParams, tdp *DelimiterPair) string { //nolint:cyclop,maintidx
host := pointer.GetString(service.Address)
port := pointer.GetUint16(service.Port)
Expand Down Expand Up @@ -516,9 +516,9 @@ func (s *Agent) DSN(service *Service, dsnParams DSNParams, tdp *DelimiterPair) s
if socket == "" {
address = net.JoinHostPort(host, strconv.Itoa(int(port)))
} else {
// Set socket dirrectory as host URI parameter.
// Set socket directory as host URI parameter.
q.Set("host", socket)
// In case of empty url.URL.Host we need to identify a start of a path (database name).
// In case of empty url.URL.Host we need to identify the start of the path (database name).
database = "/" + database
}

Expand Down Expand Up @@ -563,11 +563,20 @@ func (s *Agent) ExporterURL(q *reform.Querier) (string, error) {
}

address := net.JoinHostPort(host, strconv.Itoa(listenPort))
// We have to split MetricsPath into the path and the query because it may contain both.
// Example: "/metrics?format=prometheus&output=json"
p := strings.Split(path, "?")

u := &url.URL{
Scheme: scheme,
Host: address,
Path: path,
Path: p[0],
}

if len(p) > 1 {
u.RawQuery = p[1]
}

switch {
case password != "":
u.User = url.UserPassword(username, password)
Expand Down
49 changes: 46 additions & 3 deletions managed/models/agent_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ func TestExporterURL(t *testing.T) {
Address: "redis_exporter",
},

&models.Node{
NodeID: "ExporterServerlessNodeID2",
NodeType: models.RemoteNodeType,
NodeName: "Node 2 for Serverless Exporter",
Address: "nomad_exporter",
},

&models.Service{
ServiceID: "external",
ServiceType: models.ExternalServiceType,
Expand All @@ -417,6 +424,14 @@ func TestExporterURL(t *testing.T) {
ExternalGroup: "redis",
},

&models.Service{
ServiceID: "nomad_exporter-external",
ServiceType: models.ExternalServiceType,
ServiceName: "Service on ExporterServerlessNode 2",
NodeID: "ExporterServerlessNodeID2",
ExternalGroup: "nomad",
},

&models.Agent{
AgentID: "ExporterAgentPush",
AgentType: models.ExternalExporterType,
Expand Down Expand Up @@ -453,6 +468,32 @@ func TestExporterURL(t *testing.T) {
Username: pointer.ToString("user"),
Password: pointer.ToString("secret"),
},

&models.Agent{
AgentID: "ExporterServerlessWithQueryParams",
AgentType: models.ExternalExporterType,
RunsOnNodeID: pointer.ToString("ExporterServerlessNodeID2"),
ServiceID: pointer.ToString("nomad_exporter-external"),
MetricsScheme: pointer.ToString("http"),
PushMetrics: false,
ListenPort: pointer.ToUint16(9121),
MetricsPath: pointer.ToString("/metrics?format=prometheus&output=json"),
Username: pointer.ToString("user"),
Password: pointer.ToString("secret"),
},

&models.Agent{
AgentID: "ExporterServerlessWithEmptyMetricsPath",
AgentType: models.ExternalExporterType,
RunsOnNodeID: pointer.ToString("ExporterServerlessNodeID2"),
ServiceID: pointer.ToString("nomad_exporter-external"),
MetricsScheme: pointer.ToString("http"),
PushMetrics: false,
ListenPort: pointer.ToUint16(9121),
MetricsPath: pointer.ToString("/"),
Username: pointer.ToString("user"),
Password: pointer.ToString("secret"),
},
} {
require.NoError(t, q.Insert(str), "failed to INSERT %+v", str)
}
Expand All @@ -469,9 +510,11 @@ func TestExporterURL(t *testing.T) {
defer teardown(t)

for agentID, expected := range map[string]string{
"ExporterAgentPush": "http://127.0.0.1:9121/metrics",
"ExporterAgentPull": "http://user:[email protected]:9121/metrics",
"ExporterServerless": "http://user:secret@redis_exporter:9121/metrics",
"ExporterAgentPush": "http://127.0.0.1:9121/metrics",
"ExporterAgentPull": "http://user:[email protected]:9121/metrics",
"ExporterServerless": "http://user:secret@redis_exporter:9121/metrics",
"ExporterServerlessWithQueryParams": "http://user:secret@nomad_exporter:9121/metrics?format=prometheus&output=json",
"ExporterServerlessWithEmptyMetricsPath": "http://user:secret@nomad_exporter:9121/",
} {
t.Run(agentID, func(t *testing.T) {
agent, err := models.FindAgentByID(q, agentID)
Expand Down

0 comments on commit 6ed69a8

Please sign in to comment.