Skip to content

Commit d6c7f5f

Browse files
Storing the number of running GoFr servers (#1474)
1 parent 4fef87a commit d6c7f5f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Diff for: pkg/gofr/gofr.go

+44
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const (
4242
gofrTraceExporter = "gofr"
4343
gofrTracerURL = "https://tracer.gofr.dev"
4444
checkPortTimeout = 2 * time.Second
45+
gofrHost = "https://gofr.dev"
46+
startServerPing = "/api/ping/up"
47+
shutServerPing = "/api/ping/down"
48+
pingTimeout = 5 * time.Second
49+
defaultTelemetry = "true"
4550
)
4651

4752
// App is the main application in the GoFr framework.
@@ -173,9 +178,17 @@ func (a *App) Run() {
173178
shutdownCtx, done := context.WithTimeout(context.WithoutCancel(ctx), shutDownTimeout)
174179
defer done()
175180

181+
if a.hasTelemetry() {
182+
a.sendTelemetry(http.DefaultClient, false)
183+
}
184+
176185
_ = a.Shutdown(shutdownCtx)
177186
}()
178187

188+
if a.hasTelemetry() {
189+
go a.sendTelemetry(http.DefaultClient, true)
190+
}
191+
179192
wg := sync.WaitGroup{}
180193

181194
// Start Metrics Server
@@ -224,6 +237,37 @@ func (a *App) Run() {
224237
wg.Wait()
225238
}
226239

240+
func (a *App) hasTelemetry() bool {
241+
return a.Config.GetOrDefault("GOFR_TELEMETRY", defaultTelemetry) == "true"
242+
}
243+
244+
func (a *App) sendTelemetry(client *http.Client, isStart bool) {
245+
url := fmt.Sprint(gofrHost, shutServerPing)
246+
247+
if isStart {
248+
url = fmt.Sprint(gofrHost, startServerPing)
249+
250+
a.container.Info("GoFr records the number of active servers. Set GOFR_TELEMETRY=false in configs to disable it.")
251+
}
252+
253+
ctx, cancel := context.WithTimeout(context.Background(), pingTimeout)
254+
defer cancel()
255+
256+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, http.NoBody)
257+
if err != nil {
258+
return
259+
}
260+
261+
req.Header.Set("Connection", "close")
262+
263+
resp, err := client.Do(req)
264+
if err != nil {
265+
return
266+
}
267+
268+
resp.Body.Close()
269+
}
270+
227271
// Shutdown stops the service(s) and close the application.
228272
// It shuts down the HTTP, gRPC, Metrics servers and closes the container's active connections to datasources.
229273
func (a *App) Shutdown(ctx context.Context) error {

Diff for: pkg/gofr/gofr_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,54 @@ func TestGoFr_isPortAvailable(t *testing.T) {
7171
}
7272
}
7373

74+
// mockRoundTripper is a mock implementation of http.RoundTripper.
75+
type mockRoundTripper struct {
76+
lastRequest *http.Request // Store the last request for assertions
77+
mockResponse *http.Response
78+
mockError error
79+
}
80+
81+
// RoundTrip mocks the HTTP request and stores the request for verification.
82+
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
83+
m.lastRequest = req // Store the request for assertions
84+
return m.mockResponse, m.mockError
85+
}
86+
87+
func TestPingGoFr(t *testing.T) {
88+
tests := []struct {
89+
name string
90+
input bool
91+
expectedURL string
92+
}{
93+
{"Ping Start Server", true, gofrHost + startServerPing},
94+
{"Ping Shut Server", false, gofrHost + shutServerPing},
95+
}
96+
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
mockTransport := &mockRoundTripper{
100+
mockResponse: &http.Response{
101+
StatusCode: http.StatusOK,
102+
Body: http.NoBody,
103+
},
104+
mockError: nil,
105+
}
106+
107+
mockClient := &http.Client{Transport: mockTransport}
108+
109+
_ = testutil.NewServerConfigs(t)
110+
111+
a := New()
112+
113+
a.sendTelemetry(mockClient, tt.input)
114+
115+
assert.NotNil(t, mockTransport.lastRequest, "Request should not be nil")
116+
assert.Equal(t, tt.expectedURL, mockTransport.lastRequest.URL.String(), "Unexpected request URL")
117+
assert.Equal(t, http.MethodPost, mockTransport.lastRequest.Method, "Unexpected HTTP method")
118+
})
119+
}
120+
}
121+
74122
func TestGofr_ServerRoutes(t *testing.T) {
75123
_ = testutil.NewServerConfigs(t)
76124

0 commit comments

Comments
 (0)