make METRICS_DOMAIN optional when backend is not OpenCensus (#2617)

This commit is contained in:
Dave Protasowski 2022-10-25 14:42:56 -04:00 committed by GitHub
parent b2a3a39d92
commit ff956846a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 22 deletions

View File

@ -124,12 +124,7 @@ func (mc *metricsConfig) record(ctx context.Context, mss []stats.Measurement, ro
func createMetricsConfig(_ context.Context, ops ExporterOptions) (*metricsConfig, error) { func createMetricsConfig(_ context.Context, ops ExporterOptions) (*metricsConfig, error) {
var mc metricsConfig var mc metricsConfig
if ops.Domain == "" {
return nil, errors.New("metrics domain cannot be empty")
}
mc.domain = ops.Domain mc.domain = ops.Domain
if ops.Component == "" { if ops.Component == "" {
return nil, errors.New("metrics component name cannot be empty") return nil, errors.New("metrics component name cannot be empty")
} }
@ -159,6 +154,9 @@ func createMetricsConfig(_ context.Context, ops ExporterOptions) (*metricsConfig
switch mc.backendDestination { switch mc.backendDestination {
case openCensus: case openCensus:
if ops.Domain == "" {
return nil, errors.New("metrics domain cannot be empty")
}
mc.collectorAddress = ops.ConfigMap[collectorAddressKey] mc.collectorAddress = ops.ConfigMap[collectorAddressKey]
if isSecure := ops.ConfigMap[collectorSecureKey]; isSecure != "" { if isSecure := ops.ConfigMap[collectorSecureKey]; isSecure != "" {
var err error var err error
@ -221,22 +219,7 @@ func Domain() string {
if domain := os.Getenv(DomainEnv); domain != "" { if domain := os.Getenv(DomainEnv); domain != "" {
return domain return domain
} }
return ""
panic(fmt.Sprintf(`The environment variable %q is not set
If this is a process running on Kubernetes, then it should be specifying
this via:
env:
- name: %s
value: knative.dev/some-repository
If this is a Go unit test consuming metric.Domain() then it should add the
following import:
import (
_ "knative.dev/pkg/metrics/testing"
)`, DomainEnv, DomainEnv))
} }
// prometheusPort returns the TCP port number configured via the environment // prometheusPort returns the TCP port number configured via the environment

View File

@ -61,7 +61,7 @@ var (
name: "emptyDomain", name: "emptyDomain",
ops: ExporterOptions{ ops: ExporterOptions{
ConfigMap: map[string]string{ ConfigMap: map[string]string{
BackendDestinationKey: string(prometheus), BackendDestinationKey: string(openCensus),
}, },
Component: testComponent, Component: testComponent,
}, },