@@ -42,6 +42,11 @@ const (
42
42
gofrTraceExporter = "gofr"
43
43
gofrTracerURL = "https://tracer.gofr.dev"
44
44
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"
45
50
)
46
51
47
52
// App is the main application in the GoFr framework.
@@ -173,9 +178,17 @@ func (a *App) Run() {
173
178
shutdownCtx , done := context .WithTimeout (context .WithoutCancel (ctx ), shutDownTimeout )
174
179
defer done ()
175
180
181
+ if a .hasTelemetry () {
182
+ a .sendTelemetry (http .DefaultClient , false )
183
+ }
184
+
176
185
_ = a .Shutdown (shutdownCtx )
177
186
}()
178
187
188
+ if a .hasTelemetry () {
189
+ go a .sendTelemetry (http .DefaultClient , true )
190
+ }
191
+
179
192
wg := sync.WaitGroup {}
180
193
181
194
// Start Metrics Server
@@ -224,6 +237,37 @@ func (a *App) Run() {
224
237
wg .Wait ()
225
238
}
226
239
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
+
227
271
// Shutdown stops the service(s) and close the application.
228
272
// It shuts down the HTTP, gRPC, Metrics servers and closes the container's active connections to datasources.
229
273
func (a * App ) Shutdown (ctx context.Context ) error {
0 commit comments