MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestMetricsHandler

Function TestMetricsHandler

rest/api_test.go:2740–2797  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2738}
2739
2740func TestMetricsHandler(t *testing.T) {
2741 base.RequireNumTestBuckets(t, 2)
2742
2743 base.SkipPrometheusStatsRegistration = false
2744 defer func() {
2745 base.SkipPrometheusStatsRegistration = true
2746 }()
2747
2748 // Create and remove a databaseion
2749 // This ensures that creation and removal of a DB is possible without a re-registration issue ( the below rest tester will re-register "db")
2750 ctx := base.TestCtx(t)
2751 tBucket := base.GetTestBucket(t)
2752 context, err := db.NewDatabaseContext(ctx, "db", tBucket, false, db.DatabaseContextOptions{Scopes: db.GetScopesOptions(t, tBucket, 1)})
2753 require.NoError(t, err)
2754 context.Close(context.AddDatabaseLogContext(ctx))
2755
2756 rt := NewRestTester(t, nil)
2757 defer rt.Close()
2758
2759 rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc", "{}")
2760
2761 srv := httptest.NewServer(rt.TestMetricsHandler())
2762 defer srv.Close()
2763
2764 // Ensure metrics endpoint is accessible and that db database has entries
2765 resp, err := http.Get(srv.URL + "/_metrics")
2766 require.NoError(t, err)
2767 assert.Equal(t, http.StatusOK, resp.StatusCode)
2768 bodyString, err := io.ReadAll(resp.Body)
2769 assert.NoError(t, err)
2770 assert.Contains(t, string(bodyString), `database="db"`)
2771 err = resp.Body.Close()
2772 assert.NoError(t, err)
2773
2774 // Initialize another database to ensure both are registered successfully
2775 tBucket2 := base.GetTestBucket(t)
2776 context, err = db.NewDatabaseContext(ctx, "db2", tBucket2, false, db.DatabaseContextOptions{Scopes: db.GetScopesOptions(t, tBucket2, 1)})
2777 require.NoError(t, err)
2778 defer context.Close(context.AddDatabaseLogContext(ctx))
2779
2780 // Validate that metrics still works with both db and db2 databases and that they have entries
2781 resp, err = http.Get(srv.URL + "/_metrics")
2782 require.NoError(t, err)
2783 assert.Equal(t, http.StatusOK, resp.StatusCode)
2784 bodyString, err = io.ReadAll(resp.Body)
2785 assert.NoError(t, err)
2786 assert.Contains(t, string(bodyString), `database="db"`)
2787 assert.Contains(t, string(bodyString), `database="db2"`)
2788 err = resp.Body.Close()
2789 assert.NoError(t, err)
2790
2791 // Ensure metrics endpoint is not serving any other routes
2792 resp, err = http.Get(srv.URL + "/" + rt.GetSingleKeyspace() + "/")
2793 require.NoError(t, err)
2794 assert.Equal(t, http.StatusNotFound, resp.StatusCode)
2795 err = resp.Body.Close()
2796 assert.NoError(t, err)
2797}

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
AddDatabaseLogContextMethod · 0.95
CloseMethod · 0.95
SendAdminRequestMethod · 0.95
TestMetricsHandlerMethod · 0.95
GetSingleKeyspaceMethod · 0.95
RequireNumTestBucketsFunction · 0.92
TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
NewDatabaseContextFunction · 0.92
GetScopesOptionsFunction · 0.92
NewRestTesterFunction · 0.85

Tested by

no test coverage detected