go.temporal.io/server@v1.23.0/common/telemetry/config_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package telemetry_test 26 27 import ( 28 "testing" 29 "time" 30 31 "github.com/stretchr/testify/require" 32 "go.temporal.io/server/common/telemetry" 33 "gopkg.in/yaml.v3" 34 ) 35 36 var basicOTLPTraceOnlyConfig = ` 37 exporters: 38 - kind: 39 signal: traces 40 model: otlp 41 protocol: grpc 42 spec: 43 headers: 44 a: b 45 c: d 46 timeout: 10s 47 retry: 48 enabled: true 49 initial_interval: 1s 50 max_interval: 1s 51 max_elapsed_time: 1s 52 connection: 53 block: false 54 insecure: true 55 endpoint: localhost:4317 56 ` 57 58 var sharedConnOTLPConfig = ` 59 otel: 60 connections: 61 - kind: grpc 62 metadata: 63 name: conn1 64 spec: 65 endpoint: localhost:4317 66 exporters: 67 - kind: 68 signal: traces 69 model: otlp 70 protocol: grpc 71 spec: 72 connection_name: conn1 73 - kind: 74 signal: metrics 75 model: otlp 76 protocol: grpc 77 spec: 78 connection_name: conn1 79 ` 80 81 func TestEmptyConfig(t *testing.T) { 82 cfg := telemetry.ExportConfig{} 83 exporters, err := cfg.SpanExporters() 84 require.NoError(t, err) 85 require.Len(t, exporters, 0) 86 } 87 88 func TestExportersWithSharedConn(t *testing.T) { 89 root := struct{ Otel telemetry.PrivateExportConfig }{} 90 err := yaml.Unmarshal([]byte(sharedConnOTLPConfig), &root) 91 require.NoError(t, err) 92 cfg := &root.Otel 93 94 spanExporters, err := cfg.SpanExporters() 95 require.NoError(t, err) 96 require.Len(t, spanExporters, 1) 97 98 metricExporters, err := cfg.MetricExporters() 99 require.NoError(t, err) 100 require.Len(t, metricExporters, 1) 101 } 102 103 func TestSharedConn(t *testing.T) { 104 root := struct{ Otel telemetry.PrivateExportConfig }{} 105 err := yaml.Unmarshal([]byte(sharedConnOTLPConfig), &root) 106 require.NoError(t, err) 107 cfg := &root.Otel 108 require.Len(t, cfg.Connections, 1) 109 require.Len(t, cfg.Exporters, 2) 110 111 exp := cfg.Exporters[0] 112 require.Equal(t, exp.Kind.Signal, "traces") 113 require.Equal(t, exp.Kind.Model, "otlp") 114 require.Equal(t, exp.Kind.Protocol, "grpc") 115 require.NotNil(t, exp.Spec) 116 sspec, ok := exp.Spec.(*telemetry.OTLPGRPCSpanExporter) 117 require.True(t, ok) 118 require.Equal(t, "conn1", sspec.ConnectionName) 119 120 exp = cfg.Exporters[1] 121 require.Equal(t, exp.Kind.Signal, "metrics") 122 require.Equal(t, exp.Kind.Model, "otlp") 123 require.Equal(t, exp.Kind.Protocol, "grpc") 124 require.NotNil(t, exp.Spec) 125 mspec, ok := exp.Spec.(*telemetry.OTLPGRPCMetricExporter) 126 require.True(t, ok) 127 require.Equal(t, "conn1", mspec.ConnectionName) 128 } 129 130 func TestOTLPTraceGRPC(t *testing.T) { 131 cfg := telemetry.PrivateExportConfig{} 132 err := yaml.Unmarshal([]byte(basicOTLPTraceOnlyConfig), &cfg) 133 require.NoError(t, err) 134 require.Len(t, cfg.Connections, 0) 135 require.Len(t, cfg.Exporters, 1) 136 137 exp := cfg.Exporters[0] 138 require.Equal(t, exp.Kind.Signal, "traces") 139 require.Equal(t, exp.Kind.Model, "otlp") 140 require.Equal(t, exp.Kind.Protocol, "grpc") 141 require.NotNil(t, exp.Spec) 142 143 spec, ok := exp.Spec.(*telemetry.OTLPGRPCSpanExporter) 144 require.True(t, ok) 145 require.Equal(t, map[string]string{"a": "b", "c": "d"}, spec.Headers) 146 require.Equal(t, 10*time.Second, spec.Timeout) 147 require.True(t, spec.Retry.Enabled) 148 require.Equal(t, time.Second, spec.Retry.InitialInterval) 149 require.Equal(t, time.Second, spec.Retry.MaxInterval) 150 require.Equal(t, time.Second, spec.Retry.MaxElapsedTime) 151 152 conn := spec.Connection 153 require.True(t, conn.Insecure) 154 require.Equal(t, "localhost:4317", conn.Endpoint) 155 require.False(t, conn.Block) 156 }