knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/metrics/config.go (about)

     1  /*
     2  Copyright 2025 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metrics
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	configmap "knative.dev/pkg/configmap/parser"
    24  )
    25  
    26  const (
    27  	ProtocolGRPC         = "grpc"
    28  	ProtocolHTTPProtobuf = "http/protobuf"
    29  	ProtocolPrometheus   = "prometheus"
    30  	ProtocolNone         = "none"
    31  )
    32  
    33  // Config provides a unified observability configuration which can be used to
    34  // manage Knative observability behavior.  Typically, this is extracted from a
    35  // Kubernetes ConfigMap during application startup, and accessed via the
    36  // GetConfig() method.
    37  type Config struct {
    38  	Protocol       string        `json:"protocol,omitempty"`
    39  	Endpoint       string        `json:"endpoint,omitempty"`
    40  	ExportInterval time.Duration `json:"exportInterval,omitempty"`
    41  }
    42  
    43  func (c *Config) Validate() error {
    44  	switch c.Protocol {
    45  	case ProtocolGRPC, ProtocolHTTPProtobuf:
    46  		if c.Endpoint == "" {
    47  			return fmt.Errorf("endpoint should be set when protocol is %q", c.Protocol)
    48  		}
    49  	case ProtocolNone:
    50  		if c.Endpoint != "" {
    51  			return fmt.Errorf("endpoint should not be set when protocol is %q", c.Protocol)
    52  		}
    53  	case ProtocolPrometheus:
    54  		// Endpoint is not required, but can be used to indicate listen port
    55  	default:
    56  		return fmt.Errorf("unsupported protocol %q", c.Protocol)
    57  	}
    58  
    59  	if c.ExportInterval < 0 {
    60  		return fmt.Errorf("export interval %q should be greater than zero", c.ExportInterval)
    61  	}
    62  	return nil
    63  }
    64  
    65  // DefaultConfig returns a configuration with default values set.
    66  func DefaultConfig() Config {
    67  	return Config{
    68  		Protocol: ProtocolNone,
    69  	}
    70  }
    71  
    72  // NewFromMap unpacks flat configuration values from a ConfigMap into
    73  // the configuration used by different observability modules.
    74  func NewFromMap(m map[string]string) (Config, error) {
    75  	return NewFromMapWithPrefix("", m)
    76  }
    77  
    78  func NewFromMapWithPrefix(prefix string, m map[string]string) (Config, error) {
    79  	c := DefaultConfig()
    80  
    81  	err := configmap.Parse(m,
    82  		configmap.As(prefix+"metrics-protocol", &c.Protocol),
    83  		configmap.As(prefix+"metrics-endpoint", &c.Endpoint),
    84  		configmap.As(prefix+"metrics-export-interval", &c.ExportInterval),
    85  	)
    86  	if err != nil {
    87  		return c, err
    88  	}
    89  
    90  	return c, c.Validate()
    91  }