|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package mobile |
| 19 | + |
| 20 | +import ( |
| 21 | +"testing" |
| 22 | +"time" |
| 23 | + |
| 24 | +"maps" |
| 25 | + |
| 26 | +"github.com/google/go-cmp/cmp" |
| 27 | +"github.com/stretchr/testify/assert" |
| 28 | +"go.opentelemetry.io/collector/pdata/pcommon" |
| 29 | +"go.opentelemetry.io/collector/pdata/plog" |
| 30 | +) |
| 31 | + |
| 32 | +func TestEnrichEvents(t *testing.T) { |
| 33 | +now := time.Unix(3600, 0) |
| 34 | +timestamp := pcommon.NewTimestampFromTime(now) |
| 35 | +javaStacktrace := "Exception in thread \"main\" java.lang.RuntimeException: Test exception\n at com.example.GenerateTrace.methodB(GenerateTrace.java:13)\n at com.example.GenerateTrace.methodA(GenerateTrace.java:9)\n at com.example.GenerateTrace.main(GenerateTrace.java:5)" |
| 36 | +javaStacktraceHash := "e25c4196dc720d91" |
| 37 | + |
| 38 | +swiftStacktrace := readSwiftStacktraceFile(t, "thread-8-crash.txt") |
| 39 | +swiftStacktraceHash := "e737b0da1c8f9d5a" |
| 40 | + |
| 41 | +for _, tc := range []struct { |
| 42 | +name string |
| 43 | +eventName string |
| 44 | +input func() plog.LogRecord |
| 45 | +resourceAttrs map[string]any |
| 46 | +expectedAttributes map[string]any |
| 47 | +}{ |
| 48 | +{ |
| 49 | +name: "crash_event_java", |
| 50 | +eventName: "device.crash", |
| 51 | +resourceAttrs: map[string]any{ |
| 52 | +"telemetry.sdk.language": "java", |
| 53 | +}, |
| 54 | +input: func() plog.LogRecord { |
| 55 | +logRecord := plog.NewLogRecord() |
| 56 | +logRecord.SetTimestamp(timestamp) |
| 57 | +logRecord.Attributes().PutStr("event.name", "device.crash") |
| 58 | +logRecord.Attributes().PutStr("exception.message", "Exception message") |
| 59 | +logRecord.Attributes().PutStr("exception.type", "java.lang.RuntimeException") |
| 60 | +logRecord.Attributes().PutStr("exception.stacktrace", javaStacktrace) |
| 61 | +return logRecord |
| 62 | +}, |
| 63 | +expectedAttributes: map[string]any{ |
| 64 | +"processor.event": "error", |
| 65 | +"timestamp.us": timestamp.AsTime().UnixMicro(), |
| 66 | +"error.grouping_key": javaStacktraceHash, |
| 67 | +"error.type": "crash", |
| 68 | +"event.kind": "event", |
| 69 | +}, |
| 70 | +}, |
| 71 | +{ |
| 72 | +name: "crash_event_without_timestamp_java", |
| 73 | +eventName: "device.crash", |
| 74 | +resourceAttrs: map[string]any{ |
| 75 | +"telemetry.sdk.language": "java", |
| 76 | +}, |
| 77 | +input: func() plog.LogRecord { |
| 78 | +logRecord := plog.NewLogRecord() |
| 79 | +logRecord.SetObservedTimestamp(timestamp) |
| 80 | +logRecord.Attributes().PutStr("event.name", "device.crash") |
| 81 | +logRecord.Attributes().PutStr("exception.message", "Exception message") |
| 82 | +logRecord.Attributes().PutStr("exception.type", "java.lang.RuntimeException") |
| 83 | +logRecord.Attributes().PutStr("exception.stacktrace", javaStacktrace) |
| 84 | +return logRecord |
| 85 | +}, |
| 86 | +expectedAttributes: map[string]any{ |
| 87 | +"processor.event": "error", |
| 88 | +"timestamp.us": timestamp.AsTime().UnixMicro(), |
| 89 | +"error.grouping_key": javaStacktraceHash, |
| 90 | +"error.type": "crash", |
| 91 | +"event.kind": "event", |
| 92 | +}, |
| 93 | +}, |
| 94 | +{ |
| 95 | +name: "crash_event_non_java", |
| 96 | +eventName: "device.crash", |
| 97 | +resourceAttrs: map[string]any{ |
| 98 | +"telemetry.sdk.language": "go", |
| 99 | +}, |
| 100 | +input: func() plog.LogRecord { |
| 101 | +logRecord := plog.NewLogRecord() |
| 102 | +logRecord.SetTimestamp(timestamp) |
| 103 | +logRecord.Attributes().PutStr("event.name", "device.crash") |
| 104 | +logRecord.Attributes().PutStr("exception.message", "Exception message") |
| 105 | +logRecord.Attributes().PutStr("exception.type", "go.error") |
| 106 | +logRecord.Attributes().PutStr("exception.stacktrace", javaStacktrace) |
| 107 | +return logRecord |
| 108 | +}, |
| 109 | +expectedAttributes: map[string]any{ |
| 110 | +"processor.event": "error", |
| 111 | +"timestamp.us": timestamp.AsTime().UnixMicro(), |
| 112 | +"error.type": "crash", |
| 113 | +"event.kind": "event", |
| 114 | +}, |
| 115 | +}, |
| 116 | +{ |
| 117 | +name: "non_crash_event", |
| 118 | +eventName: "othername", |
| 119 | +resourceAttrs: map[string]any{}, |
| 120 | +input: func() plog.LogRecord { |
| 121 | +logRecord := plog.NewLogRecord() |
| 122 | +logRecord.Attributes().PutStr("event.name", "othername") |
| 123 | +return logRecord |
| 124 | +}, |
| 125 | +expectedAttributes: map[string]any{ |
| 126 | +"event.kind": "event", |
| 127 | +}, |
| 128 | +}, |
| 129 | +{ |
| 130 | +name: "crash_event_swift", |
| 131 | +eventName: "device.crash", |
| 132 | +resourceAttrs: map[string]any{ |
| 133 | +"telemetry.sdk.language": "swift", |
| 134 | +}, |
| 135 | +input: func() plog.LogRecord { |
| 136 | +logRecord := plog.NewLogRecord() |
| 137 | +logRecord.SetTimestamp(timestamp) |
| 138 | +logRecord.Attributes().PutStr("event.name", "device.crash") |
| 139 | +logRecord.Attributes().PutStr("exception.type", "SIGTRAP") |
| 140 | +logRecord.Attributes().PutStr("exception.stacktrace", swiftStacktrace) |
| 141 | +return logRecord |
| 142 | +}, |
| 143 | +expectedAttributes: map[string]any{ |
| 144 | +"processor.event": "error", |
| 145 | +"timestamp.us": timestamp.AsTime().UnixMicro(), |
| 146 | +"error.grouping_key": swiftStacktraceHash, |
| 147 | +"error.type": "crash", |
| 148 | +"event.kind": "event", |
| 149 | +}, |
| 150 | +}, |
| 151 | +} { |
| 152 | +t.Run(tc.name, func(t *testing.T) { |
| 153 | +inputLogRecord := tc.input() |
| 154 | + |
| 155 | +maps.Copy(tc.expectedAttributes, inputLogRecord.Attributes().AsRaw()) |
| 156 | + |
| 157 | +ctx := EventContext{ |
| 158 | +ResourceAttributes: tc.resourceAttrs, |
| 159 | +EventName: tc.eventName, |
| 160 | +} |
| 161 | +EnrichLogEvent(ctx, inputLogRecord) |
| 162 | + |
| 163 | +assert.Empty(t, cmp.Diff(inputLogRecord.Attributes().AsRaw(), tc.expectedAttributes, ignoreMapKey("error.id"))) |
| 164 | +errorId, ok := inputLogRecord.Attributes().Get("error.id") |
| 165 | +if ok { |
| 166 | +assert.Equal(t, "device.crash", tc.eventName) |
| 167 | +assert.Equal(t, 32, len(errorId.AsString())) |
| 168 | +} else { |
| 169 | +assert.NotEqual(t, "device.crash", tc.eventName) |
| 170 | +} |
| 171 | +}) |
| 172 | +} |
| 173 | +} |
| 174 | + |
| 175 | +func ignoreMapKey(k string) cmp.Option { |
| 176 | +return cmp.FilterPath(func(p cmp.Path) bool { |
| 177 | +mapIndex, ok := p.Last().(cmp.MapIndex) |
| 178 | +if !ok { |
| 179 | +return false |
| 180 | +} |
| 181 | +return mapIndex.Key().String() == k |
| 182 | +}, cmp.Ignore()) |
| 183 | +} |
0 commit comments