diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index b573b287ba91..843a56d70d9a 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -14,69 +14,70 @@ * @open-telemetry/collector-contrib-approvers -exporter/alibabacloudlogserviceexporter/ @open-telemetry/collector-contrib-approvers @shabicheng @kongluoxing -exporter/awsemfexporter/ @open-telemetry/collector-contrib-approvers @anuraaga @shaochengwang @mxiamxia -exporter/awsprometheusremotewriteexporter/ @open-telemetry/collector-contrib-approvers @rakyll @alolita -exporter/awsxrayexporter/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga -exporter/azuremonitorexporter/ @open-telemetry/collector-contrib-approvers @pcwiese -exporter/carbonexporter/ @open-telemetry/collector-contrib-approvers @pjanotti -exporter/datadogexporter/ @open-telemetry/collector-contrib-approvers @KSerrania @ericmustin @mx-psi -exporter/dynatraceexporter/ @open-telemetry/collector-contrib-approvers @dyladan @arminru -exporter/elasticexporter/ @open-telemetry/collector-contrib-approvers @axw @simitt @jalvz -exporter/elasticsearchexporter/ @open-telemetry/collector-contrib-approvers @urso @faec @blakerouse -exporter/f5cloudexporter/ @open-telemetry/collector-contrib-approvers @gramidt -exporter/honeycombexporter/ @open-telemetry/collector-contrib-approvers @paulosman @lizthegrey @MikeGoldsmith -exporter/jaegerthrifthttpexporter/ @open-telemetry/collector-contrib-approvers @jpkrohling @pavolloffay -exporter/awskinesisexporter/ @open-telemetry/collector-contrib-approvers @owais -exporter/loadbalancingexporter/ @open-telemetry/collector-contrib-approvers @jpkrohling -exporter/logzioexporter/ @open-telemetry/collector-contrib-approvers @yyyogev -exporter/lokiexporter/ @open-telemetry/collector-contrib-approvers @gramidt -exporter/newrelicexporter/ @open-telemetry/collector-contrib-approvers @alanwest @a-feld @jack-berg @nrcventura -exporter/sapmexporter/ @open-telemetry/collector-contrib-approvers @owais @dmitryax -exporter/sentryexporter/ @open-telemetry/collector-contrib-approvers @AbhiPrasad -exporter/signalfxexporter/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 -exporter/signalfxcorrelationexporter/ @open-telemetry/collector-contrib-approvers @jrcamp @keitwb -exporter/splunkhecexporter/ @open-telemetry/collector-contrib-approvers @atoulme -exporter/stackdriverexporter/ @open-telemetry/collector-contrib-approvers @dashpole -exporter/sumologicexporter/ @open-telemetry/collector-contrib-approvers @pmm-sumo @sumo-drosiek -exporter/uptraceexporter/ @open-telemetry/collector-contrib-approvers @vmihailenco +exporter/alibabacloudlogserviceexporter/ @open-telemetry/collector-contrib-approvers @shabicheng @kongluoxing +exporter/awsemfexporter/ @open-telemetry/collector-contrib-approvers @anuraaga @shaochengwang @mxiamxia +exporter/awsprometheusremotewriteexporter/ @open-telemetry/collector-contrib-approvers @rakyll @alolita +exporter/awsxrayexporter/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga +exporter/azuremonitorexporter/ @open-telemetry/collector-contrib-approvers @pcwiese +exporter/carbonexporter/ @open-telemetry/collector-contrib-approvers @pjanotti +exporter/datadogexporter/ @open-telemetry/collector-contrib-approvers @KSerrania @ericmustin @mx-psi +exporter/dynatraceexporter/ @open-telemetry/collector-contrib-approvers @dyladan @arminru +exporter/elasticexporter/ @open-telemetry/collector-contrib-approvers @axw @simitt @jalvz +exporter/elasticsearchexporter/ @open-telemetry/collector-contrib-approvers @urso @faec @blakerouse +exporter/f5cloudexporter/ @open-telemetry/collector-contrib-approvers @gramidt +exporter/honeycombexporter/ @open-telemetry/collector-contrib-approvers @paulosman @lizthegrey @MikeGoldsmith +exporter/jaegerthrifthttpexporter/ @open-telemetry/collector-contrib-approvers @jpkrohling @pavolloffay +exporter/awskinesisexporter/ @open-telemetry/collector-contrib-approvers @owais +exporter/loadbalancingexporter/ @open-telemetry/collector-contrib-approvers @jpkrohling +exporter/logzioexporter/ @open-telemetry/collector-contrib-approvers @yyyogev +exporter/lokiexporter/ @open-telemetry/collector-contrib-approvers @gramidt +exporter/newrelicexporter/ @open-telemetry/collector-contrib-approvers @alanwest @a-feld @jack-berg @nrcventura +exporter/sapmexporter/ @open-telemetry/collector-contrib-approvers @owais @dmitryax +exporter/sentryexporter/ @open-telemetry/collector-contrib-approvers @AbhiPrasad +exporter/signalfxexporter/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 +exporter/signalfxcorrelationexporter/ @open-telemetry/collector-contrib-approvers @jrcamp @keitwb +exporter/splunkhecexporter/ @open-telemetry/collector-contrib-approvers @atoulme +exporter/stackdriverexporter/ @open-telemetry/collector-contrib-approvers @dashpole +exporter/sumologicexporter/ @open-telemetry/collector-contrib-approvers @pmm-sumo @sumo-drosiek +exporter/uptraceexporter/ @open-telemetry/collector-contrib-approvers @vmihailenco -extension/httpforwarder/ @open-telemetry/collector-contrib-approvers @asuresh4 -extension/observer/ @open-telemetry/collector-contrib-approvers @asuresh4 @jrcamp +extension/httpforwarder/ @open-telemetry/collector-contrib-approvers @asuresh4 +extension/observer/ @open-telemetry/collector-contrib-approvers @asuresh4 @jrcamp -internal/awsxray/ @open-telemetry/collector-contrib-approvers @anuraaga @mxiamxia -internal/k8sconfig/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 -internal/splunk/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 -internal/stanza/ @open-telemetry/collector-contrib-approvers @djaglowski +internal/awsxray/ @open-telemetry/collector-contrib-approvers @anuraaga @mxiamxia +internal/k8sconfig/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 +internal/splunk/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 +internal/stanza/ @open-telemetry/collector-contrib-approvers @djaglowski -pkg/batchpertrace/ @open-telemetry/collector-contrib-approvers @jpkrohling +pkg/batchpertrace/ @open-telemetry/collector-contrib-approvers @jpkrohling -processor/groupbyattrsprocessor/ @open-telemetry/collector-contrib-approvers @pmm-sumo -processor/groupbytraceprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling -processor/k8sprocessor/ @open-telemetry/collector-contrib-approvers @owais @dmitryax @pmm-sumo -processor/metricstransformprocessor/ @open-telemetry/collector-contrib-approvers @james-bebbington -processor/resourcedetectionprocessor/ @open-telemetry/collector-contrib-approvers @jrcamp @pmm-sumo @anuraaga @dashpole -processor/routingprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling -processor/tailsamplingprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling +processor/groupbyattrsprocessor/ @open-telemetry/collector-contrib-approvers @pmm-sumo +processor/groupbytraceprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling +processor/k8sprocessor/ @open-telemetry/collector-contrib-approvers @owais @dmitryax @pmm-sumo +processor/metricstransformprocessor/ @open-telemetry/collector-contrib-approvers @james-bebbington +processor/resourcedetectionprocessor/ @open-telemetry/collector-contrib-approvers @jrcamp @pmm-sumo @anuraaga @dashpole +processor/resourcedetectionprocessor/internal/azure @open-telemetry/collector-contrib-approvers @mx-psi +processor/routingprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling +processor/tailsamplingprocessor/ @open-telemetry/collector-contrib-approvers @jpkrohling -receiver/awsecscontainermetricsreceiver/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga -receiver/awsxrayreceiver/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga -receiver/carbonreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti -receiver/collectdreceiver/ @open-telemetry/collector-contrib-approvers @owais -receiver/dockerstatsreceiver/ @open-telemetry/collector-contrib-approvers @rmfitzpatrick -receiver/jmxreceiver/ @open-telemetry/collector-contrib-approvers @rmfitzpatrick -receiver/k8sclusterreceiver/ @open-telemetry/collector-contrib-approvers @asuresh4 -receiver/kubeletstatsreceiver/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 -receiver/prometheusexecreceiver/ @open-telemetry/collector-contrib-approvers @keitwb -receiver/receivercreator/ @open-telemetry/collector-contrib-approvers @jrcamp -receiver/redisreceiver/ @open-telemetry/collector-contrib-approvers @pmcollins @jrcamp -receiver/sapmreceiver/ @open-telemetry/collector-contrib-approvers @owais -receiver/signalfxreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti @asuresh4 -receiver/simpleprometheusreceiver/ @open-telemetry/collector-contrib-approvers @asuresh4 -receiver/splunkhecreceiver/ @open-telemetry/collector-contrib-approvers @atoulme @keitwb -receiver/filelogreceiver/ @open-telemetry/collector-contrib-approvers @djaglowski -receiver/statsdreceiver/ @open-telemetry/collector-contrib-approvers @keitwb @jmacd -receiver/wavefrontreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti -receiver/windowsperfcountersreceiver/ @open-telemetry/collector-contrib-approvers @dashpole +receiver/awsecscontainermetricsreceiver/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga +receiver/awsxrayreceiver/ @open-telemetry/collector-contrib-approvers @kbrockhoff @anuraaga +receiver/carbonreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti +receiver/collectdreceiver/ @open-telemetry/collector-contrib-approvers @owais +receiver/dockerstatsreceiver/ @open-telemetry/collector-contrib-approvers @rmfitzpatrick +receiver/jmxreceiver/ @open-telemetry/collector-contrib-approvers @rmfitzpatrick +receiver/k8sclusterreceiver/ @open-telemetry/collector-contrib-approvers @asuresh4 +receiver/kubeletstatsreceiver/ @open-telemetry/collector-contrib-approvers @pmcollins @asuresh4 +receiver/prometheusexecreceiver/ @open-telemetry/collector-contrib-approvers @keitwb +receiver/receivercreator/ @open-telemetry/collector-contrib-approvers @jrcamp +receiver/redisreceiver/ @open-telemetry/collector-contrib-approvers @pmcollins @jrcamp +receiver/sapmreceiver/ @open-telemetry/collector-contrib-approvers @owais +receiver/signalfxreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti @asuresh4 +receiver/simpleprometheusreceiver/ @open-telemetry/collector-contrib-approvers @asuresh4 +receiver/splunkhecreceiver/ @open-telemetry/collector-contrib-approvers @atoulme @keitwb +receiver/filelogreceiver/ @open-telemetry/collector-contrib-approvers @djaglowski +receiver/statsdreceiver/ @open-telemetry/collector-contrib-approvers @keitwb @jmacd +receiver/wavefrontreceiver/ @open-telemetry/collector-contrib-approvers @pjanotti +receiver/windowsperfcountersreceiver/ @open-telemetry/collector-contrib-approvers @dashpole -tracegen/ @open-telemetry/collector-contrib-approvers @jpkrohling +tracegen/ @open-telemetry/collector-contrib-approvers @jpkrohling diff --git a/processor/resourcedetectionprocessor/README.md b/processor/resourcedetectionprocessor/README.md index cf2ef4494a7a..ad3153f0b8a9 100644 --- a/processor/resourcedetectionprocessor/README.md +++ b/processor/resourcedetectionprocessor/README.md @@ -78,10 +78,20 @@ ec2: * service.instance.id * service.version +* Azure: Queries the [Azure Instance Metadata Service](https://aka.ms/azureimds) to retrieve the following resource attributes: + + * cloud.provider (azure) + * cloud.region + * cloud.account.id (subscription ID) + * host.id (virtual machine ID) + * host.name + * azure.vm.size (virtual machine size) + * azure.resourcegroup.name (resource group name) + ## Configuration ```yaml -# a list of resource detectors to run, valid options are: "env", "system", "gce", "ec2", "ecs", "elastic_beanstalk" +# a list of resource detectors to run, valid options are: "env", "system", "gce", "ec2", "ecs", "elastic_beanstalk", "azure" detectors: [ ] # determines if existing resource attributes should be overridden or preserved, defaults to true override: diff --git a/processor/resourcedetectionprocessor/factory.go b/processor/resourcedetectionprocessor/factory.go index e6ccc0d3f090..c648f8db8b7a 100644 --- a/processor/resourcedetectionprocessor/factory.go +++ b/processor/resourcedetectionprocessor/factory.go @@ -29,6 +29,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ec2" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ecs" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/elasticbeanstalk" + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/env" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp/gce" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system" @@ -59,6 +60,7 @@ func NewFactory() component.ProcessorFactory { ec2.TypeStr: ec2.NewDetector, ecs.TypeStr: ecs.NewDetector, elasticbeanstalk.TypeStr: elasticbeanstalk.NewDetector, + azure.TypeStr: azure.NewDetector, }) f := &factory{ diff --git a/processor/resourcedetectionprocessor/internal/azure/azure.go b/processor/resourcedetectionprocessor/internal/azure/azure.go new file mode 100644 index 000000000000..6e4a452f88aa --- /dev/null +++ b/processor/resourcedetectionprocessor/internal/azure/azure.go @@ -0,0 +1,64 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "context" + "fmt" + + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/translator/conventions" + + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" +) + +const ( + // TypeStr is the detector type string + TypeStr = "azure" +) + +var _ internal.Detector = (*Detector)(nil) + +// Detector is an Azure metadata detector +type Detector struct { + provider azureProvider +} + +// NewDetector creates a new Azure metadata detector +func NewDetector(component.ProcessorCreateParams, internal.DetectorConfig) (internal.Detector, error) { + return &Detector{provider: newProvider()}, nil +} + +// Detect detects system metadata and returns a resource with the available ones +func (d *Detector) Detect(ctx context.Context) (pdata.Resource, error) { + res := pdata.NewResource() + attrs := res.Attributes() + + compute, err := d.provider.metadata(ctx) + if err != nil { + return res, fmt.Errorf("failed getting metadata: %w", err) + } + + attrs.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure) + attrs.InsertString(conventions.AttributeHostName, compute.Name) + attrs.InsertString(conventions.AttributeCloudRegion, compute.Location) + attrs.InsertString(conventions.AttributeHostID, compute.VMID) + attrs.InsertString(conventions.AttributeCloudAccount, compute.SubscriptionID) + attrs.InsertString("azure.vm.size", compute.VMSize) + attrs.InsertString("azure.resourcegroup.name", compute.ResourceGroupName) + + return res, nil +} diff --git a/processor/resourcedetectionprocessor/internal/azure/azure_test.go b/processor/resourcedetectionprocessor/internal/azure/azure_test.go new file mode 100644 index 000000000000..317f8b4c46d1 --- /dev/null +++ b/processor/resourcedetectionprocessor/internal/azure/azure_test.go @@ -0,0 +1,86 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/translator/conventions" + "go.uber.org/zap" + + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" +) + +type mockProvider struct { + mock.Mock +} + +func (m *mockProvider) metadata(_ context.Context) (*computeMetadata, error) { + args := m.MethodCalled("metadata") + return args.Get(0).(*computeMetadata), args.Error(1) +} + +func TestNewDetector(t *testing.T) { + d, err := NewDetector(component.ProcessorCreateParams{Logger: zap.NewNop()}, nil) + require.NoError(t, err) + assert.NotNil(t, d) +} + +func TestDetectAzureAvailable(t *testing.T) { + mp := &mockProvider{} + mp.On("metadata").Return(&computeMetadata{ + Location: "location", + Name: "name", + VMID: "vmID", + VMSize: "vmSize", + SubscriptionID: "subscriptionID", + ResourceGroupName: "resourceGroup", + }, nil) + + detector := &Detector{provider: mp} + res, err := detector.Detect(context.Background()) + require.NoError(t, err) + mp.AssertExpectations(t) + res.Attributes().Sort() + + expected := internal.NewResource(map[string]interface{}{ + conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAzure, + conventions.AttributeHostName: "name", + conventions.AttributeCloudRegion: "location", + conventions.AttributeHostID: "vmID", + conventions.AttributeCloudAccount: "subscriptionID", + "azure.vm.size": "vmSize", + "azure.resourcegroup.name": "resourceGroup", + }) + expected.Attributes().Sort() + + assert.Equal(t, expected, res) +} + +func TestDetectError(t *testing.T) { + mp := &mockProvider{} + mp.On("metadata").Return(&computeMetadata{}, fmt.Errorf("mock error")) + + detector := &Detector{provider: mp} + res, err := detector.Detect(context.Background()) + assert.Error(t, err) + assert.True(t, internal.IsEmptyResource(res)) +} diff --git a/processor/resourcedetectionprocessor/internal/azure/metadata.go b/processor/resourcedetectionprocessor/internal/azure/metadata.go new file mode 100644 index 000000000000..7187f2893934 --- /dev/null +++ b/processor/resourcedetectionprocessor/internal/azure/metadata.go @@ -0,0 +1,105 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file contains code based on the Azure IMDS samples, https://github.com/microsoft/azureimds +// under the Apache License 2.0 + +package azure + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +const ( + // Azure IMDS compute endpoint, see https://aka.ms/azureimds + metadataEndpoint = "http://169.254.169.254/metadata/instance/compute" +) + +// azureProvider gets metadata from the Azure IMDS +type azureProvider interface { + metadata(ctx context.Context) (*computeMetadata, error) +} + +type azureProviderImpl struct { + endpoint string + client *http.Client +} + +// newProvider creates a new metadata provider +func newProvider() azureProvider { + return &azureProviderImpl{ + endpoint: metadataEndpoint, + client: &http.Client{}, + } +} + +// computeMetadata is the Azure IMDS compute metadata response format +type computeMetadata struct { + Location string `json:"location"` + Name string `json:"name"` + VMID string `json:"vmID"` + VMSize string `json:"vmSize"` + SubscriptionID string `json:"subscriptionID"` + ResourceGroupName string `json:"resourceGroupName"` +} + +// queryEndpointWithContext queries a given endpoint and parses the output to the Azure IMDS format +func (p *azureProviderImpl) metadata(ctx context.Context) (*computeMetadata, error) { + const ( + // API version used + apiVersionKey = "api-version" + apiVersion = "2020-09-01" + + // format used + formatKey = "format" + jsonFormat = "json" + ) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.endpoint, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %v", err) + } + + req.Header.Add("Metadata", "True") + q := req.URL.Query() + q.Add(formatKey, jsonFormat) + q.Add(apiVersionKey, apiVersion) + req.URL.RawQuery = q.Encode() + + resp, err := p.client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to query Azure IMDS: %v", err) + } else 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 := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read Azure IMDS reply: %v", err) + } + + var metadata *computeMetadata + err = json.Unmarshal(respBody, &metadata) + if err != nil { + return nil, fmt.Errorf("failed to decode Azure IMDS reply: %v", err) + } + + return metadata, nil +} diff --git a/processor/resourcedetectionprocessor/internal/azure/metadata_test.go b/processor/resourcedetectionprocessor/internal/azure/metadata_test.go new file mode 100644 index 000000000000..b8cb8d2f8f35 --- /dev/null +++ b/processor/resourcedetectionprocessor/internal/azure/metadata_test.go @@ -0,0 +1,88 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNewProvider(t *testing.T) { + provider := newProvider() + assert.NotNil(t, provider) +} + +func TestQueryEndpointFailed(t *testing.T) { + ts := httptest.NewServer(http.NotFoundHandler()) + defer ts.Close() + + provider := &azureProviderImpl{ + endpoint: ts.URL, + client: &http.Client{}, + } + + _, err := provider.metadata(context.Background()) + assert.Error(t, err) +} + +func TestQueryEndpointMalformed(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "{") + })) + defer ts.Close() + + provider := &azureProviderImpl{ + endpoint: ts.URL, + client: &http.Client{}, + } + + _, err := provider.metadata(context.Background()) + assert.Error(t, err) +} + +func TestQueryEndpointCorrect(t *testing.T) { + sentMetadata := &computeMetadata{ + Location: "location", + Name: "name", + VMID: "vmID", + VMSize: "vmSize", + SubscriptionID: "subscriptionID", + ResourceGroupName: "resourceGroup", + } + marshalledMetadata, err := json.Marshal(sentMetadata) + require.NoError(t, err) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write(marshalledMetadata) + })) + defer ts.Close() + + provider := &azureProviderImpl{ + endpoint: ts.URL, + client: &http.Client{}, + } + + recvMetadata, err := provider.metadata(context.Background()) + + require.NoError(t, err) + assert.Equal(t, *sentMetadata, *recvMetadata) +} diff --git a/processor/resourcedetectionprocessor/testdata/config.yaml b/processor/resourcedetectionprocessor/testdata/config.yaml index 82353690ebe4..732f8a372be1 100644 --- a/processor/resourcedetectionprocessor/testdata/config.yaml +++ b/processor/resourcedetectionprocessor/testdata/config.yaml @@ -23,6 +23,10 @@ processors: detectors: [env, system] timeout: 2s override: false + resourcedetection/azure: + detectors: [env, azure] + timeout: 2s + override: false exporters: exampleexporter: @@ -37,4 +41,5 @@ service: # - resourcedetection/gce # - resourcedetection/ec2 # - resourcedetection/ecs + # - resourcedetection/azure exporters: [exampleexporter]