Skip to content

Commit 0767980

Browse files
author
Hridoy Roy
authored
Port: Telemetry For Lease Expiration Times (#10375)
* port lease metrics * go mod vendor * caught a bug
1 parent d9fb082 commit 0767980

File tree

11 files changed

+623
-58
lines changed

11 files changed

+623
-58
lines changed

command/server/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func TestLoadConfigFileIntegerAndBooleanValuesJson(t *testing.T) {
3030
testLoadConfigFileIntegerAndBooleanValuesJson(t)
3131
}
3232

33+
func TestLoadConfigFileWithLeaseMetricTelemetry(t *testing.T) {
34+
testLoadConfigFileLeaseMetrics(t)
35+
}
36+
3337
func TestLoadConfigDir(t *testing.T) {
3438
testLoadConfigDir(t)
3539
}

command/server/config_test_helpers.go

Lines changed: 131 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ func testLoadConfigFile_topLevel(t *testing.T, entropy *configutil.Entropy) {
6262
},
6363

6464
Telemetry: &configutil.Telemetry{
65-
StatsdAddr: "bar",
66-
StatsiteAddr: "foo",
67-
DisableHostname: false,
68-
DogStatsDAddr: "127.0.0.1:7254",
69-
DogStatsDTags: []string{"tag_1:val_1", "tag_2:val_2"},
70-
PrometheusRetentionTime: 30 * time.Second,
71-
UsageGaugePeriod: 5 * time.Minute,
72-
MaximumGaugeCardinality: 125,
65+
StatsdAddr: "bar",
66+
StatsiteAddr: "foo",
67+
DisableHostname: false,
68+
DogStatsDAddr: "127.0.0.1:7254",
69+
DogStatsDTags: []string{"tag_1:val_1", "tag_2:val_2"},
70+
PrometheusRetentionTime: 30 * time.Second,
71+
UsageGaugePeriod: 5 * time.Minute,
72+
MaximumGaugeCardinality: 125,
73+
LeaseMetricsEpsilon: time.Hour,
74+
NumLeaseMetricsTimeBuckets: 168,
75+
LeaseMetricsNameSpaceLabels: false,
7376
},
7477

7578
DisableMlock: true,
@@ -192,6 +195,9 @@ func testLoadConfigFile_json2(t *testing.T, entropy *configutil.Entropy) {
192195
CirconusBrokerID: "0",
193196
CirconusBrokerSelectTag: "dc:sfo",
194197
PrometheusRetentionTime: 30 * time.Second,
198+
LeaseMetricsEpsilon: time.Hour,
199+
NumLeaseMetricsTimeBuckets: 168,
200+
LeaseMetricsNameSpaceLabels: false,
195201
},
196202
},
197203

@@ -371,15 +377,18 @@ func testLoadConfigFile(t *testing.T) {
371377
},
372378

373379
Telemetry: &configutil.Telemetry{
374-
StatsdAddr: "bar",
375-
StatsiteAddr: "foo",
376-
DisableHostname: false,
377-
UsageGaugePeriod: 5 * time.Minute,
378-
MaximumGaugeCardinality: 100,
379-
DogStatsDAddr: "127.0.0.1:7254",
380-
DogStatsDTags: []string{"tag_1:val_1", "tag_2:val_2"},
381-
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
382-
MetricsPrefix: "myprefix",
380+
StatsdAddr: "bar",
381+
StatsiteAddr: "foo",
382+
DisableHostname: false,
383+
UsageGaugePeriod: 5 * time.Minute,
384+
MaximumGaugeCardinality: 100,
385+
DogStatsDAddr: "127.0.0.1:7254",
386+
DogStatsDTags: []string{"tag_1:val_1", "tag_2:val_2"},
387+
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
388+
MetricsPrefix: "myprefix",
389+
LeaseMetricsEpsilon: time.Hour,
390+
NumLeaseMetricsTimeBuckets: 168,
391+
LeaseMetricsNameSpaceLabels: false,
383392
},
384393

385394
DisableMlock: true,
@@ -477,6 +486,9 @@ func testLoadConfigFile_json(t *testing.T) {
477486
CirconusBrokerID: "",
478487
CirconusBrokerSelectTag: "",
479488
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
489+
LeaseMetricsEpsilon: time.Hour,
490+
NumLeaseMetricsTimeBuckets: 168,
491+
LeaseMetricsNameSpaceLabels: false,
480492
},
481493

