forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Elastic Beanstalk Resource detector (#1585)
* initial eb resource detector commit * lint * addressed comments * tests
- Loading branch information
1 parent
313ffc4
commit 51555af
Showing
7 changed files
with
281 additions
and
10 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
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
83 changes: 83 additions & 0 deletions
83
processor/resourcedetectionprocessor/internal/aws/elasticbeanstalk/elasticbeanstalk.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,83 @@ | ||
// Copyright -c 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:https://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 elasticbeanstalk | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strconv" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/translator/conventions" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" | ||
) | ||
|
||
const ( | ||
TypeStr = "elastic_beanstalk" | ||
linuxPath = "/var/elasticbeanstalk/xray/environment.conf" | ||
windowsPath = "C:\\Program Files\\Amazon\\XRay\\environment.conf" | ||
) | ||
|
||
var _ internal.Detector = (*Detector)(nil) | ||
|
||
type Detector struct { | ||
fs readOnlyFileSystem | ||
} | ||
|
||
type EbMetaData struct { | ||
DeploymentID int `json:"deployment_id"` | ||
EnvironmentName string `json:"environment_name"` | ||
VersionLabel string `json:"version_label"` | ||
} | ||
|
||
func NewDetector() (internal.Detector, error) { | ||
return &Detector{fs: &ebReadOnlyFileSystem{}}, nil | ||
} | ||
|
||
func (d Detector) Detect(context.Context) (pdata.Resource, error) { | ||
res := pdata.NewResource() | ||
var conf file | ||
var err error | ||
|
||
if d.fs.IsWindows() { | ||
conf, err = d.fs.Open(windowsPath) | ||
} else { | ||
conf, err = d.fs.Open(linuxPath) | ||
} | ||
|
||
// Do not want to return error so it fails silently on non-EB instances | ||
if err != nil { | ||
// TODO: Log a more specific message with zap | ||
return res, nil | ||
} | ||
|
||
ebmd := &EbMetaData{} | ||
err = json.NewDecoder(conf).Decode(ebmd) | ||
conf.Close() | ||
|
||
if err != nil { | ||
// TODO: Log a more specific error with zap | ||
return res, err | ||
} | ||
|
||
attr := res.Attributes() | ||
attr.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS) | ||
attr.InsertString("cloud.infrastructure_service", "ElasticBeanstalk") | ||
attr.InsertString(conventions.AttributeServiceInstance, strconv.Itoa(ebmd.DeploymentID)) | ||
attr.InsertString(conventions.AttributeDeploymentEnvironment, ebmd.EnvironmentName) | ||
attr.InsertString(conventions.AttributeServiceVersion, ebmd.VersionLabel) | ||
return res, nil | ||
} |
121 changes: 121 additions & 0 deletions
121
processor/resourcedetectionprocessor/internal/aws/elasticbeanstalk/elasticbeanstalk_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,121 @@ | ||
// Copyright -c 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:https://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 elasticbeanstalk | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" | ||
) | ||
|
||
const xrayConf = "{\"deployment_id\":23,\"version_label\":\"env-version-1234\",\"environment_name\":\"BETA\"}" | ||
|
||
type fakeFile struct { | ||
reader io.Reader | ||
} | ||
|
||
type mockFileSystem struct { | ||
windows bool | ||
exists bool | ||
path string | ||
contents string | ||
} | ||
|
||
func (f fakeFile) Close() error { | ||
return nil | ||
} | ||
|
||
func (f fakeFile) Read(p []byte) (n int, err error) { | ||
return f.reader.Read(p) | ||
} | ||
|
||
func (mfs *mockFileSystem) Open(path string) (file, error) { | ||
if !mfs.exists { | ||
return nil, errors.New("file not found") | ||
} | ||
mfs.path = path | ||
f := fakeFile{reader: strings.NewReader(mfs.contents)} | ||
return f, nil | ||
} | ||
|
||
func (mfs *mockFileSystem) IsWindows() bool { | ||
return mfs.windows | ||
} | ||
|
||
func Test_newDetector(t *testing.T) { | ||
d, err := NewDetector() | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, d) | ||
} | ||
|
||
func Test_windowsPath(t *testing.T) { | ||
mfs := &mockFileSystem{windows: true, exists: true, contents: xrayConf} | ||
d := Detector{fs: mfs} | ||
|
||
r, err := d.Detect(context.TODO()) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
assert.Equal(t, windowsPath, mfs.path) | ||
} | ||
|
||
func Test_fileNotExists(t *testing.T) { | ||
mfs := &mockFileSystem{exists: false} | ||
d := Detector{fs: mfs} | ||
|
||
r, err := d.Detect(context.TODO()) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
assert.Equal(t, 0, r.Attributes().Len()) | ||
} | ||
|
||
func Test_fileMalformed(t *testing.T) { | ||
mfs := &mockFileSystem{exists: true, contents: "some overwritten value"} | ||
d := Detector{fs: mfs} | ||
|
||
r, err := d.Detect(context.TODO()) | ||
|
||
assert.NotNil(t, err) | ||
assert.NotNil(t, r) | ||
assert.Equal(t, 0, r.Attributes().Len()) | ||
} | ||
|
||
func Test_AttributesDetectedSuccessfully(t *testing.T) { | ||
mfs := &mockFileSystem{exists: true, contents: xrayConf} | ||
d := Detector{fs: mfs} | ||
|
||
want := pdata.NewResource() | ||
attr := want.Attributes() | ||
attr.InsertString("cloud.provider", "aws") | ||
attr.InsertString("cloud.infrastructure_service", "ElasticBeanstalk") | ||
attr.InsertString("deployment.environment", "BETA") | ||
attr.InsertString("service.instance.id", "23") | ||
attr.InsertString("service.version", "env-version-1234") | ||
|
||
r, err := d.Detect(context.TODO()) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, r) | ||
assert.Equal(t, internal.AttributesToMap(want.Attributes()), internal.AttributesToMap(r.Attributes())) | ||
} |
27 changes: 27 additions & 0 deletions
27
processor/resourcedetectionprocessor/internal/aws/elasticbeanstalk/filesystem.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,27 @@ | ||
// Copyright -c 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:https://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 elasticbeanstalk | ||
|
||
import "io" | ||
|
||
type readOnlyFileSystem interface { | ||
Open(string) (file, error) | ||
IsWindows() bool | ||
} | ||
|
||
type file interface { | ||
io.Closer | ||
io.Reader | ||
} |
31 changes: 31 additions & 0 deletions
31
processor/resourcedetectionprocessor/internal/aws/elasticbeanstalk/filesystem_eb.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,31 @@ | ||
// Copyright -c 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:https://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 elasticbeanstalk | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
) | ||
|
||
type ebReadOnlyFileSystem struct { | ||
} | ||
|
||
func (fs ebReadOnlyFileSystem) Open(path string) (file, error) { | ||
return os.Open(path) | ||
} | ||
|
||
func (fs ebReadOnlyFileSystem) IsWindows() bool { | ||
return runtime.GOOS == "windows" | ||
} |