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

Add support for the BMS monitored resource to the Prometheus receiver #1471

Merged
merged 9 commits into from Dec 5, 2023
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PKG_VERSION="2.44.0-bms"
PKG_VERSION="2.44.0-bms"
2 changes: 1 addition & 1 deletion cmd/google_cloud_ops_agent_diagnostics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getUserAndMergedConfigs(ctx context.Context, userConfPath string) (*confgen
func main() {
defer func() {
if r := recover(); r != nil {
log.Fatal("Recovered in run", r)
log.Fatalf("Recovering from a panic due to %v", r)
}
}()
if err := run(context.Background()); err != nil {
Expand Down
60 changes: 9 additions & 51 deletions confgenerator/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (

"github.com/GoogleCloudPlatform/ops-agent/confgenerator/otel"
"github.com/GoogleCloudPlatform/ops-agent/confgenerator/resourcedetector"
"github.com/GoogleCloudPlatform/ops-agent/internal/platform"
"github.com/go-playground/validator/v10"
yaml "github.com/goccy/go-yaml"
commonconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promconfig "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
_ "github.com/prometheus/prometheus/discovery/install" // init() of this package registers service discovery impl.
strutil "github.com/prometheus/prometheus/util/strutil"
)

var (
Expand All @@ -60,11 +60,13 @@ func (r PrometheusMetrics) Type() string {
return "prometheus"
}

func (r PrometheusMetrics) Pipelines(_ context.Context) []otel.ReceiverPipeline {
func (r PrometheusMetrics) Pipelines(ctx context.Context) []otel.ReceiverPipeline {
// Get the resource metadata for the instance we're running on.
if gceMetadata, ok := MetadataResource.(resourcedetector.GCEResource); ok {
// Create a prometheus style mapping for the GCE metadata.
gceMetadataMap := createPrometheusStyleGCEMetadata(gceMetadata)
if MetadataResource != nil {
resourceMetadataMap := MetadataResource.PrometheusStyleMetadata()
if p := platform.FromContext(ctx).ResourceOverride; p != nil {
This conversation was marked as resolved.
Show resolved Hide resolved
resourceMetadataMap = p.PrometheusStyleMetadata()
}

// Add the GCE metadata to the prometheus config.
This conversation was marked as resolved.
Show resolved Hide resolved
for i := range r.PromConfig.ScrapeConfigs {
Expand All @@ -76,8 +78,8 @@ func (r PrometheusMetrics) Pipelines(_ context.Context) []otel.ReceiverPipeline
if labels == nil {
labels = model.LabelSet{}
}
for k, v := range gceMetadataMap {
// If there are conflicts, the GCE metadata should take precedence.
for k, v := range resourceMetadataMap {
// If there are conflicts, the resource metadata should take precedence.
labels[model.LabelName(k)] = model.LabelValue(v)
}

Expand Down Expand Up @@ -147,50 +149,6 @@ func deepCopy(config promconfig.Config) (promconfig.Config, error) {
return copyConfig, nil
}

func createPrometheusStyleGCEMetadata(gceMetadata resourcedetector.GCEResource) map[string]string {
metaLabels := map[string]string{
"__meta_gce_instance_id": gceMetadata.InstanceID,
"__meta_gce_instance_name": gceMetadata.InstanceName,
"__meta_gce_project": gceMetadata.Project,
"__meta_gce_zone": gceMetadata.Zone,
"__meta_gce_network": gceMetadata.Network,
// TODO(b/b/246995894): Add support for subnetwork label.
// "__meta_gce_subnetwork": gceMetadata.Subnetwork,
"__meta_gce_public_ip": gceMetadata.PublicIP,
"__meta_gce_private_ip": gceMetadata.PrivateIP,
"__meta_gce_tags": gceMetadata.Tags,
"__meta_gce_machine_type": gceMetadata.MachineType,
}
prefix := "__meta_gce_"
for k, v := range gceMetadata.Metadata {
sanitizedKey := "metadata_" + strutil.SanitizeLabelName(k)
metaLabels[prefix+sanitizedKey] = strings.ReplaceAll(v, "$", "$$")
}

// Labels are not available using the GCE metadata API.
// TODO(b/246995462): Add support for labels.
//
// for k, v := range gceMetadata.Label {
// metaLabels[prefix+"label_"+k] = v
// }

for k, v := range gceMetadata.InterfaceIPv4 {
sanitizedKey := "interface_ipv4_nic" + strutil.SanitizeLabelName(k)
metaLabels[prefix+sanitizedKey] = v
}

// Set the location, namespace and cluster labels.
metaLabels["location"] = gceMetadata.Zone
metaLabels["namespace"] = gceMetadata.InstanceID
metaLabels["cluster"] = "__gce__"

// Set some curated labels.
metaLabels["instance_name"] = gceMetadata.InstanceName
metaLabels["machine_type"] = gceMetadata.MachineType

return metaLabels
}

func validatePrometheusConfig(sl validator.StructLevel) {
promConfig := sl.Current().Interface().(promconfig.Config)

Expand Down
15 changes: 15 additions & 0 deletions confgenerator/resourcedetector/bms_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ func (r BMSResource) MonitoredResource() *monitoredres.MonitoredResource {
}
}

func (r BMSResource) PrometheusStyleMetadata() map[string]string {
metaLabels := map[string]string{
"__meta_bms_instance_id": r.InstanceID,
"__meta_bms_project": r.Project,
"__meta_bms_location": r.Location,
}
// Set the location, namespace and cluster labels.
metaLabels["location"] = r.Location
metaLabels["namespace"] = r.InstanceID
metaLabels["cluster"] = "__bms__"
// Set some curated labels.
metaLabels["instance_name"] = r.InstanceID
This conversation was marked as resolved.
Show resolved Hide resolved
return metaLabels
}

func OnBMS() bool {
return os.Getenv(bmsProjectIDEnv) != "" && os.Getenv(bmsLocationEnv) != "" && os.Getenv(bmsInstanceIDEnv) != ""
}
Expand Down
5 changes: 5 additions & 0 deletions confgenerator/resourcedetector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Resource interface {
MonitoredResource() *monitoredres.MonitoredResource
OTelResourceAttributes() map[string]string
ProjectName() string
PrometheusStyleMetadata() map[string]string
}

// Get a resource instance for the current environment;
Expand Down Expand Up @@ -59,6 +60,10 @@ func (UnrecognizedPlatformResource) MonitoredResource() *monitoredres.MonitoredR
return nil
}

func (UnrecognizedPlatformResource) PrometheusStyleMetadata() map[string]string {
return nil
}

func GetUnrecognizedPlatformResource() (Resource, error) {
return UnrecognizedPlatformResource{}, nil
}
51 changes: 50 additions & 1 deletion confgenerator/resourcedetector/gce_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package resourcedetector

import "google.golang.org/genproto/googleapis/api/monitoredres"
import (
"strings"

"github.com/prometheus/prometheus/util/strutil"
"google.golang.org/genproto/googleapis/api/monitoredres"
)

type gceAttribute int

Expand Down Expand Up @@ -128,6 +133,50 @@ func (r GCEResource) MonitoredResource() *monitoredres.MonitoredResource {
}
}

func (r GCEResource) PrometheusStyleMetadata() map[string]string {
quentinmit marked this conversation as resolved.
Show resolved Hide resolved
metaLabels := map[string]string{
"__meta_gce_instance_id": r.InstanceID,
"__meta_gce_instance_name": r.InstanceName,
"__meta_gce_project": r.Project,
"__meta_gce_zone": r.Zone,
"__meta_gce_network": r.Network,
// TODO(b/b/246995894): Add support for subnetwork label.
igorpeshansky marked this conversation as resolved.
Show resolved Hide resolved
// "__meta_gce_subnetwork": r.Subnetwork,
"__meta_gce_public_ip": r.PublicIP,
"__meta_gce_private_ip": r.PrivateIP,
"__meta_gce_tags": r.Tags,
"__meta_gce_machine_type": r.MachineType,
}
prefix := "__meta_gce_"
for k, v := range r.Metadata {
sanitizedKey := "metadata_" + strutil.SanitizeLabelName(k)
metaLabels[prefix+sanitizedKey] = strings.ReplaceAll(v, "$", "$$")
}

// Labels are not available using the GCE metadata API.
// TODO(b/246995462): Add support for labels.
//
// for k, v := range r.Label {
// metaLabels[prefix+"label_"+k] = v
// }

for k, v := range r.InterfaceIPv4 {
sanitizedKey := "interface_ipv4_nic" + strutil.SanitizeLabelName(k)
metaLabels[prefix+sanitizedKey] = v
}

// Set the location, namespace and cluster labels.
metaLabels["location"] = r.Zone
metaLabels["namespace"] = r.InstanceID
metaLabels["cluster"] = "__gce__"

// Set some curated labels.
metaLabels["instance_name"] = r.InstanceName
metaLabels["machine_type"] = r.MachineType

return metaLabels
}

type GCEResourceBuilderInterface interface {
GetResource() (Resource, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,23 +501,13 @@ receivers:
- targets:
- localhost:1234
labels:
__meta_gce_instance_id: test-instance-id
__meta_gce_instance_name: test-instance-name
__meta_gce_interface_ipv4_nictest_interface: test-interface-ipv4
__meta_gce_machine_type: test-machine-type
__meta_gce_metadata_test_escape: $${foo:bar}
__meta_gce_metadata_test_key: test-value
__meta_gce_network: test-network
__meta_gce_private_ip: test-private-ip
__meta_gce_project: test-project
__meta_gce_public_ip: test-public-ip
__meta_gce_tags: test-tag
__meta_gce_zone: test-zone
cluster: __gce__
instance_name: test-instance-name
location: test-zone
machine_type: test-machine-type
namespace: test-instance-id
__meta_bms_instance_id: test-bms-instance
__meta_bms_location: test-bms-location
__meta_bms_project: test-bms-project
cluster: __bms__
instance_name: test-bms-instance
location: test-bms-location
namespace: test-bms-instance
preserve_untyped: true
service:
pipelines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,23 +494,13 @@ receivers:
- targets:
- localhost:1234
labels:
__meta_gce_instance_id: test-instance-id
__meta_gce_instance_name: test-instance-name
__meta_gce_interface_ipv4_nictest_interface: test-interface-ipv4
__meta_gce_machine_type: test-machine-type
__meta_gce_metadata_test_escape: $${foo:bar}
__meta_gce_metadata_test_key: test-value
__meta_gce_network: test-network
__meta_gce_private_ip: test-private-ip
__meta_gce_project: test-project
__meta_gce_public_ip: test-public-ip
__meta_gce_tags: test-tag
__meta_gce_zone: test-zone
cluster: __gce__
instance_name: test-instance-name
location: test-zone
machine_type: test-machine-type
namespace: test-instance-id
__meta_bms_instance_id: test-bms-instance
__meta_bms_location: test-bms-location
__meta_bms_project: test-bms-project
cluster: __bms__
instance_name: test-bms-instance
location: test-bms-location
namespace: test-bms-instance
- job_name: drop
honor_timestamps: true
scrape_interval: 10s
Expand Down Expand Up @@ -553,23 +543,13 @@ receivers:
- targets:
- 0.0.0.0:9100
labels:
__meta_gce_instance_id: test-instance-id
__meta_gce_instance_name: test-instance-name
__meta_gce_interface_ipv4_nictest_interface: test-interface-ipv4
__meta_gce_machine_type: test-machine-type
__meta_gce_metadata_test_escape: $${foo:bar}
__meta_gce_metadata_test_key: test-value
__meta_gce_network: test-network
__meta_gce_private_ip: test-private-ip
__meta_gce_project: test-project
__meta_gce_public_ip: test-public-ip
__meta_gce_tags: test-tag
__meta_gce_zone: test-zone
cluster: __gce__
instance_name: test-instance-name
location: test-zone
machine_type: test-machine-type
namespace: test-instance-id
__meta_bms_instance_id: test-bms-instance
__meta_bms_location: test-bms-location
__meta_bms_project: test-bms-project
cluster: __bms__
instance_name: test-bms-instance
location: test-bms-location
namespace: test-bms-instance
preserve_untyped: true
service:
pipelines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,23 +501,13 @@ receivers:
- targets:
- localhost:1234
labels:
__meta_gce_instance_id: test-instance-id
__meta_gce_instance_name: test-instance-name
__meta_gce_interface_ipv4_nictest_interface: test-interface-ipv4
__meta_gce_machine_type: test-machine-type
__meta_gce_metadata_test_escape: $${foo:bar}
__meta_gce_metadata_test_key: test-value
__meta_gce_network: test-network
__meta_gce_private_ip: test-private-ip
__meta_gce_project: test-project
__meta_gce_public_ip: test-public-ip
__meta_gce_tags: test-tag
__meta_gce_zone: test-zone
cluster: __gce__
instance_name: test-instance-name
location: test-zone
machine_type: test-machine-type
namespace: test-instance-id
__meta_bms_instance_id: test-bms-instance
__meta_bms_location: test-bms-location
__meta_bms_project: test-bms-project
cluster: __bms__
instance_name: test-bms-instance
location: test-bms-location
namespace: test-bms-instance
preserve_untyped: true
service:
pipelines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,23 +501,13 @@ receivers:
- targets:
- localhost:1234
labels:
__meta_gce_instance_id: test-instance-id
__meta_gce_instance_name: test-instance-name
__meta_gce_interface_ipv4_nictest_interface: test-interface-ipv4
__meta_gce_machine_type: test-machine-type
__meta_gce_metadata_test_escape: $${foo:bar}
__meta_gce_metadata_test_key: test-value
__meta_gce_network: test-network
__meta_gce_private_ip: test-private-ip
__meta_gce_project: test-project
__meta_gce_public_ip: test-public-ip
__meta_gce_tags: test-tag
__meta_gce_zone: test-zone
cluster: __gce__
instance_name: test-instance-name
location: test-zone
machine_type: test-machine-type
namespace: test-instance-id
__meta_bms_instance_id: test-bms-instance
__meta_bms_location: test-bms-location
__meta_bms_project: test-bms-project
cluster: __bms__
instance_name: test-bms-instance
location: test-bms-location
namespace: test-bms-instance
preserve_untyped: true
service:
pipelines:
Expand Down
Loading
Loading