@@ -9,23 +9,31 @@ import (
99	"net/http" 
1010	"time" 
1111
12+ 	"go.opentelemetry.io/otel/attribute" 
13+ 	"go.opentelemetry.io/otel/metric" 
14+ 	"go.opentelemetry.io/otel/trace" 
15+ 	"go.uber.org/zap" 
16+ 
1217	"github.com/e2b-dev/infra/packages/shared/pkg/consts" 
18+ 	"github.com/e2b-dev/infra/packages/shared/pkg/logger" 
1319)
1420
1521const  (
16- 	requestTimeout  =  50  *  time .Millisecond 
17- 	loopDelay       =  5  *  time .Millisecond 
22+ 	loopDelay  =  5  *  time .Millisecond 
1823)
1924
2025// doRequestWithInfiniteRetries does a request with infinite retries until the context is done. 
2126// The parent context should have a deadline or a timeout. 
22- func  doRequestWithInfiniteRetries (ctx  context.Context , method , address  string , requestBody  []byte , accessToken  * string ) (* http.Response , error ) {
27+ func  doRequestWithInfiniteRetries (ctx  context.Context , method , address  string , requestBody  []byte , accessToken  * string , envdInitRequestTimeout  time.Duration , sandboxID  string ) (* http.Response , int64 , error ) {
28+ 	count  :=  int64 (0 )
2329	for  {
24- 		reqCtx , cancel  :=  context .WithTimeout (ctx , requestTimeout )
30+ 		count ++ 
31+ 		start  :=  time .Now ()
32+ 		reqCtx , cancel  :=  context .WithTimeout (ctx , envdInitRequestTimeout )
2533		request , err  :=  http .NewRequestWithContext (reqCtx , method , address , bytes .NewReader (requestBody ))
2634		if  err  !=  nil  {
2735			cancel ()
28- 			return  nil , err 
36+ 			return  nil , count ,  err 
2937		}
3038
3139		// make sure request to already authorized envd will not fail 
@@ -38,12 +46,13 @@ func doRequestWithInfiniteRetries(ctx context.Context, method, address string, r
3846		cancel ()
3947
4048		if  err  ==  nil  {
41- 			return  response , nil 
49+ 			return  response , count ,  nil 
4250		}
4351
52+ 		zap .L ().Error ("Error requesting infinite retries" , zap .Error (err ), logger .WithSandboxID (sandboxID ), zap .Int64 ("elapsed" , time .Since (start ).Milliseconds ()))
4453		select  {
4554		case  <- ctx .Done ():
46- 			return  nil , fmt .Errorf ("%w with cause: %w" , ctx .Err (), context .Cause (ctx ))
55+ 			return  nil , count ,  fmt .Errorf ("%w with cause: %w" , ctx .Err (), context .Cause (ctx ))
4756		case  <- time .After (loopDelay ):
4857		}
4958	}
@@ -56,10 +65,12 @@ type PostInitJSONBody struct {
5665	Timestamp    * time.Time          `json:"timestamp,omitempty"` 
5766}
5867
59- func  (s  * Sandbox ) initEnvd (ctx  context.Context , envVars  map [string ]string , accessToken  * string ) error  {
60- 	childCtx , childSpan  :=  tracer .Start (ctx , "envd-init" )
68+ func  (s  * Sandbox ) initEnvd (ctx  context.Context , envVars  map [string ]string , accessToken  * string ,  envdInitRequestTimeout  time. Duration ) error  {
69+ 	childCtx , childSpan  :=  tracer .Start (ctx , "envd-init" ,  trace . WithAttributes ( attribute . String ( "evd_version" ,  s . Config . Envd . Version )) )
6170	defer  childSpan .End ()
6271
72+ 	attributes  :=  metric .WithAttributes (attribute .String ("envd_version" , s .Config .Envd .Version ), attribute .Int64 ("timeout_ms" , envdInitRequestTimeout .Milliseconds ()))
73+ 
6374	hyperloopIP  :=  s .Slot .HyperloopIPString ()
6475	address  :=  fmt .Sprintf ("http://%s:%d/init" , s .Slot .HostIPString (), consts .DefaultEnvdServerPort )
6576	now  :=  time .Now ()
@@ -75,7 +86,8 @@ func (s *Sandbox) initEnvd(ctx context.Context, envVars map[string]string, acces
7586		return  err 
7687	}
7788
78- 	response , err  :=  doRequestWithInfiniteRetries (childCtx , "POST" , address , body , accessToken )
89+ 	response , count , err  :=  doRequestWithInfiniteRetries (childCtx , "POST" , address , body , accessToken , envdInitRequestTimeout , s .Runtime .SandboxID )
90+ 	envdInitAttempts .Add (ctx , count , attributes )
7991	if  err  !=  nil  {
8092		return  fmt .Errorf ("failed to init envd: %w" , err )
8193	}
@@ -90,5 +102,8 @@ func (s *Sandbox) initEnvd(ctx context.Context, envVars map[string]string, acces
90102		return  err 
91103	}
92104
105+ 	// Track successful envd init 
106+ 	envdInitSuccess .Add (ctx , 1 , attributes )
107+ 
93108	return  nil 
94109}
0 commit comments