Skip to content

Commit

Permalink
refactor(metadata handler): uses req context instead of context.Backg…
Browse files Browse the repository at this point in the history
…round
  • Loading branch information
VAveryanov8 committed Jan 9, 2025
1 parent f6d8540 commit fcbd42e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/cmd/agent/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit fcbd42e

Please sign in to comment.