482494
PidFile: "./pidfile",
@@ -540,12 +552,15 @@ func testLoadConfigDir(t *testing.T) {
540552
},
541553

542554
Telemetry: &configutil.Telemetry{
543-
StatsiteAddr: "qux",
544-
StatsdAddr: "baz",
545-
DisableHostname: true,
546-
UsageGaugePeriod: 5 * time.Minute,
547-
MaximumGaugeCardinality: 100,
548-
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
555+
StatsiteAddr: "qux",
556+
StatsdAddr: "baz",
557+
DisableHostname: true,
558+
UsageGaugePeriod: 5 * time.Minute,
559+
MaximumGaugeCardinality: 100,
560+
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
561+
LeaseMetricsEpsilon: time.Hour,
562+
NumLeaseMetricsTimeBuckets: 168,
563+
LeaseMetricsNameSpaceLabels: false,
549564
},
550565
ClusterName: "testcluster",
551566
},
@@ -668,6 +683,9 @@ func testConfig_Sanitized(t *testing.T) {
668683
"stackdriver_debug_logs": false,
669684
"statsd_address": "bar",
670685
"statsite_address": "",
686+
"lease_metrics_epsilon": time.Hour,
687+
"num_lease_metrics_buckets": 168,
688+
"add_lease_metrics_namespace_labels": false,
671689
},
672690
}
673691

@@ -787,3 +805,93 @@ func testParseSeals(t *testing.T) {
787805
}
788806
require.Equal(t, config, expected)
789807
}
808+
809+
func testLoadConfigFileLeaseMetrics(t *testing.T) {
810+
config, err := LoadConfigFile("./test-fixtures/config5.hcl")
811+
if err != nil {
812+
t.Fatalf("err: %s", err)
813+
}
814+
815+
expected := &Config{
816+
SharedConfig: &configutil.SharedConfig{
817+
Listeners: []*configutil.Listener{
818+
{
819+
Type: "tcp",
820+
Address: "127.0.0.1:443",
821+
},
822+
},
823+
824+
Telemetry: &configutil.Telemetry{
825+
StatsdAddr: "bar",
826+
StatsiteAddr: "foo",
827+
DisableHostname: false,
828+
UsageGaugePeriod: 5 * time.Minute,
829+
MaximumGaugeCardinality: 100,
830+
DogStatsDAddr: "127.0.0.1:7254",
831+
DogStatsDTags: []string{"tag_1:val_1", "tag_2:val_2"},
832+
PrometheusRetentionTime: configutil.PrometheusDefaultRetentionTime,
833+
MetricsPrefix: "myprefix",
834+
LeaseMetricsEpsilon: time.Hour,
835+
NumLeaseMetricsTimeBuckets: 2,
836+
LeaseMetricsNameSpaceLabels: true,
837+
},
838+
839+
DisableMlock: true,
840+
841+
Entropy: nil,
842+
843+
PidFile: "./pidfile",
844+
845+
ClusterName: "testcluster",
846+
},
847+
848+
Storage: &Storage{
849+
Type: "consul",
850+
RedirectAddr: "foo",
851+
Config: map[string]string{
852+
"foo": "bar",
853+
},
854+
},
855+
856+
HAStorage: &Storage{
857+
Type: "consul",
858+
RedirectAddr: "snafu",
859+
Config: map[string]string{
860+
"bar": "baz",
861+
},
862+
DisableClustering: true,
863+
},
864+
865+
ServiceRegistration: &ServiceRegistration{
866+
Type: "consul",
867+
Config: map[string]string{
868+
"foo": "bar",
869+
},
870+
},
871+
872+
DisableCache: true,
873+
DisableCacheRaw: true,
874+
DisablePrintableCheckRaw: true,
875+
DisablePrintableCheck: true,
876+
EnableUI: true,
877+
EnableUIRaw: true,
878+
879+
EnableRawEndpoint: true,
880+
EnableRawEndpointRaw: true,
881+
882+
DisableSealWrap: true,
883+
DisableSealWrapRaw: true,
884+
885+
MaxLeaseTTL: 10 * time.Hour,
886+
MaxLeaseTTLRaw: "10h",
887+
DefaultLeaseTTL: 10 * time.Hour,
888+
DefaultLeaseTTLRaw: "10h",
889+
}
890+
891+
addExpectedEntConfig(expected, []string{})
892+
893+
config.Listeners[0].RawConfig = nil
894+
if diff := deep.Equal(config, expected); diff != nil {
895+
t.Fatal(diff)
896+
}
897+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
disable_cache = true
2+
disable_mlock = true
3+
4+
ui = true
5+
6+
listener "tcp" {
7+
address = "127.0.0.1:443"
8+
allow_stuff = true
9+
}
10+
11+
backend "consul" {
12+
foo = "bar"
13+
advertise_addr = "foo"
14+
}
15+
16+
ha_backend "consul" {
17+
bar = "baz"
18+
advertise_addr = "snafu"
19+
disable_clustering = "true"
20+
}
21+
22+
service_registration "consul" {
23+
foo = "bar"
24+
}
25+
26+
telemetry {
27+
statsd_address = "bar"
28+
usage_gauge_period = "5m"
29+
maximum_gauge_cardinality = 100
30+
31+
statsite_address = "foo"
32+
dogstatsd_addr = "127.0.0.1:7254"
33+
dogstatsd_tags = ["tag_1:val_1", "tag_2:val_2"]
34+
metrics_prefix = "myprefix"
35+
36+
lease_metrics_epsilon = "1h"
37+
num_lease_metrics_buckets = 2
38+
add_lease_metrics_namespace_labels = true
39+
}
40+
41+
sentinel {
42+
additional_enabled_modules = []
43+
}
44+
45+
max_lease_ttl = "10h"
46+
default_lease_ttl = "10h"
47+
cluster_name = "testcluster"
48+
pid_file = "./pidfile"
49+
raw_storage_endpoint = true
50+
disable_sealwrap = true
51+
disable_printable_check = true

helper/metricsutil/bucket.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ func TTLBucket(ttl time.Duration) string {
3737
}
3838

3939
}
40+
41+
func ExpiryBucket(expiryTime time.Time, leaseEpsilon time.Duration, rollingWindow time.Time, labelNS string, useNS bool) *LeaseExpiryLabel {
42+
if !useNS {
43+
labelNS = ""
44+
}
45+
leaseExpiryLabel := LeaseExpiryLabel{LabelNS: labelNS}
46+
47+
// calculate rolling window
48+
if expiryTime.Before(rollingWindow) {
49+
leaseExpiryLabel.LabelName = expiryTime.Round(leaseEpsilon).String()
50+
return &leaseExpiryLabel
51+
}
52+
return nil
53+
}
54+
55+
type LeaseExpiryLabel = struct {
56+
LabelName string
57+
LabelNS string
58+
}

