-
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.
[exporter/datadog] Add Azure, GCP and Docker hostname providers (#10819)
* [exporter/datadog] Add 'gcp' hostname provider * [exporter/datadog] Add 'docker' hostname provider * [exporter/datadog] Add 'azure' hostname provider * Apply suggestions from code review Co-authored-by: Julien Lebot <[email protected]> Co-authored-by: Julien Lebot <[email protected]>
- Loading branch information
1 parent
d0563b1
commit e90064f
Showing
8 changed files
with
542 additions
and
12 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
44 changes: 44 additions & 0 deletions
44
exporter/datadogexporter/internal/metadata/internal/azure/provider.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,44 @@ | ||
// 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 "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/internal/azure" | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/provider" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/azure" | ||
) | ||
|
||
var _ provider.HostnameProvider = (*Provider)(nil) | ||
|
||
type Provider struct { | ||
detector azure.Provider | ||
} | ||
|
||
// Hostname returns the Azure cloud integration hostname. | ||
func (p *Provider) Hostname(ctx context.Context) (string, error) { | ||
metadata, err := p.detector.Metadata(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return metadata.VMID, nil | ||
} | ||
|
||
// NewProvider creates a new Azure hostname provider. | ||
func NewProvider() *Provider { | ||
return &Provider{detector: azure.NewProvider()} | ||
} |
43 changes: 43 additions & 0 deletions
43
exporter/datadogexporter/internal/metadata/internal/azure/provider_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,43 @@ | ||
// 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" | ||
"testing" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/azure" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProvider(t *testing.T) { | ||
mp := &azure.MockProvider{} | ||
mp.On("Metadata").Return(&azure.ComputeMetadata{ | ||
Location: "location", | ||
Name: "name", | ||
VMID: "vmID", | ||
VMSize: "vmSize", | ||
SubscriptionID: "subscriptionID", | ||
ResourceGroupName: "resourceGroup", | ||
VMScaleSetName: "myScaleset", | ||
}, nil) | ||
|
||
provider := &Provider{detector: mp} | ||
hostname, err := provider.Hostname(context.Background()) | ||
require.NoError(t, err) | ||
assert.Equal(t, "vmID", hostname) | ||
} |
42 changes: 42 additions & 0 deletions
42
exporter/datadogexporter/internal/metadata/internal/docker/provider.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,42 @@ | ||
// 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 docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/internal/docker" | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/provider" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/docker" | ||
) | ||
|
||
var _ provider.HostnameProvider = (*Provider)(nil) | ||
|
||
type Provider struct { | ||
detector docker.Provider | ||
} | ||
|
||
// Hostname returns the hostname from the Docker socket. | ||
func (p *Provider) Hostname(ctx context.Context) (string, error) { | ||
return p.detector.Hostname(ctx) | ||
} | ||
|
||
// NewProvider creates a new Docker hostname provider. | ||
func NewProvider() (*Provider, error) { | ||
detector, err := docker.NewProvider() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Provider{detector: detector}, nil | ||
} |
68 changes: 68 additions & 0 deletions
68
exporter/datadogexporter/internal/metadata/internal/gcp/provider.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,68 @@ | ||
// 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 gcp // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/internal/gcp" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/provider" | ||
|
||
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp" | ||
) | ||
|
||
var _ provider.HostnameProvider = (*Provider)(nil) | ||
|
||
var _ gcpDetector = gcp.NewDetector() | ||
|
||
type gcpDetector interface { | ||
ProjectID() (string, error) | ||
CloudPlatform() gcp.Platform | ||
GCEHostName() (string, error) | ||
} | ||
|
||
type Provider struct { | ||
detector gcpDetector | ||
} | ||
|
||
// Hostname returns the GCP cloud integration hostname. | ||
func (p *Provider) Hostname(context.Context) (string, error) { | ||
if p.detector.CloudPlatform() != gcp.GCE { | ||
return "", fmt.Errorf("not on Google Cloud Engine") | ||
} | ||
|
||
name, err := p.detector.GCEHostName() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get instance name: %w", err) | ||
} | ||
|
||
// Use the same logic as in the metadata from attributes logic. | ||
if strings.Count(name, ".") >= 3 { | ||
name = strings.SplitN(name, ".", 2)[0] | ||
} | ||
|
||
cloudAccount, err := p.detector.ProjectID() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get project ID: %w", err) | ||
} | ||
|
||
return fmt.Sprintf("%s.%s", name, cloudAccount), nil | ||
} | ||
|
||
// NewProvider creates a new GCP hostname provider. | ||
func NewProvider() *Provider { | ||
return &Provider{detector: gcp.NewDetector()} | ||
} |
Oops, something went wrong.