| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package azurevm // import "go.opentelemetry.io/contrib/detectors/azure/azurevm" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/resource" |
| 15 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 16 |
) |
| 17 |
|
| 18 |
const defaultAzureVMMetadataEndpoint = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json" |
| 19 |
|
| 20 |
// ResourceDetector collects resource information of Azure VMs. |
| 21 |
type ResourceDetector struct { |
| 22 |
endpoint string |
| 23 |
} |
| 24 |
|
| 25 |
type vmMetadata struct { |
| 26 |
VMId *string `json:"vmId"` |
| 27 |
Location *string `json:"location"` |
| 28 |
ResourceId *string `json:"resourceId"` |
| 29 |
Name *string `json:"name"` |
| 30 |
VMSize *string `json:"vmSize"` |
| 31 |
OsType *string `json:"osType"` |
| 32 |
Version *string `json:"version"` |
| 33 |
} |
| 34 |
|
| 35 |
// New returns a [ResourceDetector] that will detect Azure VM resources. |
| 36 |
func New() *ResourceDetector { |
| 37 |
return &ResourceDetector{defaultAzureVMMetadataEndpoint} |
| 38 |
} |
| 39 |
|
| 40 |
// Detect detects associated resources when running on an Azure VM. |
| 41 |
func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { |
| 42 |
jsonMetadata, runningInAzure, err := detector.getJSONMetadata(ctx) |
| 43 |
if err != nil { |
| 44 |
if !runningInAzure { |
| 45 |
return resource.Empty(), nil |
| 46 |
} |
| 47 |
|
| 48 |
return nil, err |
| 49 |
} |
| 50 |
|
| 51 |
var metadata vmMetadata |
| 52 |
err = json.Unmarshal(jsonMetadata, &metadata) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
|
| 57 |
attributes := []attribute.KeyValue{ |
| 58 |
semconv.CloudProviderAzure, |
| 59 |
semconv.CloudPlatformAzureVM, |
| 60 |
} |
| 61 |
|
| 62 |
if metadata.VMId != nil { |
| 63 |
attributes = append(attributes, semconv.HostID(*metadata.VMId)) |
| 64 |
} |
| 65 |
if metadata.Location != nil { |
| 66 |
attributes = append(attributes, semconv.CloudRegion(*metadata.Location)) |
| 67 |
} |
| 68 |
if metadata.ResourceId != nil { |
| 69 |
attributes = append(attributes, semconv.CloudResourceID(*metadata.ResourceId)) |
| 70 |
} |
| 71 |
if metadata.Name != nil { |
| 72 |
attributes = append(attributes, semconv.HostName(*metadata.Name)) |
| 73 |
} |
| 74 |
if metadata.VMSize != nil { |
| 75 |
attributes = append(attributes, semconv.HostType(*metadata.VMSize)) |
| 76 |
} |
| 77 |
if metadata.OsType != nil { |
| 78 |
attributes = append(attributes, semconv.OSTypeKey.String(*metadata.OsType)) |
| 79 |
} |
| 80 |
if metadata.Version != nil { |
| 81 |
attributes = append(attributes, semconv.OSVersion(*metadata.Version)) |
| 82 |
} |
| 83 |
|
| 84 |
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 85 |
} |
| 86 |
|
| 87 |
func (detector *ResourceDetector) getJSONMetadata(ctx context.Context) ([]byte, bool, error) { |
| 88 |
pTransport := &http.Transport{Proxy: nil} |
| 89 |
|
| 90 |
client := http.Client{Transport: pTransport} |
| 91 |
|
| 92 |
req, err := http.NewRequestWithContext(ctx, http.MethodGet, detector.endpoint, http.NoBody) |
| 93 |
if err != nil { |
| 94 |
return nil, false, err |
| 95 |
} |
| 96 |
|
| 97 |
req.Header.Add("Metadata", "True") |
| 98 |
|
| 99 |
resp, err := client.Do(req) |
| 100 |
if err != nil { |
| 101 |
return nil, false, err |
| 102 |
} |
| 103 |
defer resp.Body.Close() |
| 104 |
|
| 105 |
if resp.StatusCode == http.StatusOK { |
| 106 |
bytes, err := io.ReadAll(resp.Body) |
| 107 |
return bytes, true, err |
| 108 |
} |
| 109 |
|
| 110 |
runningInAzure := resp.StatusCode < 400 || resp.StatusCode > 499 |
| 111 |
|
| 112 |
return nil, runningInAzure, errors.New(http.StatusText(resp.StatusCode)) |
| 113 |
} |