-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serving SRV targets and getting all nodes in single request (#835)
* overall handler + label wrapper * srv seed list + splitCluster option * added request opts to overall handler * added splitcluster+srv tests * added overall handler test * readme update * fixed most linters * liners * added licence header * updated sync package * formatted * Update v1_compatibility_test.go * fixes due to idoqo review * overall handler + label wrapper * srv seed list + splitCluster option * added request opts to overall handler * added splitcluster+srv tests * added overall handler test * readme update * fixed most linters * liners * added licence header * fixes due to idoqo review * fix redeclaration * missing collector.pbm * fix prev merge * fix enable fcv Signed-off-by: ad <d.lexand@gmail.com> --------- Signed-off-by: ad <d.lexand@gmail.com> Co-authored-by: Jiří Čtvrtka <62988319+JiriCtvrtka@users.noreply.github.com> Co-authored-by: Nurlan Moldomurov <bupychuk1989@gmail.com> Co-authored-by: Nurlan Moldomurov <nurlan.moldomurov@percona.com>
- Loading branch information
1 parent
669a25b
commit 7aaecd0
Showing
15 changed files
with
619 additions
and
62 deletions.
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
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,59 @@ | ||
// mongodb_exporter | ||
// Copyright (C) 2017 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exporter | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
io_prometheus_client "github.com/prometheus/client_model/go" | ||
) | ||
|
||
// GathererWrapped is a wrapper for prometheus.Gatherer that adds labels to all metrics. | ||
type GathererWrapped struct { | ||
originalGatherer prometheus.Gatherer | ||
labels prometheus.Labels | ||
} | ||
|
||
// NewGathererWrapper creates a new GathererWrapped with the given Gatherer and additional labels. | ||
func NewGathererWrapper(gs prometheus.Gatherer, labels prometheus.Labels) *GathererWrapped { | ||
return &GathererWrapped{ | ||
originalGatherer: gs, | ||
labels: labels, | ||
} | ||
} | ||
|
||
// Gather implements prometheus.Gatherer interface. | ||
func (g *GathererWrapped) Gather() ([]*io_prometheus_client.MetricFamily, error) { | ||
metrics, err := g.originalGatherer.Gather() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to gather metrics") | ||
} | ||
|
||
for _, metric := range metrics { | ||
for _, m := range metric.GetMetric() { | ||
for k, v := range g.labels { | ||
v := v | ||
k := k | ||
m.Label = append(m.Label, &io_prometheus_client.LabelPair{ | ||
Name: &k, | ||
Value: &v, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return metrics, nil | ||
} |
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,88 @@ | ||
// mongodb_exporter | ||
// Copyright (C) 2017 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exporter | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// GetSeedListFromSRV converts mongodb+srv URI to flat connection string. | ||
func GetSeedListFromSRV(uri string, log *logrus.Logger) string { | ||
uriParsed, err := url.Parse(uri) | ||
if err != nil { | ||
log.Fatalf("Failed to parse URI %s: %v", uri, err) | ||
} | ||
|
||
cname, srvRecords, err := net.LookupSRV("mongodb", "tcp", uriParsed.Hostname()) | ||
if err != nil { | ||
log.Errorf("Failed to lookup SRV records for %s: %v", uri, err) | ||
return uri | ||
} | ||
|
||
if len(srvRecords) == 0 { | ||
log.Errorf("No SRV records found for %s", uri) | ||
return uri | ||
} | ||
|
||
queryString := uriParsed.RawQuery | ||
|
||
txtRecords, err := net.LookupTXT(uriParsed.Hostname()) | ||
if err != nil { | ||
log.Errorf("Failed to lookup TXT records for %s: %v", cname, err) | ||
} | ||
if len(txtRecords) > 1 { | ||
log.Errorf("Multiple TXT records found for %s, thus were not applied", cname) | ||
} | ||
if len(txtRecords) == 1 { | ||
// We take connection parameters from the TXT record | ||
uriParams, err := url.ParseQuery(txtRecords[0]) | ||
if err != nil { | ||
log.Errorf("Failed to parse TXT record %s: %v", txtRecords[0], err) | ||
} else { | ||
// Override connection parameters with ones from URI query string | ||
for p, v := range uriParsed.Query() { | ||
uriParams[p] = v | ||
} | ||
queryString = uriParams.Encode() | ||
} | ||
} | ||
|
||
// Build final connection URI | ||
servers := make([]string, len(srvRecords)) | ||
for i, srv := range srvRecords { | ||
servers[i] = net.JoinHostPort(strings.TrimSuffix(srv.Target, "."), strconv.FormatUint(uint64(srv.Port), 10)) | ||
} | ||
uri = "mongodb://" | ||
if uriParsed.User != nil { | ||
uri += uriParsed.User.String() + "@" | ||
} | ||
uri += strings.Join(servers, ",") | ||
if uriParsed.Path != "" { | ||
uri += uriParsed.Path | ||
} else { | ||
uri += "/" | ||
} | ||
if queryString != "" { | ||
uri += "?" + queryString | ||
} | ||
|
||
return uri | ||
} |
Oops, something went wrong.