Skip to content

Commit

Permalink
[manual backport] 1.15.x - emit consul version periodically (#18729)
Browse files Browse the repository at this point in the history
fix: emit consul version metric on a regular interval (#18724)
  • Loading branch information
analogue authored Sep 8, 2023
1 parent 174b3d1 commit 5680edd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/18724.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
telemetry: emit consul version metric on a regular interval.
```
6 changes: 0 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,6 @@ func (a *Agent) Start(ctx context.Context) error {
go m.Monitor(&lib.StopChannelContext{StopCh: a.shutdownCh})
}

// consul version metric with labels
metrics.SetGaugeWithLabels([]string{"version"}, 1, []metrics.Label{
{Name: "version", Value: a.config.VersionWithMetadata()},
{Name: "pre_release", Value: a.config.VersionPrerelease},
})

// start a go routine to reload config based on file watcher events
if a.configFileWatcher != nil {
a.baseDeps.Logger.Debug("starting file watcher")
Expand Down
29 changes: 29 additions & 0 deletions agent/consul/usagemetrics/usagemetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/hashicorp/consul/agent/consul/state"
"github.com/hashicorp/consul/logging"
"github.com/hashicorp/consul/version"
)

var Gauges = []prometheus.GaugeDefinition{
Expand Down Expand Up @@ -92,6 +93,10 @@ var Gauges = []prometheus.GaugeDefinition{
Name: []string{"state", "billable_service_instances"},
Help: "Total number of billable service instances in the local datacenter.",
},
{
Name: []string{"version"},
Help: "Represents the Consul version.",
},
}

type getMembersFunc func() []serf.Member
Expand Down Expand Up @@ -241,6 +246,7 @@ func (u *UsageMetricsReporter) runOnce() {
}

u.emitConfigEntryUsage(configUsage)
u.emitVersion()
}

func (u *UsageMetricsReporter) memberUsage() []serf.Member {
Expand All @@ -263,3 +269,26 @@ func (u *UsageMetricsReporter) memberUsage() []serf.Member {

return out
}

func (u *UsageMetricsReporter) emitVersion() {
// consul version metric with labels
metrics.SetGaugeWithLabels(
[]string{"version"},
1,
[]metrics.Label{
{Name: "version", Value: versionWithMetadata()},
{Name: "pre_release", Value: version.VersionPrerelease},
},
)
}

func versionWithMetadata() string {
vsn := version.Version
metadata := version.VersionMetadata

if metadata != "" {
vsn += "+" + metadata
}

return vsn
}
20 changes: 20 additions & 0 deletions agent/consul/usagemetrics/usagemetrics_ce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package usagemetrics

import (
"fmt"
"testing"
"time"

Expand All @@ -18,6 +19,7 @@ import (
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/pbpeering"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/consul/version"
)

func newStateStore() (*state.Store, error) {
Expand Down Expand Up @@ -453,6 +455,15 @@ var baseCases = map[string]testCase{
{Name: "kind", Value: "tcp-route"},
},
},
// --- version ---
fmt.Sprintf("consul.usage.test.version;version=%s;pre_release=%s", versionWithMetadata(), version.VersionPrerelease): {
Name: "consul.usage.test.version",
Value: 1,
Labels: []metrics.Label{
{Name: "version", Value: versionWithMetadata()},
{Name: "pre_release", Value: version.VersionPrerelease},
},
},
},
getMembersFunc: func() []serf.Member { return []serf.Member{} },
},
Expand Down Expand Up @@ -896,6 +907,15 @@ var baseCases = map[string]testCase{
{Name: "kind", Value: "tcp-route"},
},
},
// --- version ---
fmt.Sprintf("consul.usage.test.version;version=%s;pre_release=%s", versionWithMetadata(), version.VersionPrerelease): {
Name: "consul.usage.test.version",
Value: 1,
Labels: []metrics.Label{
{Name: "version", Value: versionWithMetadata()},
{Name: "pre_release", Value: version.VersionPrerelease},
},
},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion agent/consul/usagemetrics/usagemetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func assertEqualGaugeMaps(t *testing.T, expectedMap, foundMap map[string]metrics

for key := range foundMap {
if _, ok := expectedMap[key]; !ok {
t.Errorf("found unexpected gauge key: %s", key)
t.Errorf("found unexpected gauge key: %s with value: %v", key, foundMap[key])
}
}

Expand Down

0 comments on commit 5680edd

Please sign in to comment.