From fcbd42e093728c9762dabbbedffcca2d7ea76bad Mon Sep 17 00:00:00 2001 From: Vasil Averyanau Date: Thu, 9 Jan 2025 13:36:08 +0100 Subject: [PATCH] refactor(metadata handler): uses req context instead of context.Background --- pkg/cmd/agent/metadata.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/agent/metadata.go b/pkg/cmd/agent/metadata.go index 91929c942..860988e8f 100644 --- a/pkg/cmd/agent/metadata.go +++ b/pkg/cmd/agent/metadata.go @@ -22,7 +22,7 @@ func newMetadataHandler(logger log.Logger) http.HandlerFunc { ) // Caches only successful result of GetInstanceMetadata. - lazyGetMetadata := func() (cloudmeta.InstanceMetadata, error) { + lazyGetMetadata := func(ctx context.Context) (cloudmeta.InstanceMetadata, error) { m.Lock() defer m.Unlock() if loaded { @@ -34,7 +34,7 @@ func newMetadataHandler(logger log.Logger) http.HandlerFunc { return cloudmeta.InstanceMetadata{}, errors.Wrap(err, "NewCloudMeta") } - metadata, err = metaSvc.GetInstanceMetadata(context.Background()) + metadata, err = metaSvc.GetInstanceMetadata(ctx) if err != nil { return cloudmeta.InstanceMetadata{}, err } @@ -44,12 +44,13 @@ func newMetadataHandler(logger log.Logger) http.HandlerFunc { } return func(w http.ResponseWriter, r *http.Request) { - instanceMeta, err := lazyGetMetadata() + ctx := r.Context() + instanceMeta, err := lazyGetMetadata(ctx) if err != nil { // Metadata may not be available for several reasons: // 1. running on-premise 2. disabled 3. smth went wrong with metadata server. // As we cannot distinguish between these cases, we can only log err. - logger.Error(r.Context(), "GetInstanceMetadata", "err", err) + logger.Error(ctx, "GetInstanceMetadata", "err", err) render.Respond(w, r, models.InstanceMetadata{}) return }