Best Keploy code snippet using telemetry.NewTelemetry
telemetry_test.go
Source:telemetry_test.go  
1// Copyright Istio Authors2//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 at6//7//     http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.14package model15import (16	"testing"17	"github.com/gogo/protobuf/types"18	"github.com/google/go-cmp/cmp"19	"google.golang.org/protobuf/testing/protocmp"20	meshconfig "istio.io/api/mesh/v1alpha1"21	tpb "istio.io/api/telemetry/v1alpha1"22	"istio.io/api/type/v1beta1"23	"istio.io/istio/pkg/config"24	"istio.io/istio/pkg/config/mesh"25	"istio.io/istio/pkg/config/schema/collection"26	"istio.io/istio/pkg/config/schema/collections"27)28func TestTelemetries_EffectiveTelemetry(t *testing.T) {29	rootAccessLogs := &tpb.Telemetry{30		AccessLogging: []*tpb.AccessLogging{31			{32				Providers: []*tpb.ProviderRef{33					{34						Name: "stdout",35					},36				},37			},38		},39	}40	barAccessLogs := &tpb.Telemetry{41		AccessLogging: []*tpb.AccessLogging{42			{43				Disabled: &types.BoolValue{Value: true},44			},45		},46	}47	bazAccessLogs := &tpb.Telemetry{48		AccessLogging: []*tpb.AccessLogging{49			{50				Providers: []*tpb.ProviderRef{51					{52						Name: "custom-provider",53					},54				},55				Disabled: &types.BoolValue{Value: false},56			},57		},58	}59	rootTrace := &tpb.Telemetry{60		Tracing: []*tpb.Tracing{61			{62				Providers: []*tpb.ProviderRef{63					{64						Name: "zipkin",65					},66				},67				RandomSamplingPercentage: &types.DoubleValue{Value: 10.10},68			},69		},70	}71	fooTrace := &tpb.Telemetry{72		Selector: &v1beta1.WorkloadSelector{73			MatchLabels: map[string]string{"service.istio.io/canonical-name": "foo"},74		},75		Tracing: []*tpb.Tracing{76			{77				RandomSamplingPercentage: &types.DoubleValue{Value: 0.0},78			},79		},80	}81	barTrace := &tpb.Telemetry{82		Tracing: []*tpb.Tracing{83			{84				DisableSpanReporting: &types.BoolValue{Value: true},85			},86		},87	}88	bazTrace := &tpb.Telemetry{89		Tracing: []*tpb.Tracing{90			{91				Providers: []*tpb.ProviderRef{92					{93						Name: "not-zipkin",94					},95				},96				CustomTags: map[string]*tpb.Tracing_CustomTag{97					"fun": {Type: &tpb.Tracing_CustomTag_Environment{98						Environment: &tpb.Tracing_Environment{99							Name:         "SOME_ENV_VAR",100							DefaultValue: "EMPTY",101						},102					}},103				},104			},105		},106	}107	cases := []struct {108		name           string109		ns             string110		configs        []config.Config111		workloadLabels map[string]string112		want           *tpb.Telemetry113	}{114		{115			name: "no configured telemetry",116			ns:   "missing",117		},118		{119			name:    "root namespace no workload labels",120			ns:      "istio-system",121			configs: []config.Config{newTelemetry("root", "istio-system", rootTrace)},122			want:    rootTrace,123		},124		{125			name: "workload-specific in namespace but no workload labels",126			ns:   "foo",127			configs: []config.Config{128				newTelemetry("root", "istio-system", rootTrace),129				newTelemetry("foo", "foo", fooTrace),130			},131			want: &tpb.Telemetry{132				Tracing: []*tpb.Tracing{133					{134						Providers: []*tpb.ProviderRef{135							{136								Name: "zipkin",137							},138						},139						RandomSamplingPercentage: &types.DoubleValue{Value: 10.10},140					},141				},142			},143		},144		{145			name: "workload-agnostic in namespace and no workload labels",146			ns:   "foo",147			configs: []config.Config{148				newTelemetry("root", "istio-system", rootTrace),149				newTelemetry("foo", "foo", barTrace),150			},151			want: &tpb.Telemetry{152				Tracing: []*tpb.Tracing{153					{154						Providers: []*tpb.ProviderRef{155							{156								Name: "zipkin",157							},158						},159						RandomSamplingPercentage: &types.DoubleValue{Value: 10.10},160						DisableSpanReporting:     &types.BoolValue{Value: true},161					},162				},163			},164		},165		{166			name:           "both workload-specific and workload-agnostic in namespace with workload labels",167			ns:             "foo",168			workloadLabels: map[string]string{"service.istio.io/canonical-name": "foo"},169			configs: []config.Config{170				newTelemetry("root", "istio-system", rootTrace),171				newTelemetry("foo", "foo", fooTrace),172				newTelemetry("bar", "foo", barTrace),173				newTelemetry("baz", "baz", bazTrace),174			},175			want: &tpb.Telemetry{176				Tracing: []*tpb.Tracing{177					{178						Providers: []*tpb.ProviderRef{179							{180								Name: "zipkin",181							},182						},183						DisableSpanReporting:     &types.BoolValue{Value: true},184						RandomSamplingPercentage: &types.DoubleValue{Value: 0.0},185					},186				},187			},188		},189		{190			name: "both workload-specific and workload-agnostic in namespace without workload labels",191			ns:   "foobar",192			configs: []config.Config{193				newTelemetry("root", "istio-system", rootTrace),194				newTelemetry("foo", "foobar", fooTrace),195				newTelemetry("bar", "foobar", barTrace),196				newTelemetry("baz", "baz", bazTrace),197			},198			want: &tpb.Telemetry{199				Tracing: []*tpb.Tracing{200					{201						Providers: []*tpb.ProviderRef{202							{203								Name: "zipkin",204							},205						},206						RandomSamplingPercentage: &types.DoubleValue{Value: 10.10},207						DisableSpanReporting:     &types.BoolValue{Value: true},208					},209				},210			},211		},212		{213			name: "provider and custom tags override",214			ns:   "baz",215			configs: []config.Config{216				newTelemetry("root", "istio-system", rootTrace),217				newTelemetry("foo", "foo", fooTrace),218				newTelemetry("baz", "baz", bazTrace),219			},220			want: &tpb.Telemetry{221				Tracing: []*tpb.Tracing{222					{223						Providers: []*tpb.ProviderRef{224							{225								Name: "not-zipkin",226							},227						},228						RandomSamplingPercentage: &types.DoubleValue{Value: 10.10},229						CustomTags: map[string]*tpb.Tracing_CustomTag{230							"fun": {Type: &tpb.Tracing_CustomTag_Environment{231								Environment: &tpb.Tracing_Environment{232									Name:         "SOME_ENV_VAR",233									DefaultValue: "EMPTY",234								},235							}},236						},237					},238				},239			},240		},241		{242			name: "access logs disable",243			ns:   "bar",244			configs: []config.Config{245				newTelemetry("root", "istio-system", rootAccessLogs),246				newTelemetry("bar", "bar", barAccessLogs),247			},248			want: &tpb.Telemetry{249				AccessLogging: []*tpb.AccessLogging{250					{251						Providers: []*tpb.ProviderRef{252							{253								Name: "stdout",254							},255						},256						Disabled: &types.BoolValue{Value: true},257					},258				},259			},260		},261		{262			name: "access logs provider",263			ns:   "baz",264			configs: []config.Config{265				newTelemetry("root", "istio-system", rootAccessLogs),266				newTelemetry("baz", "baz", bazAccessLogs),267			},268			want: &tpb.Telemetry{269				AccessLogging: []*tpb.AccessLogging{270					{271						Providers: []*tpb.ProviderRef{272							{273								Name: "custom-provider",274							},275						},276						Disabled: &types.BoolValue{Value: false},277					},278				},279			},280		},281		{282			name: "access logs and tracing",283			ns:   "bar",284			configs: []config.Config{285				newTelemetry("root-logs", "istio-system", rootAccessLogs),286				newTelemetry("bar", "bar", barTrace),287			},288			want: &tpb.Telemetry{289				Tracing: []*tpb.Tracing{290					{291						DisableSpanReporting: &types.BoolValue{Value: true},292					},293				},294				AccessLogging: []*tpb.AccessLogging{295					{296						Providers: []*tpb.ProviderRef{297							{298								Name: "stdout",299							},300						},301					},302				},303			},304		},305	}306	for _, v := range cases {307		t.Run(v.name, func(tt *testing.T) {308			telemetries := createTestTelemetries(v.configs, tt)309			got := telemetries.EffectiveTelemetry(&Proxy{ConfigNamespace: v.ns, Metadata: &NodeMetadata{Labels: v.workloadLabels}})310			if diff := cmp.Diff(v.want, got, protocmp.Transform()); diff != "" {311				tt.Errorf("EffectiveTelemetry(%s, %v) returned unexpected diff (-want +got):\n%s", v.ns, v.workloadLabels, diff)312			}313		})314	}315}316func createTestTelemetries(configs []config.Config, t *testing.T) *Telemetries {317	t.Helper()318	store := &telemetryStore{}319	for _, cfg := range configs {320		store.add(cfg)321	}322	environment := &Environment{323		IstioConfigStore: MakeIstioStore(store),324		Watcher:          mesh.NewFixedWatcher(&meshconfig.MeshConfig{RootNamespace: "istio-system"}),325	}326	telemetries, err := GetTelemetries(environment)327	if err != nil {328		t.Fatalf("GetTelemetries failed: %v", err)329	}330	return telemetries331}332func newTelemetry(name, ns string, spec config.Spec) config.Config {333	return config.Config{334		Meta: config.Meta{335			GroupVersionKind: collections.IstioTelemetryV1Alpha1Telemetries.Resource().GroupVersionKind(),336			Name:             name,337			Namespace:        ns,338		},339		Spec: spec,340	}341}342type telemetryStore struct {343	ConfigStore344	data []struct {345		typ config.GroupVersionKind346		ns  string347		cfg config.Config348	}349}350func (ts *telemetryStore) add(cfg config.Config) {351	ts.data = append(ts.data, struct {352		typ config.GroupVersionKind353		ns  string354		cfg config.Config355	}{356		typ: cfg.GroupVersionKind,357		ns:  cfg.Namespace,358		cfg: cfg,359	})360}361func (ts *telemetryStore) Schemas() collection.Schemas {362	return collection.SchemasFor()363}364func (ts *telemetryStore) Get(_ config.GroupVersionKind, _, _ string) *config.Config {365	return nil366}367func (ts *telemetryStore) List(typ config.GroupVersionKind, namespace string) ([]config.Config, error) {368	var configs []config.Config369	for _, data := range ts.data {370		if data.typ == typ {371			if namespace != "" && data.ns == namespace {372				continue373			}374			configs = append(configs, data.cfg)375		}376	}377	return configs, nil378}...NewTelemetry
Using AI Code Generation
1import (2func main() {3	node, err := goeapi.ConnectTo("veos1")4	if err != nil {5		log.Fatal("Failed to connect to switch")6	}7	telemetry := goeapi.NewTelemetry(node)8	sub, err := telemetry.NewSubscription("show version")9	if err != nil {10		log.Fatal("Failed to create subscription")11	}12	err = sub.Start()13	if err != nil {14		log.Fatal("Failed to start subscription")15	}16	for {17		data, err := sub.Receive()18		if err != nil {19			log.Fatal("Failed to receive data")20		}21		log.Println(data)22	}23}NewTelemetry
Using AI Code Generation
1import (2func main() {3	t, err := telemetry.NewTelemetry("localhost:6030")4	if err != nil {5		fmt.Printf("Error: %s\n", err)6	}7	fmt.Println("Connected to telemetry server")8	defer t.Close()9}10import (11func main() {12	t, err := telemetry.NewTelemetry("localhost:6030")13	if err != nil {14		fmt.Printf("Error: %s\n", err)15	}16	fmt.Println("Connected to telemetry server")17	defer t.Close()18	sub, err := t.Subscribe(telemetry.Subscription{19	})20	if err != nil {21		fmt.Printf("Error: %s\n", err)22	}23	defer sub.Close()24	msg, err := sub.Recv()25	if err != nil {26		fmt.Printf("Error: %s\n", err)27	}28	fmt.Printf("Received message: %s\n", msg)29}30import (31func main() {32	t, err := telemetry.NewTelemetry("localhost:6030")33	if err != nil {34		fmt.Printf("Error: %s\n", err)35	}36	fmt.Println("Connected to telemetry server")37	defer t.Close()38	sub, err := t.Subscribe(telemetry.Subscription{39	})40	if err != nil {41		fmt.Printf("ErrorNewTelemetry
Using AI Code Generation
1import "fmt"2import "github.com/telemetryapp/gotelemetry"3func main() {4  telemetry.NewTelemetry("your_api_key")5  fmt.Println("Hello, world.")6}7import "fmt"8import "github.com/telemetryapp/gotelemetry"9func main() {10  telemetry.NewTelemetry("your_api_key")11  fmt.Println("Hello, world.")12}13import "fmt"14import "github.com/telemetryapp/gotelemetry"15func main() {16  telemetry.NewTelemetry("your_api_key")17  fmt.Println("Hello, world.")18}19import "fmt"20import "github.com/telemetryapp/gotelemetry"21func main() {22  telemetry.NewTelemetry("your_api_key")23  fmt.Println("Hello, world.")24}25import "fmt"26import "github.com/telemetryapp/gotelemetry"27func main() {28  telemetry.NewTelemetry("your_api_key")29  fmt.Println("Hello, world.")30}31import "fmt"32import "github.com/telemetryapp/gotelemetry"33func main() {34  telemetry.NewTelemetry("your_api_key")35  fmt.Println("Hello, world.")36}37import "fmt"38import "github.com/telemetryapp/gotelemetry"39func main() {40  telemetry.NewTelemetry("your_api_key")41  fmt.Println("Hello, world.")42}43import "fmt"44import "github.com/telemetryapp/gotelemetry"45func main() {46  telemetry.NewTelemetry("your_api_key")47  fmt.Println("Hello, world.")48}49import "fmt"50import "githubNewTelemetry
Using AI Code Generation
1import (2func main() {3	t := telemetry.NewTelemetry(10, 10)4	err := t.Connect("localhost:8080")5	if err != nil {6		panic(err)7	}8	defer t.Close()9}10import (11func main() {12	t := telemetry.NewTelemetry(10, 10)13	err := t.Connect("localhost:8080")14	if err != nil {15		panic(err)16	}17	defer t.Close()18	stream, err := t.Subscribe("Arista-EOS-MIB", "eosCounters")19	if err != nil {20		panic(err)21	}22	defer stream.Close()23	for update := range stream.C {24		fmt.Println(update)25	}26}27import (28func main() {29	t := telemetry.NewTelemetry(10, 10)30	err := t.Connect("localhost:8080")31	if err != nil {32		panic(err)33	}34	defer t.Close()35	stream, err := t.Subscribe("Arista-EOS-MIB", "eosCounters")36	if err != nil {37		panic(err)38	}39	defer stream.Close()40	for update := range stream.C {41		fmt.Println(update)42	}43}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
