This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
ability to share default registry #9
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"go.opencensus.io/tag" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type mSlice []*stats.Int64Measure | ||
|
@@ -448,3 +449,88 @@ tests_foo{key_1="issue659",key_2="",key_3="issue659",key_4="",key_5="issue659"} | |
t.Fatalf("output differed from expected output: %s want: %s", output, want) | ||
} | ||
} | ||
|
||
func TestShareDefaultRegistry(t *testing.T) { | ||
_, err := NewExporter(Options{ | ||
Registerer: prometheus.DefaultRegisterer, | ||
Gatherer: prometheus.DefaultGatherer, | ||
}) | ||
if err != nil { | ||
t.Fatalf("failed to create prometheus exporter: %v", err) | ||
} | ||
m := stats.Int64("tests/foo", "foo", stats.UnitDimensionless) | ||
v := &view.View{ | ||
Name: m.Name(), | ||
Description: m.Description(), | ||
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) | ||
|
||
stats.Record(context.Background(), m.M(1)) | ||
|
||
// counter, prometheus way | ||
c := prometheus.NewCounter(prometheus.CounterOpts{Name: "prom_counter", Help: "Prometheus Counter"}) | ||
prometheus.MustRegister(c) | ||
|
||
c.Add(1) | ||
|
||
// Use prometheus handler | ||
srv := httptest.NewServer(promhttp.Handler()) | ||
defer srv.Close() | ||
|
||
var i int | ||
var output string | ||
for { | ||
time.Sleep(10 * time.Millisecond) | ||
if i == 1000 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: may be 10 is sufficient to declare it failed. Oc counter should be available right away. Not sure about Prometheus counter though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your review. All updates are done, please take a look. |
||
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") | ||
} | ||
|
||
wantOc := `# HELP tests_foo foo | ||
# TYPE tests_foo counter | ||
tests_foo 1 | ||
` | ||
if !strings.Contains(output, wantOc) { | ||
t.Errorf("output does not contain opencensus counter. Output: %s want: %s", output, wantOc) | ||
} | ||
|
||
wantP := `# HELP prom_counter Prometheus Counter | ||
# TYPE prom_counter counter | ||
prom_counter 1 | ||
` | ||
if !strings.Contains(output, wantP) { | ||
t.Errorf("output does not contain opencensus counter. Output: %s want: %s", output, wantP) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since prometheus exporter is using new metricexport.Reader interface it simply reads when request is made to /metrics endpoint. So setting reporting period is not necessary. This was required before when Oc library used to push metrics to prometheus exporter.