knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/tracing/config_test.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 tracing 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 ) 24 25 func TestNewFromMap(t *testing.T) { 26 got, err := NewFromMap(nil) 27 want := DefaultConfig() 28 29 if err != nil { 30 t.Error("unexpected error:", err) 31 } 32 33 if diff := cmp.Diff(want, got); diff != "" { 34 t.Error("unexpected diff (-want +got): ", diff) 35 } 36 } 37 38 func TestNewFromMapBadInput(t *testing.T) { 39 cases := []struct { 40 name string 41 m map[string]string 42 }{{ 43 name: "unexpected endpoint set", 44 m: map[string]string{"tracing-endpoint": "https://blah.example.com"}, 45 }, { 46 name: "missing endpoint", 47 m: map[string]string{"tracing-protocol": "grpc"}, 48 }, { 49 name: "unsupported protocol", 50 m: map[string]string{"tracing-protocol": "bad-protocol"}, 51 }, { 52 name: "bad sampling rate - less than zero", 53 m: map[string]string{"tracing-sampling-rate": "-1"}, 54 }, { 55 name: "bad sampling rate - greater than one", 56 m: map[string]string{"tracing-sampling-rate": "1.1"}, 57 }, { 58 name: "bad sampling rate - not a float", 59 m: map[string]string{"tracing-sampling-rate": "bad-rate"}, 60 }} 61 62 for _, tc := range cases { 63 t.Run(tc.name, func(t *testing.T) { 64 if _, err := NewFromMap(tc.m); err == nil { 65 t.Error("expected an error") 66 } 67 }) 68 } 69 } 70 71 func TestNewFromMapWithPrefix(t *testing.T) { 72 got, err := NewFromMapWithPrefix("request-", map[string]string{ 73 "request-tracing-protocol": ProtocolGRPC, 74 "request-tracing-endpoint": "https://blah.example.com", 75 "request-tracing-sampling-rate": "0.8", 76 }) 77 if err != nil { 78 t.Error("unexpected error", err) 79 } 80 81 want := Config{ 82 Protocol: ProtocolGRPC, 83 Endpoint: "https://blah.example.com", 84 SamplingRate: 0.8, 85 } 86 87 if diff := cmp.Diff(want, got); diff != "" { 88 t.Error("unexpected diff (-want +got): ", diff) 89 } 90 }