@@ -2,9 +2,9 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"io"
7
- "io/ioutil"
8
8
"log"
9
9
"net/http"
10
10
"os"
@@ -24,8 +24,6 @@ type server struct {
24
24
}
25
25
26
26
func main () {
27
- //
28
- // Adapter API
29
27
config := datadog .DefaultDatadogConfig ()
30
28
config .ServiceName = "iota"
31
29
config .AgentHost = "http://ddagent:8126"
@@ -97,6 +95,7 @@ func upload(res http.ResponseWriter, req *http.Request) {
97
95
}
98
96
99
97
func (s * server ) runModule (res http.ResponseWriter , req * http.Request ) {
98
+ ctx := context .Background ()
100
99
if req .Method != http .MethodPost {
101
100
res .WriteHeader (http .StatusMethodNotAllowed )
102
101
return
@@ -111,40 +110,48 @@ func (s *server) runModule(res http.ResponseWriter, req *http.Request) {
111
110
}
112
111
113
112
path := filepath .Join (os .TempDir (), name )
114
- wasm , err := ioutil .ReadFile (path )
113
+ wasm , err := os .ReadFile (path )
115
114
if err != nil {
116
115
log .Println ("name error: no module found" , err )
117
116
res .WriteHeader (http .StatusNotFound )
118
117
res .Write ([]byte ("Not Found" ))
119
118
return
120
119
}
121
120
122
- //
123
- // Collector API
124
- collector := observe .NewCollector (nil )
125
- ctx , r , err := collector .InitRuntime ()
121
+ s .adapter .Start (ctx )
122
+ defer s .adapter .Stop (true )
123
+
124
+ cfg := wazero .NewRuntimeConfig ().WithCustomSections (true )
125
+ rt := wazero .NewRuntimeWithConfig (ctx , cfg )
126
+ traceCtx , err := s .adapter .NewTraceCtx (ctx , rt , wasm , nil )
126
127
if err != nil {
127
128
log .Panicln (err )
128
129
}
129
- defer r .Close (ctx ) // This closes everything this Runtime created.
130
-
131
- wasi_snapshot_preview1 .MustInstantiate (ctx , r )
130
+ wasi_snapshot_preview1 .MustInstantiate (ctx , rt )
132
131
output := & bytes.Buffer {}
133
132
config := wazero .NewModuleConfig ().WithStdin (req .Body ).WithStdout (output )
134
133
defer req .Body .Close ()
135
134
136
- s .adapter .Start (collector , wasm )
137
- mod , err := r .InstantiateWithConfig (ctx , wasm , config )
135
+ mod , err := rt .InstantiateWithConfig (ctx , wasm , config )
138
136
if err != nil {
139
137
log .Println ("module instance error:" , err )
140
138
res .WriteHeader (http .StatusInternalServerError )
141
139
res .Write ([]byte ("Internal Service Error" ))
142
140
return
143
141
}
144
- s .adapter .Stop (collector )
145
- log .Println ("stopped collector, sent to datadog" )
146
142
defer mod .Close (ctx )
147
143
144
+ meta := datadog.DatadogMetadata {
145
+ ResourceName : "iota-go" ,
146
+ HttpUrl : req .URL .String (),
147
+ HttpStatusCode : 200 ,
148
+ SpanKind : datadog .Server ,
149
+ HttpClientIp : req .RemoteAddr ,
150
+ }
151
+ traceCtx .Metadata (meta )
152
+ traceCtx .Finish ()
153
+ log .Println ("stopped collector, sent to datadog" )
154
+
148
155
res .WriteHeader (http .StatusOK )
149
156
res .Header ().Add ("content-type" , "application/json" )
150
157
res .Write (output .Bytes ())
0 commit comments