Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Allow views on measurements without required tags to succeed #988

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions exporter/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,78 @@ tests_foo{method="issue961",service="spanner"} 1
t.Fatal("output differed from expected")
}
}

func TestViewMeasureWithoutTag(t *testing.T) {
exporter, err := NewExporter(Options{})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
view.RegisterExporter(exporter)
defer view.UnregisterExporter(exporter)

m := stats.Int64("tests/foo", "foo", stats.UnitDimensionless)
k, _ := tag.NewKey("key")

v := &view.View{
Name: m.Name(),
Description: m.Description(),
TagKeys: []tag.Key{k}, // Ensure view has a tag
Measure: m,
Aggregation: view.Count(),
}

if err := view.Register(v); err != nil {
t.Fatalf("failed to create views: %v", err)
}
defer view.Unregister(v)

view.SetReportingPeriod(time.Millisecond)

// Make a measure without the tag in the view.
stats.Record(context.Background(), m.M(1))

srv := httptest.NewServer(exporter)
defer srv.Close()

var i int
var output string
for {
time.Sleep(10 * time.Millisecond)
if i == 1000 {
t.Fatal("no output at /metrics (10s wait)")
}
i++

resp, err := http.Get(srv.URL)
if err != nil {
t.Fatalf("failed to get /metrics: %v", err)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
resp.Body.Close()

output = string(body)
if output != "" {
break
}
}

if strings.Contains(output, "collected before with the same name and label values") {
t.Fatal("metric name and labels being duplicated but must be unique")
}

if strings.Contains(output, "error(s) occurred") {
t.Fatal("error reported by prometheus registry")
}

want := `# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{key=""} 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming that we would just create an empty value label for missing tags.

`
if output != want {
t.Fatal("output differed from expected")
}
}