helper/metricsutil/wrapped_metrics.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ type ClusterMetricSink struct {
2828

2929
// Sink is the go-metrics instance to send to.
3030
Sink metrics.MetricSink
31+
32+
// Constants that are helpful for metrics within the metrics sink
33+
TelemetryConsts TelemetryConstConfig
34+
}
35+
36+
type TelemetryConstConfig struct {
37+
LeaseMetricsEpsilon time.Duration
38+
NumLeaseMetricsTimeBuckets int
39+
LeaseMetricsNameSpaceLabels bool
3140
}
3241

3342
type Metrics interface {
@@ -83,8 +92,9 @@ func BlackholeSink() *ClusterMetricSink {
8392

8493
func NewClusterMetricSink(clusterName string, sink metrics.MetricSink) *ClusterMetricSink {
8594
cms := &ClusterMetricSink{
86-
ClusterName: atomic.Value{},
87-
Sink: sink,
95+
ClusterName: atomic.Value{},
96+
Sink: sink,
97+
TelemetryConsts: TelemetryConstConfig{},
8898
}
8999
cms.ClusterName.Store(clusterName)
90100
return cms

internalshared/configutil/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ func (c *SharedConfig) Sanitized() map[string]interface{} {
214214
"stackdriver_location": c.Telemetry.StackdriverLocation,
215215
"stackdriver_namespace": c.Telemetry.StackdriverNamespace,
216216
"stackdriver_debug_logs": c.Telemetry.StackdriverDebugLogs,
217+
"lease_metrics_epsilon": c.Telemetry.LeaseMetricsEpsilon,
218+
"num_lease_metrics_buckets": c.Telemetry.NumLeaseMetricsTimeBuckets,
219+
"add_lease_metrics_namespace_labels": c.Telemetry.LeaseMetricsNameSpaceLabels,
217220
}
218221
result["telemetry"] = sanitizedTelemetry
219222
}

0 commit comments

Comments
 (0)