Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING CHANGE] Enable "new-metrics" by default #1148

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/monitoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Many metrics are provided by the Collector for its monitoring. Below some
key recommendations for alerting and monitoring are listed. All metrics
referenced below are using the `--new-metrics` option, eventually the Collector
will transition to these metrics as default and disable the old ones.
referenced below are using the `--new-metrics` option that is enabled by
default.

## Critical Monitoring

Expand Down
6 changes: 3 additions & 3 deletions internal/collector/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ func Flags(flags *flag.FlagSet) {

useLegacyMetricsPtr = flags.Bool(
"legacy-metrics",
true,
false,
"Flag to control usage of legacy metrics",
)

useNewMetricsPtr = flags.Bool(
"new-metrics",
false,
true,
"Flag to control usage of new metrics",
)

addInstanceIDPtr = flags.Bool(
"add-instance-id",
false,
true,
"Flag to control the addition of 'service.instance.id' to the collector metrics.")
}

Expand Down
47 changes: 30 additions & 17 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"sort"
"strconv"
"strings"
"testing"

"github.com/prometheus/common/expfmt"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -53,7 +53,6 @@ func TestApplication_Start(t *testing.T) {
"--config=testdata/otelcol-config.yaml",
"--metrics-addr=localhost:" + strconv.FormatUint(uint64(metricsPort), 10),
"--metrics-prefix=" + testPrefix,
"--add-instance-id=true",
})

appDone := make(chan struct{})
Expand All @@ -67,7 +66,16 @@ func TestApplication_Start(t *testing.T) {
require.True(t, isAppAvailable(t, "http://localhost:13133"))
assert.Equal(t, app.logger, app.GetLogger())

assertMetricsPrefix(t, testPrefix, metricsPort)
// All labels added to all collector metrics by default are listed below.
// These labels are hard coded here in order to avoid inadvertent changes:
// at this point changing labels should be treated as a breaking changing
// and requires a good justification. The reason is that changes to metric
// names or labels can break alerting, dashboards, etc that are used to
// monitor the Collector in production deployments.
mandatoryLabels := []string{
"service_instance_id",
}
assertMetrics(t, testPrefix, metricsPort, mandatoryLabels)

close(app.stopTestChan)
<-appDone
Expand Down Expand Up @@ -127,33 +135,38 @@ func isAppAvailable(t *testing.T, healthCheckEndPoint string) bool {
return resp.StatusCode == http.StatusOK
}

func assertMetricsPrefix(t *testing.T, prefix string, metricsPort uint16) {
func assertMetrics(t *testing.T, prefix string, metricsPort uint16, mandatoryLabels []string) {
client := &http.Client{}
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/metrics", metricsPort))
require.NoError(t, err)

defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)

for {
s, err := reader.ReadString('\n')
if err == io.EOF {
break
}

require.NoError(t, err)
if len(s) == 0 || s[0] == '#' {
// Skip this line since it is not a metric.
continue
}
var parser expfmt.TextParser
parsed, err := parser.TextToMetricFamilies(reader)
require.NoError(t, err)

for metricName, metricFamily := range parsed {
// require is used here so test fails with a single message.
require.True(
t,
strings.HasPrefix(s, prefix),
strings.HasPrefix(metricName, prefix),
"expected prefix %q but string starts with %q",
prefix,
s[:len(prefix)+1]+"...")
metricName[:len(prefix)+1]+"...")

for _, metric := range metricFamily.Metric {
var labelNames []string
for _, labelPair := range metric.Label {
labelNames = append(labelNames, *labelPair.Name)
}

for _, mandatoryLabel := range mandatoryLabels {
// require is used here so test fails with a single message.
require.Contains(t, labelNames, mandatoryLabel, "mandatory label %q not present", mandatoryLabel)
}
}
}
}

Expand Down