-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[resourcedetectionprocessor] Azure detector #2372
Changes from 8 commits
fddddc1
c147817
70a349a
c51ef5f
7076340
ae5904e
d98e478
40d1e26
1ab6a6e
beda709
e75ddd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tigrannajaryan thoughts on these? Do we have any policy about using things that aren't in semantic conventions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we do. See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/attribute-and-label-naming.md#recommendations-for-opentelemetry-authors and https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/attribute-and-label-naming.md#recommendations-for-application-developers This seems applicable:
Or perhaps this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the Amazon ECS detector also uses attribute names that are not in the semantic conventions and they prefix them by I think the resource group one is fairly specific to how Azure works and has no equivalents on other cloud providers so it should have some Azure prefix (if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can keep it for now, but think about Azure attribute name conventions and if you can please make a proposal in spec repo. |
||
attrs.InsertString("azure.resourcegroup.name", compute.ResourceGroupName) | ||
|
||
return res, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added myself to CODEOWNERS here (I would recommend looking at the diff hiding whitespace changes)