Skip to content

Commit fb52353

Browse files
authored
Merge pull request #48 from czephyr/main
TLS and lockstate, fault tolerance metrics
2 parents 18ebf67 + c79e787 commit fb52353

File tree

10 files changed

+573
-19
lines changed

10 files changed

+573
-19
lines changed

Dockerfile

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
FROM --platform=amd64 ubuntu:jammy
1+
FROM golang:1.22 AS download-env
22

3-
ENV GOPATH /go
3+
ENV FDB_DOWNLOAD_URL="https://github.com/apple/foundationdb/releases/download"
4+
ENV CGO_ENABLED=1
5+
ENV GOOS=linux
6+
ENV MULTVERSIONS="6.3.23 7.1.31 7.3.59"
7+
ENV VERSION="7.3.59"
48

5-
RUN apt update && apt install -y golang wget && apt-get purge -y --auto-remove \
6-
&& wget https://github.com/apple/foundationdb/releases/download/7.1.7/foundationdb-clients_7.1.7-1_amd64.deb \
7-
&& dpkg -i foundationdb*.deb
9+
RUN for v in $MULTVERSIONS; do \
10+
wget -O /tmp/libfdb_c.$v.x86_64.so $FDB_DOWNLOAD_URL/$v/libfdb_c.x86_64.so; \
11+
done && \
12+
wget -P /tmp/ ${FDB_DOWNLOAD_URL}/${VERSION}/foundationdb-clients_${VERSION}-1_amd64.deb && \
13+
dpkg -i /tmp/foundationdb-clients_${VERSION}-1_amd64.deb && \
14+
rm /tmp/foundationdb-clients_${VERSION}-1_amd64.deb
15+
16+
FROM golang:1.22 AS build-env
17+
18+
COPY --from=download-env /usr/include/foundationdb/fdb* /usr/include/foundationdb/
19+
COPY --from=download-env /usr/lib/libfdb_c.so /lib/
820

921
RUN mkdir -p /go/src/github.com/tigrisdata/fdb-exporter
1022
WORKDIR /go/src/github.com/tigrisdata/fdb-exporter
@@ -16,15 +28,13 @@ RUN GO11MODULE=on go mod download
1628

1729
COPY . .
1830

19-
RUN go build -tags=release -buildvcs=false .
20-
21-
FROM --platform=amd64 ubuntu:jammy
22-
23-
COPY --from=0 /go/src/github.com/tigrisdata/fdb-exporter/fdb-exporter /fdb-exporter
24-
25-
RUN apt update && apt install -y wget && apt-get purge -y --auto-remove \
26-
&& wget https://github.com/apple/foundationdb/releases/download/7.1.7/foundationdb-clients_7.1.7-1_amd64.deb \
27-
&& dpkg -i foundationdb*.deb \
28-
&& chmod +x /fdb-exporter
31+
RUN go build -tags=release -buildvcs=false -o /tmp/fdb-exporter
2932

30-
ENTRYPOINT [ "/fdb-exporter" ]
33+
FROM debian:trixie-slim
34+
ENV FDB_NETWORK_OPTION_IGNORE_EXTERNAL_CLIENT_FAILURES=""
35+
ENV FDB_NETWORK_OPTION_EXTERNAL_CLIENT_DIRECTORY=/usr/lib/
36+
COPY --from=download-env /tmp/libfdb*.so /usr/lib/
37+
COPY --from=download-env /usr/bin/fdbcli /usr/bin/
38+
COPY --from=download-env /usr/lib/libfdb_c.so /lib/
39+
COPY --from=build-env /tmp/fdb-exporter /usr/local/bin
40+
ENTRYPOINT ["/usr/local/bin/fdb-exporter"]

README.md

Lines changed: 375 additions & 2 deletions
Large diffs are not rendered by default.

db/conn.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package db
1616

