Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pyohannes committed May 29, 2024
1 parent a3503a4 commit 0fabd24
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
35 changes: 14 additions & 21 deletions detectors/azure/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
)

const (
defaultAzureVmMetadataEndpoint = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
)

type config struct {
client Client
endpoint string
}

func newConfig(options ...Option) config {
c := config{&azureInstanceMetadataClient{}}
c := config{defaultAzureVmMetadataEndpoint}
for _, option := range options {
c = option.apply(c)
}
Expand All @@ -38,21 +42,17 @@ func (fn optionFunc) apply(c config) config {
return fn(c)
}

// WithClient sets the client for obtaining a Azure instance metadata JSON.
func WithClient(t Client) Option {
// WithEndpoint sets the endpoint for obtaining a Azure instance metadata JSON.
func WithEndpoint(e string) Option {
return optionFunc(func(c config) config {
c.client = t
c.endpoint = e

return c
})
}

func (cfg config) getClient() Client {
return cfg.client
}

type resourceDetector struct {
client Client
endpoint string
}

type vmMetadata struct {
Expand All @@ -68,12 +68,12 @@ type vmMetadata struct {
// New returns a [resource.Detector] that will detect Azure VM resources.
func New(opts ...Option) resource.Detector {
c := newConfig(opts...)
return &resourceDetector{c.getClient()}
return &resourceDetector{c.endpoint}
}

// Detect detects associated resources when running on an Azure VM.
func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) {
jsonMetadata, err := detector.client.GetJSONMetadata()
jsonMetadata, err := detector.getJSONMetadata()
if err != nil {
return nil, err

Check warning on line 78 in detectors/azure/vm/vm.go

View check run for this annotation

Codecov / codecov/patch

detectors/azure/vm/vm.go#L78

Added line #L78 was not covered by tests
}
Expand Down Expand Up @@ -114,19 +114,12 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil
}

// Client is an interface that allows mocking for testing.
type Client interface {
GetJSONMetadata() ([]byte, error)
}

type azureInstanceMetadataClient struct{}

func (c *azureInstanceMetadataClient) GetJSONMetadata() ([]byte, error) {
func (detector *resourceDetector) getJSONMetadata() ([]byte, error) {
PTransport := &http.Transport{Proxy: nil}

client := http.Client{Transport: PTransport}

req, err := http.NewRequest("GET", "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json", nil)
req, err := http.NewRequest("GET", detector.endpoint, nil)
if err != nil {
return nil, err

Check warning on line 124 in detectors/azure/vm/vm.go

View check run for this annotation

Codecov / codecov/patch

detectors/azure/vm/vm.go#L124

Added line #L124 was not covered by tests
}
Expand Down
33 changes: 16 additions & 17 deletions detectors/azure/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package vm

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +20,7 @@ import (
func TestDetect(t *testing.T) {
type input struct {
jsonMetadata string
err error
statusCode int
}
type expected struct {
resource *resource.Resource
Expand All @@ -41,7 +43,7 @@ func TestDetect(t *testing.T) {
"osType": "linux",
"version": "6.5.0-26-generic"
}`,
err: nil,
statusCode: http.StatusOK,
},
expected: expected{
resource: resource.NewWithAttributes(semconv.SchemaURL, []attribute.KeyValue{
Expand All @@ -61,7 +63,7 @@ func TestDetect(t *testing.T) {
{
input: input{
jsonMetadata: `{`,
err: nil,
statusCode: http.StatusOK,
},
expected: expected{
resource: nil,
Expand All @@ -71,7 +73,7 @@ func TestDetect(t *testing.T) {
{
input: input{
jsonMetadata: "",
err: errors.New("cannot get metadata"),
statusCode: http.StatusNotFound,
},
expected: expected{
resource: nil,
Expand All @@ -81,23 +83,20 @@ func TestDetect(t *testing.T) {
}

for _, tCase := range testTable {
detector := New(WithClient(&mockClient{
jsonMetadata: []byte(tCase.input.jsonMetadata),
err: tCase.input.err,
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)

if r.Header.Get("Metadata") == "True" {
fmt.Fprintf(w, tCase.input.jsonMetadata)
}
}))
defer svr.Close()

detector := New(WithEndpoint(svr.URL))

azureResource, err := detector.Detect(context.Background())

assert.Equal(t, err != nil, tCase.expected.err)
assert.Equal(t, tCase.expected.resource, azureResource)
}
}

type mockClient struct {
jsonMetadata []byte
err error
}

func (c *mockClient) GetJSONMetadata() ([]byte, error) {
return c.jsonMetadata, c.err
}

0 comments on commit 0fabd24

Please sign in to comment.