-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AKS resource detector Behavior tested on AKS, Azure VM, and Docker Desktop k8s. * Address PR feedback
- Loading branch information
Showing
10 changed files
with
258 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
processor/resourcedetectionprocessor/internal/azure/aks/aks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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 aks | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"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" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure" | ||
) | ||
|
||
const ( | ||
TypeStr = "aks" | ||
|
||
// Environment variable that is set when running on Kubernetes | ||
kubernetesServiceHostEnvVar = "KUBERNETES_SERVICE_HOST" | ||
) | ||
|
||
type Detector struct { | ||
provider azure.Provider | ||
} | ||
|
||
// NewDetector creates a new AKS detector | ||
func NewDetector(component.ProcessorCreateParams, internal.DetectorConfig) (internal.Detector, error) { | ||
return &Detector{provider: azure.NewProvider()}, nil | ||
} | ||
|
||
func (d *Detector) Detect(ctx context.Context) (pdata.Resource, error) { | ||
res := pdata.NewResource() | ||
|
||
if !onK8s() { | ||
return res, nil | ||
} | ||
|
||
// If we can't get a response from the metadata endpoint, we're not running in Azure | ||
if !azureMetadataAvailable(ctx, d.provider) { | ||
return res, nil | ||
} | ||
|
||
attrs := res.Attributes() | ||
attrs.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure) | ||
attrs.InsertString(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureAKS) | ||
|
||
return res, nil | ||
} | ||
|
||
func onK8s() bool { | ||
return os.Getenv(kubernetesServiceHostEnvVar) != "" | ||
} | ||
|
||
func azureMetadataAvailable(ctx context.Context, p azure.Provider) bool { | ||
_, err := p.Metadata(ctx) | ||
return err == nil | ||
} |
80 changes: 80 additions & 0 deletions
80
processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 aks | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure" | ||
) | ||
|
||
func TestNewDetector(t *testing.T) { | ||
d, err := NewDetector(component.ProcessorCreateParams{Logger: zap.NewNop()}, nil) | ||
require.NoError(t, err) | ||
assert.NotNil(t, d) | ||
} | ||
|
||
func TestDetector_Detect_K8s_Azure(t *testing.T) { | ||
os.Clearenv() | ||
setK8sEnv(t) | ||
detector := &Detector{provider: mockProvider()} | ||
res, err := detector.Detect(context.Background()) | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]interface{}{ | ||
"cloud.provider": "azure", | ||
"cloud.platform": "azure_aks", | ||
}, internal.AttributesToMap(res.Attributes()), "Resource attrs returned are incorrect") | ||
} | ||
|
||
func TestDetector_Detect_K8s_NonAzure(t *testing.T) { | ||
os.Clearenv() | ||
setK8sEnv(t) | ||
mp := &azure.MockProvider{} | ||
mp.On("Metadata").Return(nil, errors.New("")) | ||
detector := &Detector{provider: mp} | ||
res, err := detector.Detect(context.Background()) | ||
require.NoError(t, err) | ||
attrs := res.Attributes() | ||
assert.Equal(t, 0, attrs.Len()) | ||
} | ||
|
||
func TestDetector_Detect_NonK8s(t *testing.T) { | ||
os.Clearenv() | ||
detector := &Detector{provider: mockProvider()} | ||
res, err := detector.Detect(context.Background()) | ||
require.NoError(t, err) | ||
attrs := res.Attributes() | ||
assert.Equal(t, 0, attrs.Len()) | ||
} | ||
|
||
func mockProvider() *azure.MockProvider { | ||
mp := &azure.MockProvider{} | ||
mp.On("Metadata").Return(&azure.ComputeMetadata{}, nil) | ||
return mp | ||
} | ||
|
||
func setK8sEnv(t *testing.T) { | ||
err := os.Setenv("KUBERNETES_SERVICE_HOST", "localhost") | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.