|
| 1 | +package tracing_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "go.opentelemetry.io/otel/attribute" |
| 11 | + "go.opentelemetry.io/otel/trace" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "cdr.dev/slog" |
| 16 | + "github.com/coder/coder/coderd/tracing" |
| 17 | +) |
| 18 | + |
| 19 | +type stringer string |
| 20 | + |
| 21 | +var _ fmt.Stringer = stringer("") |
| 22 | + |
| 23 | +func (s stringer) String() string { |
| 24 | + return string(s) |
| 25 | +} |
| 26 | + |
| 27 | +type traceEvent struct { |
| 28 | + name string |
| 29 | + attributes []attribute.KeyValue |
| 30 | +} |
| 31 | + |
| 32 | +type slogFakeSpan struct { |
| 33 | + trace.Span // always nil |
| 34 | + |
| 35 | + isRecording bool |
| 36 | + events []traceEvent |
| 37 | +} |
| 38 | + |
| 39 | +// We overwrite some methods below. |
| 40 | +var _ trace.Span = &slogFakeSpan{} |
| 41 | + |
| 42 | +// IsRecording implements trace.Span. |
| 43 | +func (s *slogFakeSpan) IsRecording() bool { |
| 44 | + return s.isRecording |
| 45 | +} |
| 46 | + |
| 47 | +// AddEvent implements trace.Span. |
| 48 | +func (s *slogFakeSpan) AddEvent(name string, options ...trace.EventOption) { |
| 49 | + cfg := trace.NewEventConfig(options...) |
| 50 | + |
| 51 | + s.events = append(s.events, traceEvent{ |
| 52 | + name: name, |
| 53 | + attributes: cfg.Attributes(), |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func Test_SlogSink(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + fieldsMap := map[string]interface{}{ |
| 61 | + "test_bool": true, |
| 62 | + "test_[]bool": []bool{true, false}, |
| 63 | + "test_float32": float32(1.1), |
| 64 | + "test_float64": float64(1.1), |
| 65 | + "test_[]float64": []float64{1.1, 2.2}, |
| 66 | + "test_int": int(1), |
| 67 | + "test_[]int": []int{1, 2}, |
| 68 | + "test_int8": int8(1), |
| 69 | + "test_int16": int16(1), |
| 70 | + "test_int32": int32(1), |
| 71 | + "test_int64": int64(1), |
| 72 | + "test_[]int64": []int64{1, 2}, |
| 73 | + "test_uint": uint(1), |
| 74 | + "test_uint8": uint8(1), |
| 75 | + "test_uint16": uint16(1), |
| 76 | + "test_uint32": uint32(1), |
| 77 | + "test_uint64": uint64(1), |
| 78 | + "test_string": "test", |
| 79 | + "test_[]string": []string{"test1", "test2"}, |
| 80 | + "test_duration": time.Second, |
| 81 | + "test_time": time.Now(), |
| 82 | + "test_stringer": stringer("test"), |
| 83 | + "test_struct": struct { |
| 84 | + Field string `json:"field"` |
| 85 | + }{ |
| 86 | + Field: "test", |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + entry := slog.SinkEntry{ |
| 91 | + Time: time.Now(), |
| 92 | + Level: slog.LevelInfo, |
| 93 | + Message: "hello", |
| 94 | + LoggerNames: []string{"foo", "bar"}, |
| 95 | + Func: "hello", |
| 96 | + File: "hello.go", |
| 97 | + Line: 42, |
| 98 | + Fields: mapToSlogFields(fieldsMap), |
| 99 | + } |
| 100 | + |
| 101 | + t.Run("NotRecording", func(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + |
| 104 | + sink := tracing.SlogSink{} |
| 105 | + span := &slogFakeSpan{ |
| 106 | + isRecording: false, |
| 107 | + } |
| 108 | + ctx := trace.ContextWithSpan(context.Background(), span) |
| 109 | + |
| 110 | + sink.LogEntry(ctx, entry) |
| 111 | + require.Len(t, span.events, 0) |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("OK", func(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + |
| 117 | + sink := tracing.SlogSink{} |
| 118 | + sink.Sync() |
| 119 | + |
| 120 | + span := &slogFakeSpan{ |
| 121 | + isRecording: true, |
| 122 | + } |
| 123 | + ctx := trace.ContextWithSpan(context.Background(), span) |
| 124 | + |
| 125 | + sink.LogEntry(ctx, entry) |
| 126 | + require.Len(t, span.events, 1) |
| 127 | + |
| 128 | + sink.LogEntry(ctx, entry) |
| 129 | + require.Len(t, span.events, 2) |
| 130 | + |
| 131 | + e := span.events[0] |
| 132 | + require.Equal(t, "log: INFO: hello", e.name) |
| 133 | + |
| 134 | + expectedAttributes := mapToBasicMap(fieldsMap) |
| 135 | + delete(expectedAttributes, "test_struct") |
| 136 | + expectedAttributes["slog.time"] = entry.Time.Format(time.RFC3339Nano) |
| 137 | + expectedAttributes["slog.logger"] = strings.Join(entry.LoggerNames, ".") |
| 138 | + expectedAttributes["slog.level"] = entry.Level.String() |
| 139 | + expectedAttributes["slog.message"] = entry.Message |
| 140 | + expectedAttributes["slog.func"] = entry.Func |
| 141 | + expectedAttributes["slog.file"] = entry.File |
| 142 | + expectedAttributes["slog.line"] = int64(entry.Line) |
| 143 | + |
| 144 | + require.Equal(t, expectedAttributes, attributesToMap(e.attributes)) |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +func mapToSlogFields(m map[string]interface{}) slog.Map { |
| 149 | + fields := make(slog.Map, 0, len(m)) |
| 150 | + for k, v := range m { |
| 151 | + fields = append(fields, slog.F(k, v)) |
| 152 | + } |
| 153 | + |
| 154 | + return fields |
| 155 | +} |
| 156 | + |
| 157 | +func mapToBasicMap(m map[string]interface{}) map[string]interface{} { |
| 158 | + basic := make(map[string]interface{}, len(m)) |
| 159 | + for k, v := range m { |
| 160 | + var val interface{} = v |
| 161 | + switch v := v.(type) { |
| 162 | + case float32: |
| 163 | + val = float64(v) |
| 164 | + case int: |
| 165 | + val = int64(v) |
| 166 | + case []int: |
| 167 | + i64Slice := make([]int64, len(v)) |
| 168 | + for i, v := range v { |
| 169 | + i64Slice[i] = int64(v) |
| 170 | + } |
| 171 | + val = i64Slice |
| 172 | + case int8: |
| 173 | + val = int64(v) |
| 174 | + case int16: |
| 175 | + val = int64(v) |
| 176 | + case int32: |
| 177 | + val = int64(v) |
| 178 | + case uint: |
| 179 | + val = int64(v) |
| 180 | + case uint8: |
| 181 | + val = int64(v) |
| 182 | + case uint16: |
| 183 | + val = int64(v) |
| 184 | + case uint32: |
| 185 | + val = int64(v) |
| 186 | + case uint64: |
| 187 | + val = int64(v) |
| 188 | + case time.Duration: |
| 189 | + val = v.String() |
| 190 | + case time.Time: |
| 191 | + val = v.Format(time.RFC3339Nano) |
| 192 | + case fmt.Stringer: |
| 193 | + val = v.String() |
| 194 | + } |
| 195 | + |
| 196 | + basic[k] = val |
| 197 | + } |
| 198 | + |
| 199 | + return basic |
| 200 | +} |
| 201 | + |
| 202 | +func attributesToMap(attrs []attribute.KeyValue) map[string]interface{} { |
| 203 | + m := make(map[string]interface{}, len(attrs)) |
| 204 | + for _, attr := range attrs { |
| 205 | + m[string(attr.Key)] = attr.Value.AsInterface() |
| 206 | + } |
| 207 | + |
| 208 | + return m |
| 209 | +} |
0 commit comments