1717
import (
18+
"bufio"
1819
"encoding/json"
1920
"os"
21+
"regexp"
2022
"strconv"
2123

2224
"github.com/rs/zerolog/log"
@@ -28,6 +30,11 @@ import (
2830

2931
const DefaultApiVersion = "710"
3032

33+
var (
34+
tls = false
35+
netopts fdb.NetworkOptions
36+
)
37+
3138
func getFdb() fdb.Database {
3239
clusterFile := os.Getenv("FDB_CLUSTER_FILE")
3340
if clusterFile == "" {
@@ -42,8 +49,48 @@ func getFdb() fdb.Database {
4249
if err != nil {
4350
log.Error().Str("FDB_API_VERSION", apiVersionStr).Msg("Could not convert api version to integer")
4451
}
52+
tlsCaFile := os.Getenv("FDB_TLS_CA_FILE")
53+
tlsCertFile := os.Getenv("FDB_TLS_CERT_FILE")
54+
tlsKeyFile := os.Getenv("FDB_TLS_KEY_FILE")
55+
tlsCheckValid := os.Getenv("FDB_TLS_VERIFY_PEERS")
4556

4657
fdb.MustAPIVersion(apiVersion)
58+
netopts = fdb.Options()
59+
60+
// Check TLS in clusterfile
61+
tls = isTLSmode(&clusterFile)
62+
if tls {
63+
// TLS params for fdbclient
64+
if len(tlsCaFile) > 0 {
65+
err := netopts.SetTLSCaPath(tlsCaFile)
66+
if err != nil {
67+
log.Error().Err(err).Str("msg", "Error cannot set CA").Msg("TLS CA error")
68+
}
69+
log.Info().Str("msg", "TLS CA File").Str("fdb.tls-ca-file", tlsCaFile).Msg("TLS CA file set")
70+
}
71+
72+
if len(tlsCertFile) > 0 {
73+
err := netopts.SetTLSCertPath(tlsCertFile)
74+
if err != nil {
75+
log.Error().Err(err).Str("msg", "Error cannot set Cert").Msg("TLS Cert error")
76+
}
77+
log.Info().Str("msg", "TLS Cert File").Str("fdb.tls-cert-file", tlsCertFile).Msg("TLS cert file set")
78+
}
79+
80+
if len(tlsKeyFile) > 0 {
81+
err := netopts.SetTLSKeyPath(tlsKeyFile)
82+
if err != nil {
83+
log.Error().Err(err).Str("msg", "Error cannot set Key").Msg("TLS Key error")
84+
}
85+
log.Info().Str("msg", "TLS Private Key").Str("fdb.tls-key-file", tlsKeyFile).Msg("TLS private key set")
86+
}
87+
88+
err := netopts.SetTLSVerifyPeers([]byte(tlsCheckValid))
89+
if err != nil {
90+
log.Error().Err(err).Str("msg", "Error cannot set VerifyPeers").Msg("TLS VerifyPeers error")
91+
}
92+
log.Info().Str("msg", "TLS VerifyPeers").Str("fdb.tls-check-valid", tlsCheckValid).Msg("TLS VerifyPeers set")
93+
}
4794
db, err := fdb.OpenDatabase(clusterFile)
4895
if err != nil {
4996
log.Error().Str("cluster_file", clusterFile).Msg("failed to open database using cluster file")
@@ -73,3 +120,31 @@ func GetStatus() (*models.FullStatus, error) {
73120
}
74121
return &status, nil
75122
}
123+
124+
func isTLSmode(c *string) bool {
125+
// Find if must run in TLS mode
126+
file, err := os.Open(*c)
127+
if err != nil {
128+
log.Fatal().Err(err).Msgf("failed to open %s", *c)
129+
}
130+
131+
scanner := bufio.NewScanner(file)
132+
scanner.Split(bufio.ScanLines)
133+
var text []string
134+
135+
for scanner.Scan() {
136+
text = append(text, scanner.Text())
137+
}
138+
139+
// The method os.File.Close() is called
140+
// on the os.File object to close the file
141+
file.Close()
142+
143+
// and then a loop iterates through
144+
// and prints each of the slice values.
145+
for _, eachLn := range text {
146+
//docker:[email protected]:4500:tls
147+
tls, err = regexp.Match(`[0-9]+:tls`, []byte(eachLn))
148+
}
149+
return tls
150+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/tigrisdata/fdb-exporter
33
go 1.18
44

55
require (
6-
github.com/apple/foundationdb/bindings/go v0.0.0-20220521054011-a88e049b28d8
6+
github.com/apple/foundationdb/bindings/go v0.0.0-20231025163521-0301045e333a
77
github.com/m3db/prometheus_client_golang v1.12.8
88
github.com/rs/zerolog v1.28.0
99
github.com/stretchr/testify v1.8.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
4040
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
4141
github.com/apple/foundationdb/bindings/go v0.0.0-20220521054011-a88e049b28d8 h1:B1KM1sz2bMjLThSQZSg+2kE2OBFMbtGdDcekqj0t2z0=
4242
github.com/apple/foundationdb/bindings/go v0.0.0-20220521054011-a88e049b28d8/go.mod h1:w63jdZTFCtvdjsUj5yrdKgjxaAD5uXQX6hJ7EaiLFRs=
43+
github.com/apple/foundationdb/bindings/go v0.0.0-20231025163521-0301045e333a h1:dBykmcZ1/zh36vj9aPC/6FXS4fx0OoPpAkHuqTwDJyw=
44+
github.com/apple/foundationdb/bindings/go v0.0.0-20231025163521-0301045e333a/go.mod h1:OMVSB21p9+xQUIqlGizHPZfjK+SHws1ht+ZytVDoz9U=
45+
github.com/apple/foundationdb/bindings/go v0.0.0-20250221231555-5140696da2df h1:XlE/l8moueBRTJr7xt0/9f0HJ1FaLupzguIKoj0a74g=
46+
github.com/apple/foundationdb/bindings/go v0.0.0-20250221231555-5140696da2df/go.mod h1:OMVSB21p9+xQUIqlGizHPZfjK+SHws1ht+ZytVDoz9U=
4347
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
4448
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
4549
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

metrics/group_data.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (d *DataMetricGroup) GetMetrics(status *models.FullStatus) {
4949
"total_disk_used_bytes": status.Cluster.Data.TotalDiskUsedBytes,
5050
"total_kv_size_bytes": status.Cluster.Data.TotalKvSizeBytes,
5151
"min_replicas_remaining": status.Cluster.Data.State.MinReplicasRemaining,
52+
"state_health": status.Cluster.Data.State.Healthy,
5253
"missing_data": missingData,
5354
}
5455
SetMultipleGauges(scope, metrics, GetBaseTags())

metrics/group_fault_tolerance.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2022-2023 Tigris Data, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import (
18+
"github.com/rs/zerolog/log"
19+
"github.com/tigrisdata/fdb-exporter/models"
20+
)
21+
22+
type DbClusterFaultTolerance struct {
23+
metricGroup
24+
}
25+
26+
func NewDbClusterFaultTolerance(info *MetricReporter) *DbClusterFaultTolerance {
27+
return &DbClusterFaultTolerance{*newMetricGroup("fault_tolerance", info.GetScopeOrExit("cluster"), info)}
28+
}
29+
30+
func (d *DbClusterFaultTolerance) GetMetrics(status *models.FullStatus) {
31+
scope := d.GetScopeOrExit("default")
32+
if !isValidClusterFaultTolerance(status) {
33+
log.Error().Msg("failed to get database fault tolerance")
34+
return
35+
}
36+
SetGauge(scope, "zone_failures_availability", GetBaseTags(), status.Cluster.FaultTolerance.MaxZoneFailuresWithoutLosingAvailability)
37+
SetGauge(scope, "zone_failures_data", GetBaseTags(), status.Cluster.FaultTolerance.MaxZoneFailuresWithoutLosingData)
38+
}

metrics/group_lockstate.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2022-2023 Tigris Data, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import (
18+
"github.com/rs/zerolog/log"
19+
"github.com/tigrisdata/fdb-exporter/models"
20+
)
21+
22+
type DbClusterLock struct {
23+
metricGroup
24+
}
25+
26+
func NewDbClusterLock(info *MetricReporter) *DbClusterLock {
27+
return &DbClusterLock{*newMetricGroup("lockstate", info.GetScopeOrExit("cluster"), info)}
28+
}
29+
30+
func (d *DbClusterLock) GetMetrics(status *models.FullStatus) {
31+
scope := d.GetScopeOrExit("default")
32+
if !isValidClusterLockState(status) {
33+
log.Error().Msg("failed to get database lock_state")
34+
return
35+
}
36+
SetGauge(scope, "locked", GetBaseTags(), status.Cluster.DatabaseLockState.Locked)
37+
}

metrics/metric_reporter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func NewMetricReporter() *MetricReporter {
7575
m.groups = []Collectable{
7676
NewCoordinatorMetricGroup(&m),
7777
NewDbStatusMetricGroup(&m),
78+
NewDbClusterFaultTolerance(&m),
79+
NewDbClusterLock(&m),
7880
NewWorkloadOperationsMetricGroup(&m),
7981
NewWorkloadTransactionsMetricGroup(&m),
8082
NewWorkloadKeysMetricGroup(&m),

metrics/status.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ package metrics
1616

1717
import "github.com/tigrisdata/fdb-exporter/models"
1818

19+
func isValidClusterLockState(status *models.FullStatus) bool {
20+
if status == nil || status.Cluster == nil || status.Cluster.DatabaseLockState == nil {
21+
return false
22+
}
23+
return true
24+
}
25+
26+
func isValidClusterFaultTolerance(status *models.FullStatus) bool {
27+
if status == nil || status.Cluster == nil || status.Cluster.FaultTolerance == nil {
28+
return false
29+
}
30+
return true
31+
}
32+
1933
func isValidClusterData(status *models.FullStatus) bool {
2034
if status == nil || status.Cluster == nil || status.Cluster.Data == nil {
2135
return false

0 commit comments

Comments
 (0)