Skip to content
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

Added LabelRename functionality to enable label normalization (for ECS for now) #24

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ validate-only:

validate: validate-deps validate-only

run-docker-dev:
docker run --rm -it -v $(WORKDIR):/go/src/github.com/newrelic/nri-docker golang:1.10

compile-deps:
@echo "=== $(INTEGRATION) === [ compile-deps ]: installing build dependencies..."
@$(GO) get -v -d -t ./...
Expand Down
28 changes: 27 additions & 1 deletion src/nri/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ func (cs *ContainerSampler) SampleAll(i *integration.Integration) error {
return err
}

shortContainerID := container.ID[:shortContainerIDLength]
var shortContainerID string
if len(container.ID) > shortContainerIDLength {
shortContainerID = container.ID[:shortContainerIDLength]
} else {
shortContainerID = container.ID
}

ms := entity.NewMetricSet(containerSampleName,
metric.Attr(attrContainerID, container.ID),
metric.Attr(attrShortContainerID, shortContainerID),
Expand Down Expand Up @@ -145,9 +151,29 @@ func attributes(container types.Container) []entry {
}
}

// labelRename contains a list of labels that should be
// renamed. It does not rename the label in place, but creates
// a copy to ensure we are not deleting any data.
var labelRename = map[string]string{
"com.amazonaws.ecs.container-name": "ecsContainerName",
"com.amazonaws.ecs.cluster": "ecsClusterName",
"com.amazonaws.ecs.task-arn": "ecsTaskArn",
"com.amazonaws.ecs.task-definition-family": "ecsTaskDefinitionFamily",
"com.amazonaws.ecs.task-definition-version": "ecsTaskDefinitionVersion",
}

func labels(container types.Container) []entry {
metrics := make([]entry, 0, len(container.Labels))
for key, val := range container.Labels {

if newName, ok := labelRename[key]; ok {
metrics = append(metrics, entry{
Name: newName,
Value: val,
Type: metric.ATTRIBUTE,
})
}

metrics = append(metrics, entry{
Name: labelPrefix + key,
Value: val,
Expand Down
112 changes: 112 additions & 0 deletions src/nri/sampler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package nri

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Thank you ❤️


import (
"context"
"testing"

"github.com/newrelic/infra-integrations-sdk/integration"
"github.com/stretchr/testify/assert"

"github.com/docker/docker/api/types"

"github.com/stretchr/testify/mock"

"github.com/newrelic/nri-docker/src/biz"
)

// mocker is a Docker mock
type mocker struct {
mock.Mock
}

func (m *mocker) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
args := m.Called(ctx, options)
return args.Get(0).([]types.Container), args.Error(1)
}

func (m *mocker) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
args := m.Called(ctx, containerID)
return args.Get(0).(types.ContainerJSON), args.Error(1)
}

type mockStorer struct {
mock.Mock
}

func (m *mockStorer) Set(key string, value interface{}) int64 {
args := m.Called(key, value)
return args.Get(0).(int64)
}
func (m *mockStorer) Get(key string, valuePtr interface{}) (int64, error) {
args := m.Called(key, valuePtr)
return args.Get(0).(int64), args.Error(1)
}
func (m *mockStorer) Delete(key string) error {
return m.Called(key).Error(0)
}
func (m *mockStorer) Save() error {
return m.Called().Error(0)
}

func TestECSLabelRename(t *testing.T) {
var (
givenLabels = map[string]string{
"com.amazonaws.ecs.container-name": "the-container-name",
"com.amazonaws.ecs.cluster": "the-cluster-name",
"com.amazonaws.ecs.task-arn": "the-task-arn",
"com.amazonaws.ecs.task-definition-family": "the-task-definition-family",
"com.amazonaws.ecs.task-definition-version": "the-task-definition-version",
"my-label-name": "my-label-value",
}
expectedLabels = map[string]string{
// the original labels
"label.com.amazonaws.ecs.container-name": "the-container-name",
"label.com.amazonaws.ecs.cluster": "the-cluster-name",
"label.com.amazonaws.ecs.task-arn": "the-task-arn",
"label.com.amazonaws.ecs.task-definition-family": "the-task-definition-family",
"label.com.amazonaws.ecs.task-definition-version": "the-task-definition-version",
// the normalized ECS labels, not prefixed with "label."
"ecsContainerName": "the-container-name",
"ecsClusterName": "the-cluster-name",
"ecsTaskArn": "the-task-arn",
"ecsTaskDefinitionFamily": "the-task-definition-family",
"ecsTaskDefinitionVersion": "the-task-definition-version",
// the random label
"label.my-label-name": "my-label-value",
}
)
mocker := &mocker{}
mocker.On("ContainerList", mock.Anything, mock.Anything).Return([]types.Container{
{
ID: "containerid",
Names: []string{"Container 1"},
Image: "my_image",
ImageID: "my_image_id",
Labels: givenLabels,
},
}, nil)

mStore := &mockStorer{}
mStore.On("Save").Return(nil)

sampler := ContainerSampler{
metrics: biz.NewProcessor(mStore, nil, mocker),
docker: mocker,
store: mStore,
}

i, err := integration.New("test", "test-version")
assert.NoError(t, err)
assert.NoError(t, sampler.SampleAll(i))

for expectedName, expectedValue := range expectedLabels {
value, ok := i.Entities[0].Metrics[0].Metrics[expectedName]
if !ok {
t.Errorf("Expected label '%s=%s' not found.", expectedName, expectedValue)
}

if value != expectedValue {
t.Errorf("Label %s has value of %v, expected %s", expectedName, value, expectedValue)
}
}
}
42 changes: 42 additions & 0 deletions vendor/github.com/docker/docker/api/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/docker/docker/api/common.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/docker/docker/api/common_unix.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/docker/docker/api/common_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/docker/docker/api/swagger-gen.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading