Skip to content

Commit

Permalink
[internal/metadataproviders/azure] Fix memory leak on AKS (open-telem…
Browse files Browse the repository at this point in the history
…etry#32574)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
From [`client.Do` documentation](https://pkg.go.dev/net/http#Client.Do),
a client's response can safely be ignored when an error is returned, but
must be handled in all other cases, even when a response code is not
2XX. This fixes the internal AKS detection to properly close the
response body in the case of a non-200 status returned.

This also enables `goleak` on the `internal/metadata/azure` package
which helps ensure no goroutines are being leaked. `goleak` is what
detected this bug.

**Link to tracking Issue:** <Issue number if applicable>
open-telemetry#30438

**Testing:** <Describe what testing was performed and which tests were
added.>
All existing tests are passing as well as added `goleak` check.
  • Loading branch information
crobert-1 authored Apr 22, 2024
1 parent b82cd7f commit ce2d146
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/goleak_internalazure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: processor/resourcedetection, exporter/datadog

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix memory leak on AKS

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32574]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
5 changes: 3 additions & 2 deletions internal/metadataproviders/azure/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ func (p *azureProviderImpl) Metadata(ctx context.Context) (*ComputeMetadata, err
resp, err := p.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query Azure IMDS: %w", err)
} else if resp.StatusCode != 200 {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
//lint:ignore ST1005 Azure is a capitalized proper noun here
return nil, fmt.Errorf("Azure IMDS replied with status code: %s", resp.Status)
}

defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read Azure IMDS reply: %w", err)
Expand Down
14 changes: 14 additions & 0 deletions internal/metadataproviders/azure/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package azure

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

0 comments on commit ce2d146

Please sign in to comment.