github.com/google/cloudprober@v0.11.3/surfacers/stackdriver/resource.go (about)

     1  // Copyright 2021 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stackdriver
    16  
    17  import (
    18  	"os"
    19  
    20  	"cloud.google.com/go/compute/metadata"
    21  	md "github.com/google/cloudprober/common/metadata"
    22  	monitoring "google.golang.org/api/monitoring/v3"
    23  )
    24  
    25  func kubernetesResource(projectID string) (*monitoring.MonitoredResource, error) {
    26  	namespace := md.KubernetesNamespace()
    27  
    28  	// We ignore error in getting cluster name and location. These attributes
    29  	// should be set while running on GKE.
    30  	clusterName, _ := metadata.InstanceAttributeValue("cluster-name")
    31  	location, _ := metadata.InstanceAttributeValue("cluster-location")
    32  
    33  	// We can likely use cluster-location instance attribute for location. Using
    34  	// zone provides more granular scope though.
    35  	return &monitoring.MonitoredResource{
    36  		Type: "k8s_container",
    37  		Labels: map[string]string{
    38  			"cluster_name":   clusterName,
    39  			"location":       location,
    40  			"project_id":     projectID,
    41  			"pod_name":       os.Getenv("HOSTNAME"),
    42  			"namespace_name": namespace,
    43  			// To get the `container_name` label, users need to explicitly provide it.
    44  			"container_name": os.Getenv("CONTAINER_NAME"),
    45  		},
    46  	}, nil
    47  }
    48  
    49  func gceResource(projectID string) (*monitoring.MonitoredResource, error) {
    50  	name, err := metadata.InstanceName()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	zone, err := metadata.Zone()
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return &monitoring.MonitoredResource{
    61  		Type: "gce_instance",
    62  		Labels: map[string]string{
    63  			"project_id": projectID,
    64  			// Note that following is not correct, as we are adding instance name as
    65  			// instance id, but this is what we have been doing all along, and for
    66  			// monitoring instance name may be more useful, at least as of now there
    67  			// is no automatic instance to custom-monitoring mapping.
    68  			"instance_id": name,
    69  			"zone":        zone,
    70  		},
    71  	}, nil
    72  }
    73  
    74  func monitoredResourceOnGCE(projectID string) (*monitoring.MonitoredResource, error) {
    75  	if md.IsKubernetes() {
    76  		return kubernetesResource(projectID)
    77  	}
    78  	return gceResource(projectID)
    79  }