MCPcopy
hub / github.com/google/mtail / New

Function New

internal/runtime/runtime.go:250–338  ·  view source on GitHub ↗

New creates a new program loader that reads programs from programPath.

(lines <-chan *logline.LogLine, wg *sync.WaitGroup, programPath string, store *metrics.Store, options ...Option)

Source from the content-addressed store, hash-verified

248
249// New creates a new program loader that reads programs from programPath.
250func New(lines <-chan *logline.LogLine, wg *sync.WaitGroup, programPath string, store *metrics.Store, options ...Option) (*Runtime, error) {
251 if store == nil {
252 return nil, ErrNeedsStore
253 }
254 if wg == nil {
255 return nil, ErrNeedsWaitgroup
256 }
257 r := &Runtime{
258 ms: store,
259 programPath: programPath,
260 handles: make(map[string]*vmHandle),
261 programErrors: make(map[string]error),
262 signalQuit: make(chan struct{}),
263 }
264 initDone := make(chan struct{})
265 defer close(initDone)
266 var err error
267 if err = r.SetOption(options...); err != nil {
268 return nil, err
269 }
270 if r.c, err = compiler.New(r.cOpts...); err != nil {
271 return nil, err
272 }
273 // Defer shutdown handling to avoid a race on r.wg.
274 wg.Add(1)
275 defer func() {
276 go func() {
277 defer wg.Done()
278 <-initDone
279 r.wg.Wait()
280 }()
281 }()
282 // This goroutine is the main consumer/producer loop.
283 r.wg.Add(1)
284 go func() {
285 defer r.wg.Done() // signal to owner we're done
286 <-initDone
287 for line := range lines {
288 LineCount.Add(1)
289 r.handleMu.RLock()
290 for prog := range r.handles {
291 r.handles[prog].lines <- line
292 }
293 r.handleMu.RUnlock()
294 }
295 glog.Info("END OF LINE")
296 glog.Infof("processed %s lines", LineCount.String())
297 close(r.signalQuit)
298 r.handleMu.Lock()
299 for prog := range r.handles {
300 close(r.handles[prog].lines)
301 delete(r.handles, prog)
302 }
303 r.handleMu.Unlock()
304 }()
305 if r.programPath == "" {
306 glog.Info("No program path specified, no programs will be loaded.")
307 return r, nil

Callers 1

initRuntimeMethod · 0.92

Calls 6

SetOptionMethod · 0.95
LoadAllProgramsMethod · 0.95
NewFunction · 0.92
StopMethod · 0.80
StringMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected