github.com/zchee/zap-cloudlogging@v0.0.0-20220819025602-19b026d3900e/pkg/detector/detector.go (about)

     1  // Copyright 2022 The zap-cloudlogging Authors
     2  // SPDX-License-Identifier: BSD-3-Clause
     3  
     4  package detector
     5  
     6  // Platform represents a GCP service platforms.
     7  type Platform uint8
     8  
     9  const (
    10  	// UnknownPlatform is the unknown platform.
    11  	UnknownPlatform Platform = iota
    12  
    13  	// GKE is the Kubernetes Engine platform.
    14  	// TODO(zchee): not implemented yet.
    15  	GKE
    16  
    17  	// GCE is the Google Compute Engine platform.
    18  	// TODO(zchee): not implemented yet.
    19  	GCE
    20  
    21  	// CloudRun is the Cloud Run platform.
    22  	CloudRun
    23  
    24  	// CloudRunJobs is the Cloud Run jobs platform.
    25  	CloudRunJobs
    26  
    27  	// CloudFunctions is the Cloud Functions platform.
    28  	CloudFunctions
    29  
    30  	// AppEngineStandard is the App Engine Standard 2nd platform.
    31  	AppEngineStandard
    32  
    33  	// AppEngineFlex is the App Engine Flex platform.
    34  	AppEngineFlex
    35  )
    36  
    37  // Detector collects resource information for all GCP platforms.
    38  type Detector struct {
    39  	attrs ResourceAttributesFetcher
    40  }
    41  
    42  // NewDetector returns the new Detector based by attrs.
    43  func NewDetector(attrs ResourceAttributesFetcher) *Detector {
    44  	return &Detector{
    45  		attrs: attrs,
    46  	}
    47  }
    48  
    49  // CloudPlatform returns the platform on which this program is running.
    50  func (d *Detector) CloudPlatform() Platform {
    51  	d.attrs = fetcher
    52  
    53  	switch {
    54  	case d.isGCE(): // TODO(zchee): not implemented yet.
    55  		return UnknownPlatform // GCE
    56  
    57  	case d.isGKE(): // TODO(zchee): not implemented yet.
    58  		return UnknownPlatform // GKE
    59  
    60  	case d.isCloudRun():
    61  		return CloudRun
    62  
    63  	case d.isCloudRunJobs():
    64  		return CloudRunJobs
    65  
    66  	case d.isCloudFunctions():
    67  		return CloudFunctions
    68  
    69  	case d.isAppEngineStandard():
    70  		return AppEngineStandard
    71  
    72  	case d.isAppEngineFlex():
    73  		return AppEngineFlex
    74  	}
    75  
    76  	return UnknownPlatform
    77  }
    78  
    79  // MetadataProvider contains the subset of the metadata.Client functions used
    80  // by this resource Detector to allow testing with a fake implementation.
    81  type MetadataProvider interface {
    82  	ProjectID() (string, error)
    83  	InstanceID() (string, error)
    84  	Get(string) (string, error)
    85  	InstanceName() (string, error)
    86  	Zone() (string, error)
    87  	InstanceAttributeValue(string) (string, error)
    88  }
    89  
    90  // OSProvider contains the subset of the os package functions used by.
    91  type OSProvider interface {
    92  	LookupEnv(string) (string, bool)
    93  }