-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[datadogexporter] Send host metadata (#1351)
* Add EC2 metadata * Add option to disable sending metadata Set it to false in tests to avoid creating goroutines * Launch host metadata goroutine only once * Add tests for EC2 hostname resolution * Address linter issues * [empty] Retrigger CI * Do not send tags with metrics or traces The backend will add these tags since they are sent with metadata * Add env to host tags Reusing the `GetTags` function for this since this PR leaves it without use * Fix indentation on go.mod * Apply suggestions from code review Fix documentation comments Co-authored-by: Kylian Serrania <kylian.serrania@datadoghq.com> * Use params.ApplicationStartInfo for getting Flavor and Version * [empty] Retrigger CI * [empty] Retrigger CI again * [empty] Retrigger CI (third attempt) * [empty] Retrigger CI (4) * Fix after merge * Improve test coverage Co-authored-by: Kylian Serrania <kylian.serrania@datadoghq.com>
- Loading branch information
Showing
21 changed files
with
599 additions
and
146 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
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
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 ec2 | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var defaultPrefixes = [3]string{"ip-", "domu", "ec2amaz-"} | ||
|
||
type HostInfo struct { | ||
InstanceID string | ||
EC2Hostname string | ||
} | ||
|
||
// isDefaultHostname checks if a hostname is an EC2 default | ||
func isDefaultHostname(hostname string) bool { | ||
for _, val := range defaultPrefixes { | ||
if strings.HasPrefix(hostname, val) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// GetHostInfo gets the hostname info from EC2 metadata | ||
func GetHostInfo(logger *zap.Logger) (hostInfo *HostInfo) { | ||
sess, err := session.NewSession() | ||
hostInfo = &HostInfo{} | ||
|
||
if err != nil { | ||
logger.Warn("Failed to build AWS session", zap.Error(err)) | ||
return | ||
} | ||
|
||
meta := ec2metadata.New(sess) | ||
|
||
if !meta.Available() { | ||
logger.Info("EC2 Metadata not available") | ||
return | ||
} | ||
|
||
if idDoc, err := meta.GetInstanceIdentityDocument(); err == nil { | ||
hostInfo.InstanceID = idDoc.InstanceID | ||
} else { | ||
logger.Warn("Failed to get EC2 instance id document", zap.Error(err)) | ||
} | ||
|
||
if ec2Hostname, err := meta.GetMetadata("hostname"); err == nil { | ||
hostInfo.EC2Hostname = ec2Hostname | ||
} else { | ||
logger.Warn("Failed to get EC2 hostname", zap.Error(err)) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (hi *HostInfo) GetHostname(logger *zap.Logger) string { | ||
if isDefaultHostname(hi.EC2Hostname) { | ||
return hi.InstanceID | ||
} | ||
|
||
return hi.EC2Hostname | ||
} |
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,52 @@ | ||
// 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 ec2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
testIP = "ip-12-34-56-78.us-west-2.compute.internal" | ||
testDomu = "domu-12-34-56-78.us-west-2.compute.internal" | ||
testEC2 = "ec2amaz-12-34-56-78.us-west-2.compute.internal" | ||
customHost = "custom-hostname" | ||
testInstanceID = "i-0123456789" | ||
) | ||
|
||
func TestDefaultHostname(t *testing.T) { | ||
assert.True(t, isDefaultHostname(testIP)) | ||
assert.True(t, isDefaultHostname(testDomu)) | ||
assert.True(t, isDefaultHostname(testEC2)) | ||
assert.False(t, isDefaultHostname(customHost)) | ||
} | ||
|
||
func TestGetHostname(t *testing.T) { | ||
logger := zap.NewNop() | ||
|
||
hostInfo := &HostInfo{ | ||
InstanceID: testInstanceID, | ||
EC2Hostname: testIP, | ||
} | ||
assert.Equal(t, testInstanceID, hostInfo.GetHostname(logger)) | ||
|
||
hostInfo = &HostInfo{ | ||
InstanceID: testInstanceID, | ||
EC2Hostname: customHost, | ||
} | ||
assert.Equal(t, customHost, hostInfo.GetHostname(logger)) | ||
} |
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.