@@ -16,9 +16,40 @@ import (
16
16
17
17
type stackTraceProcessor struct {
18
18
nextConsumer consumer.Traces
19
+ sourceMaps map [string ][]byte
19
20
}
20
21
21
22
func (s * stackTraceProcessor ) Start (ctx context.Context , host component.Host ) error {
23
+ s .sourceMaps = make (map [string ][]byte )
24
+ files , err := os .ReadDir ("testdata" )
25
+ if err != nil {
26
+ return err
27
+ }
28
+ for _ , file := range files {
29
+ if strings .HasSuffix (file .Name (), ".map" ) {
30
+ err := s .ReadSourceMap ("testdata" , file .Name ())
31
+ if err != nil {
32
+ return err
33
+ }
34
+ }
35
+ }
36
+ return nil
37
+ }
38
+
39
+ func (s * stackTraceProcessor ) ReadSourceMap (path string , name string ) error {
40
+ fullPath := fmt .Sprintf ("%s/%s" , path , name )
41
+ if path == "" {
42
+ fullPath = name
43
+ }
44
+ file , err := os .Open (fullPath )
45
+ if err != nil {
46
+ return err
47
+ }
48
+ b , err := io .ReadAll (file )
49
+ if err != nil {
50
+ return err
51
+ }
52
+ s .sourceMaps [name ] = b
22
53
return nil
23
54
}
24
55
@@ -35,36 +66,36 @@ func (s *stackTraceProcessor) ConsumeTraces(ctx context.Context, td ptrace.Trace
35
66
resourceSpan := td .ResourceSpans ().At (resourceSpanId )
36
67
sdkLanguage , valid := resourceSpan .Resource ().Attributes ().Get ("telemetry.sdk.language" )
37
68
if valid && sdkLanguage .Str () == "webjs" {
38
- ConsumeScopeSpans (resourceSpan .ScopeSpans ())
69
+ s . ConsumeScopeSpans (resourceSpan .ScopeSpans ())
39
70
}
40
71
}
41
72
return s .nextConsumer .ConsumeTraces (ctx , td )
42
73
}
43
74
44
- func ConsumeScopeSpans (scopeSpans ptrace.ScopeSpansSlice ) {
75
+ func ( s * stackTraceProcessor ) ConsumeScopeSpans (scopeSpans ptrace.ScopeSpansSlice ) {
45
76
for scopeSpanId := 0 ; scopeSpanId < scopeSpans .Len (); scopeSpanId ++ {
46
77
spans := scopeSpans .At (scopeSpanId ).Spans ()
47
- ConsumeSpans (spans )
78
+ s . ConsumeSpans (spans )
48
79
}
49
80
}
50
81
51
- func ConsumeSpans (spans ptrace.SpanSlice ) {
82
+ func ( s * stackTraceProcessor ) ConsumeSpans (spans ptrace.SpanSlice ) {
52
83
for spanId := 0 ; spanId < spans .Len (); spanId ++ {
53
84
span := spans .At (spanId )
54
- ConsumeSpan (span )
85
+ s . ConsumeSpan (span )
55
86
}
56
87
}
57
88
58
- func ConsumeSpan (span ptrace.Span ) {
89
+ func ( s * stackTraceProcessor ) ConsumeSpan (span ptrace.Span ) {
59
90
for eventId := 0 ; eventId < span .Events ().Len (); eventId ++ {
60
91
event := span .Events ().At (eventId )
61
92
if event .Name () == "exception" {
62
- ConsumeException (event )
93
+ s . ConsumeException (event )
63
94
}
64
95
}
65
96
}
66
97
67
- func ConsumeException (event ptrace.SpanEvent ) {
98
+ func ( s * stackTraceProcessor ) ConsumeException (event ptrace.SpanEvent ) {
68
99
stacktrace , valid := event .Attributes ().Get ("exception.stacktrace" )
69
100
if valid != true {
70
101
return
@@ -93,27 +124,21 @@ func ConsumeException(event ptrace.SpanEvent) {
93
124
res = append (res , line )
94
125
continue
95
126
}
96
- file , err := os .Open (fmt .Sprintf ("%s.map" , sourceFile ))
127
+ mapFileName := fmt .Sprintf ("%s.map" , sourceFile )
128
+
129
+ smap , err := sourcemap .Parse (sourceFile , s .sourceMaps [mapFileName ])
97
130
if err != nil {
98
131
res = append (res , line )
99
132
continue
100
133
}
101
- b , err := io .ReadAll (file )
102
- if err != nil {
134
+
135
+ finalFile , _ , sourceLine , sourceColumn , ok := smap .Source (mapLine , mapColumn )
136
+ if ok != true {
103
137
res = append (res , line )
104
138
continue
105
139
}
106
-
107
- smap , err := sourcemap .Parse (sourceFile , b )
108
- if err != nil {
109
- panic (err )
110
- }
111
-
112
- finalFile , fn , sourceLine , sourceColumn , ok := smap .Source (mapLine , mapColumn )
113
- fmt .Println (finalFile , fn , sourceLine , sourceColumn , ok )
114
140
res = append (res , fmt .Sprintf ("%s@%s:%d:%d" , trace , finalFile , sourceLine , sourceColumn ))
115
141
}
116
- fmt .Println (res )
117
142
event .Attributes ().PutStr ("exception.stacktrace" , strings .Join (res , "\n " ))
118
143
}
119
144
0 commit comments