Skip to content

Commit 1f092ae

Browse files
committed
Add tls support for mongodb connections
1 parent 5b3ab6e commit 1f092ae

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ApplicationInstrumentation struct {
1212
Type string `json:"type"`
1313
Host string `json:"host"`
1414
Port string `json:"port"`
15+
Sni string `json:"sni"`
1516
Credentials Credentials `json:"credentials"`
1617
Params map[string]string `json:"params"`
1718
Instance string `json:"instance"`

metrics/mongo/mongo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mongo
22

33
import (
44
"context"
5+
"crypto/tls"
56
"fmt"
67
"sort"
78
"strings"
@@ -62,7 +63,7 @@ type Collector struct {
6263
prevSettingsText string
6364
}
6465

65-
func New(host, username, password string, scrapeInterval, collectTimeout time.Duration,
66+
func New(host, username, password, tlsOpt string, sni string, scrapeInterval, collectTimeout time.Duration,
6667
logger logger.Logger, emitter dbtracker.ChangeEmitter, targetAddr string,
6768
maxTablesPerDB int, trackSizes bool) *Collector {
6869

@@ -90,6 +91,12 @@ func New(host, username, password string, scrapeInterval, collectTimeout time.Du
9091
Password: password,
9192
})
9293
}
94+
if tlsOpt == "true" {
95+
c.clientOpts.SetTLSConfig(&tls.Config{
96+
ServerName: sni,
97+
MinVersion: tls.VersionTLS12,
98+
})
99+
}
93100
trackSchema := c.emitter != nil
94101
if trackSchema || trackSizes {
95102
c.dbTracker = newDatabaseTracker(maxTablesPerDB, trackSchema, trackSizes, logger)

metrics/target.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type CredentialsSecret struct {
4747
type Target struct {
4848
Type TargetType
4949
Addr string
50+
Sni string
5051
Credentials Credentials
5152
CredentialsSecret CredentialsSecret
5253
Params map[string]string
@@ -152,10 +153,17 @@ func (t *Target) StartExporter(reg *prometheus.Registry, credentials Credentials
152153
t.stop = func() {}
153154

154155
case TargetTypeMongodb:
156+
sni := t.Sni
157+
tlsParam := t.Params["tls"]
158+
if tlsParam == "" {
159+
tlsParam = "false"
160+
}
155161
collector := mongo.New(
156162
t.Addr,
157163
credentials.Username,
158164
credentials.Password,
165+
tlsParam,
166+
sni,
159167
scrapeInterval,
160168
collectTimeout,
161169
t.logger,
@@ -198,6 +206,7 @@ func TargetFromConfig(i config.ApplicationInstrumentation) *Target {
198206
t := &Target{
199207
Type: TargetType(i.Type),
200208
Addr: net.JoinHostPort(i.Host, i.Port),
209+
Sni: i.Sni,
201210
Credentials: Credentials{
202211
Username: i.Credentials.Username,
203212
Password: i.Credentials.Password,
@@ -277,6 +286,7 @@ func TargetFromPod(pod *k8s.Pod) *Target {
277286
t = &Target{
278287
Type: TargetTypeMongodb,
279288
Addr: net.JoinHostPort(pod.IP, cmp.Or(pod.Annotations["coroot.com/mongodb-scrape-port"], "27017")),
289+
Sni: pod.Id.Name,
280290
Credentials: Credentials{
281291
Username: pod.Annotations["coroot.com/mongodb-scrape-credentials-username"],
282292
Password: pod.Annotations["coroot.com/mongodb-scrape-credentials-password"],
@@ -287,6 +297,9 @@ func TargetFromPod(pod *k8s.Pod) *Target {
287297
UsernameKey: pod.Annotations["coroot.com/mongodb-scrape-credentials-secret-username-key"],
288298
PasswordKey: pod.Annotations["coroot.com/mongodb-scrape-credentials-secret-password-key"],
289299
},
300+
Params: map[string]string{
301+
"tls": pod.Annotations["coroot.com/mongodb-scrape-param-tls"],
302+
},
290303
}
291304
}
292305

0 commit comments

Comments
 (0)