Example #1
0
func (c *Container) Start(ctx context.Context) (err error) {
	c.ctx, c.Cancel = context.WithCancel(ctx)

	if c.watch {
		dCli, err := dockercli.NewClient(c.endpoint)
		if err != nil {
			logx.Error(err)
			return err
		}
		ch := make(chan *dockercli.Stats)
		errChan := make(chan error, 1)

		go func() {
			err := dCli.Stats(dockercli.StatsOptions{
				ID:     c.id,
				Stats:  ch,
				Stream: true,
			})
			if err != nil {
				errChan <- err
				close(errChan)
			}
		}()

		go func() {
			warmCount := 0
			for {
				select {
				case <-c.ctx.Done():
					logx.Infof("stop reporting for %s", c.name)
					return
				case err := <-errChan:
					logx.OnError(err)
					return
				case stats := <-ch:
					if !c.warmedUp {
						if warmCount < 4 {
							warmCount++
						} else {
							logx.Infof("start reporting for %s", c.name)
							c.warmedUp = true
						}
					}
					if stats != nil {
						c.updateStats(*stats)
					}
				}
			}
		}()
		logx.Infof("docker container probe for %s started", c.name)
	} else {
		logx.Infof("docker container probe for %s uses dummy mode", c.name)
	}
	return
}
Example #2
0
func (p *Probe) addContainer(id string) (err error) {
	info, err := p.dockerclient.InspectContainer(id)
	if err != nil {
		return
	}

	p.mu.Lock()
	defer p.mu.Unlock()
	_, ok := p.containers[id]
	if !ok {
		cn := newContainer(info, p.endpoint, p.dropper, p.annotator)
		err = cn.Start(p.ctx)
		if err != nil {
			err = logx.OnError(err)
			return
		}
		p.containers[id] = cn
		logx.Infof("add %s to watch", id)
	}
	return
}