Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pyohannes committed May 31, 2024
1 parent d4e48d3 commit a81f6a0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 75 deletions.
29 changes: 1 addition & 28 deletions detectors/azure/azurevm/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
# Azure VM Resource detector

The Azure VM resource detector supports detecting attributes specific to Azure VMs.

## Usage

```golang
// Instantiate a new Azure VM resource detector
azureVmResourceDetector := azurevm.New()
resource, err := azureVmResourceDetector.Detect(context.Background())
```

## Supported attributes

According to semantic conventions for
[host](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md),
[cloud](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/cloud.md),
and
[os](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/os.md)
attributes, each of the following attributes is added if it is available:

* `cloud.provider`
* `cloud.platform`
* `cloud.region`
* `cloud.resource_id`
* `host.id`
* `host.name`
* `host.type`
* `os.type`
* `os.version`
[![PkgGoDev](https://pkg.go.dev/badge/go.opentelemetry.io/contrib/detectors/azure/azurevm)](https://pkg.go.dev/go.opentelemetry.io/contrib/detectors/azure/azurevm)
24 changes: 24 additions & 0 deletions detectors/azure/azurevm/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

/*
The Azure VM [resource.Detector] supports detecting attributes specific to Azure VMs.
According to semantic conventions for [host], [cloud], and [os] attributes,
each of the following attributes is added if it is available:
- cloud.provider
- cloud.platform
- cloud.region
- cloud.resource_id
- host.id
- host.name
- host.type
- os.type
- os.version
[host]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md
[cloud]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/cloud.md
[os]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/os.md
*/
package azurevm // import "go.opentelemetry.io/contrib/detectors/azure/azurevm"
20 changes: 20 additions & 0 deletions detectors/azure/azurevm/example_new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package azurevm_test

import (
"context"
"fmt"

"go.opentelemetry.io/contrib/detectors/azure/azurevm"
)

func ExampleNew() {
azureVmResourceDetector := azurevm.New()
resource, err := azureVmResourceDetector.Detect(context.Background())

if err != nil {
panic(err)
}

// Now, you can use the resource (e.g. pass it to a tracer or meter provider).
fmt.Println(resource.SchemaURL())
}
63 changes: 18 additions & 45 deletions detectors/azure/azurevm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,6 @@ const (
defaultAzureVMMetadataEndpoint = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
)

type config struct {
endpoint string
}

func newConfig(options ...Option) config {
c := config{defaultAzureVMMetadataEndpoint}
for _, option := range options {
c = option.apply(c)
}

return c
}

// Option applies an Azure VM detector configuration option.
type Option interface {
apply(config) config
}

type optionFunc func(config) config

func (fn optionFunc) apply(c config) config {
return fn(c)
}

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

return c
})
}

type resourceDetector struct {
endpoint string
}
Expand All @@ -67,16 +34,19 @@ 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.endpoint}
func New() *resourceDetector {
return &resourceDetector{defaultAzureVMMetadataEndpoint}
}

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

var metadata vmMetadata
Expand Down Expand Up @@ -115,28 +85,31 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil
}

func (detector *resourceDetector) getJSONMetadata() ([]byte, error) {
func (detector *resourceDetector) getJSONMetadata() ([]byte, error, bool) {
pTransport := &http.Transport{Proxy: nil}

client := http.Client{Transport: pTransport}

req, err := http.NewRequest("GET", detector.endpoint, nil)
if err != nil {
return nil, err
return nil, err, false

Check warning on line 95 in detectors/azure/azurevm/vm.go

View check run for this annotation

Codecov / codecov/patch

detectors/azure/azurevm/vm.go#L95

Added line #L95 was not covered by tests
}

req.Header.Add("Metadata", "True")

resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, err, false

Check warning on line 102 in detectors/azure/azurevm/vm.go

View check run for this annotation

Codecov / codecov/patch

detectors/azure/azurevm/vm.go#L102

Added line #L102 was not covered by tests
}

if resp.StatusCode != http.StatusOK {
return nil, errors.New(http.StatusText(resp.StatusCode))
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
bytes, err := io.ReadAll(resp.Body)
return bytes, err, true
}

defer resp.Body.Close()
runningInAzure := resp.StatusCode < 400 || resp.StatusCode > 499

return io.ReadAll(resp.Body)
return nil, errors.New(http.StatusText(resp.StatusCode)), runningInAzure
}
15 changes: 13 additions & 2 deletions detectors/azure/azurevm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,24 @@ func TestDetect(t *testing.T) {
},
{
input: input{
jsonMetadata: "{}",
jsonMetadata: "",
statusCode: http.StatusNotFound,
},
expected: expected{
resource: resource.Empty(),
err: false,
},
},
{
input: input{
jsonMetadata: "",
statusCode: http.StatusInternalServerError,
},
expected: expected{
resource: nil,
err: true,
},
},
}

for _, tCase := range testTable {
Expand All @@ -91,7 +101,8 @@ func TestDetect(t *testing.T) {
}
}))

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

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

Expand Down

0 comments on commit a81f6a0

Please sign in to comment.