Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions flytestdlib/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/flyteorg/flyte/v2/flytestdlib/config"
"github.com/flyteorg/flyte/v2/flytestdlib/config/viper"
"github.com/flyteorg/flyte/v2/flytestdlib/logger"
"github.com/flyteorg/flyte/v2/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// App is the shared entry-point skeleton for Flyte services.
Expand Down Expand Up @@ -81,6 +83,11 @@ func (a *App) serve(ctx context.Context) error {
Mux: http.NewServeMux(),
}

if sc.Scope == nil {
sc.Scope = promutils.NewScope(a.Name)
}

sc.Mux.Handle("/metrics", promhttp.Handler())
// 3. Let the caller populate resources & register handlers
if err := a.Setup(ctx, sc); err != nil {
return fmt.Errorf("setup failed: %w", err)
Expand Down
48 changes: 48 additions & 0 deletions flytestdlib/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app

import (
"context"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestApp_MetricsEndpointAndScope(t *testing.T) {
var capturedScope interface{}
setupDone := make(chan struct{})

testApp := &App{
Name: "test-metrics-app",
Short: "Testing the metrics framework plumbing",
Setup: func(ctx context.Context, sc *SetupContext) error {
sc.Port = 8099
capturedScope = sc.Scope
close(setupDone)
return nil
},
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
_ = testApp.serve(ctx)
}()

select {
case <-setupDone:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for app setup")
}

assert.NotNil(t, capturedScope, "SetupContext.Scope should be initialized by the framework before Setup is called")

resp, err := http.Get("http://localhost:8099/metrics")
require.NoError(t, err)
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode, "Expected /metrics to return a 200 OK status")
}
Loading