Exemplo n.º 1
0
func AppendPackageEvent(site, path, foundWay string, t time.Time, a sppb.HistoryEvent_Action_Enum) error {
	return UpdatePackageHistory(site, path, func(hi *sppb.HistoryInfo) error {
		if hi.FoundTime == nil {
			// The first time the package was found
			hi.FoundTime, _ = ptypes.TimestampProto(t)
			hi.FoundWay = foundWay
		}
		if a == sppb.HistoryEvent_Action_None {
			return nil
		}
		// Insert the event
		tsp, _ := ptypes.TimestampProto(t)
		hi.Events = append([]*sppb.HistoryEvent{{
			Action:    a,
			Timestamp: tsp,
		}}, hi.Events...)
		if len(hi.Events) > maxHistoryEvents {
			hi.Events = hi.Events[:maxHistoryEvents]
		}
		switch a {
		case sppb.HistoryEvent_Action_Success:
			hi.LatestSuccess = tsp
		case sppb.HistoryEvent_Action_Failed:
			hi.LatestFailed = tsp
		}
		return nil
	})
}
Exemplo n.º 2
0
func TestProtoBefore(t *testing.T) {
	now := utcNow()
	nowpb, err := ptypes.TimestampProto(now)
	require.NoError(t, err)

	cases := []struct {
		ts     time.Time
		before bool
	}{
		{
			ts:     now.Add(time.Second),
			before: true,
		}, {
			ts:     now.Add(-time.Second),
			before: false,
		}, {
			ts:     now.Add(time.Nanosecond),
			before: true,
		}, {
			ts:     now.Add(-time.Nanosecond),
			before: false,
		}, {
			ts:     now,
			before: false,
		},
	}

	for _, c := range cases {
		tspb, err := ptypes.TimestampProto(c.ts)
		require.NoError(t, err)

		res := protoBefore(nowpb, tspb)
		require.Equal(t, c.before, res, "protoBefore returned unexpected result")
	}
}
Exemplo n.º 3
0
// SetTimeRange adjust the time range of a silence if allowed. If start or end
// are zero times, the current value remains unmodified.
func (s *Silences) SetTimeRange(id string, start, end time.Time) error {
	now, err := s.nowProto()
	if err != nil {
		return err
	}
	s.mtx.Lock()
	defer s.mtx.Unlock()

	sil, ok := s.getSilence(id)
	if !ok {
		return ErrNotFound
	}

	// Retrieve protobuf start and end time, default to current value
	// of the silence.
	var startp, endp *timestamp.Timestamp
	if start.IsZero() {
		startp = sil.StartsAt
	} else if startp, err = ptypes.TimestampProto(start); err != nil {
		return err
	}
	if end.IsZero() {
		endp = sil.EndsAt
	} else if endp, err = ptypes.TimestampProto(end); err != nil {
		return err
	}

	if sil, err = silenceSetTimeRange(sil, now, startp, endp); err != nil {
		return err
	}
	return s.setSilence(sil)
}
Exemplo n.º 4
0
func TestCheckPackageStatus(t *testing.T) {
	// No crawling info, new package
	assert.Equal(t, "CheckPackageStatus", CheckPackageStatus(&stpb.PackageInfo{}, nil), OutOfDate)
	pkgCrawlTime, _ := ptypes.TimestampProto(time.Now().Add(-5 * timep.Day))

	newRepoInfoCrawlTime, _ := ptypes.TimestampProto(time.Now().Add(-3 * timep.Day))
	newPkgUpdateTime, _ := ptypes.TimestampProto(time.Now().Add(-4 * timep.Day))
	assert.Equal(t, "CheckPackageStatus", CheckPackageStatus(&stpb.PackageInfo{
		CrawlingInfo: &sppb.CrawlingInfo{
			CrawlingTime: pkgCrawlTime,
		},
	}, &sppb.RepoInfo{
		CrawlingTime: newRepoInfoCrawlTime,
		LastUpdated:  newPkgUpdateTime,
	}), OutOfDate)

	newPkgUpdateTime, _ = ptypes.TimestampProto(time.Now().Add(-6 * timep.Day))
	assert.Equal(t, "CheckPackageStatus", CheckPackageStatus(&stpb.PackageInfo{
		CrawlingInfo: &sppb.CrawlingInfo{
			CrawlingTime: pkgCrawlTime,
		},
	}, &sppb.RepoInfo{
		CrawlingTime: newRepoInfoCrawlTime,
		LastUpdated:  newPkgUpdateTime,
	}), UpToDate)
}
Exemplo n.º 5
0
func repoInfoFromGithub(repo *github.Repository) *sppb.RepoInfo {
	ri := &sppb.RepoInfo{
		Description: stringsp.Get(repo.Description),
		Stars:       int32(getInt(repo.StargazersCount)),
	}
	ri.CrawlingTime, _ = ptypes.TimestampProto(time.Now())
	ri.LastUpdated, _ = ptypes.TimestampProto(getTimestamp(repo.PushedAt).Time)
	if repo.Source != nil {
		ri.Source = stringsp.Get(repo.Source.Name)
	}
	return ri
}
Exemplo n.º 6
0
func TestAppendPackageEvent(t *testing.T) {
	const (
		site     = "TestAppendPackageEvent.com"
		path     = "gcse"
		foundWay = "test"
	)
	// Insert a found only event, no action.
	foundTm := time.Now()
	foundTs, _ := ptypes.TimestampProto(foundTm)
	assert.NoError(t, AppendPackageEvent(site, path, "test", foundTm, sppb.HistoryEvent_Action_None))
	h, err := ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay, FoundTime: foundTs})

	// Inser a Success action
	succTm := foundTm.Add(time.Hour)
	succTs, _ := ptypes.TimestampProto(succTm)
	assert.NoError(t, AppendPackageEvent(site, path, "non-test", succTm, sppb.HistoryEvent_Action_Success))
	h, err = ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundWay:  foundWay,
		FoundTime: foundTs,
		Events: []*sppb.HistoryEvent{{
			Timestamp: succTs,
			Action:    sppb.HistoryEvent_Action_Success,
		}},
		LatestSuccess: succTs,
	})
	// Inser a Failed action
	failedTm := succTm.Add(time.Hour)
	failedTs, _ := ptypes.TimestampProto(failedTm)
	assert.NoError(t, AppendPackageEvent(site, path, "", failedTm, sppb.HistoryEvent_Action_Failed))
	h, err = ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundWay:  foundWay,
		FoundTime: foundTs,
		Events: []*sppb.HistoryEvent{{
			Timestamp: failedTs,
			Action:    sppb.HistoryEvent_Action_Failed,
		}, {
			Timestamp: succTs,
			Action:    sppb.HistoryEvent_Action_Success,
		}},
		LatestSuccess: succTs,
		LatestFailed:  failedTs,
	})
}
Exemplo n.º 7
0
func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer) error {
	t := time.Time{}
	if r.Timestamp != nil {
		from, err := ptypes.Timestamp(r.Timestamp)
		if err != nil {
			return err
		}
		t = from
	}
	events := s.sv.Events.Events(t)
	defer s.sv.Events.Unsubscribe(events)
	for e := range events {
		tsp, err := ptypes.TimestampProto(e.Timestamp)
		if err != nil {
			return err
		}
		if err := stream.Send(&types.Event{
			Id:        e.ID,
			Type:      e.Type,
			Timestamp: tsp,
			Pid:       e.PID,
			Status:    uint32(e.Status),
		}); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 8
0
func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer) error {
	t := time.Time{}
	if r.Timestamp != nil {
		from, err := ptypes.Timestamp(r.Timestamp)
		if err != nil {
			return err
		}
		t = from
	}
	if r.StoredOnly && t.IsZero() {
		return fmt.Errorf("invalid parameter: StoredOnly cannot be specified without setting a valid Timestamp")
	}
	events := s.sv.Events(t, r.StoredOnly, r.Id)
	defer s.sv.Unsubscribe(events)
	for e := range events {
		tsp, err := ptypes.TimestampProto(e.Timestamp)
		if err != nil {
			return err
		}
		if r.Id == "" || e.ID == r.Id {
			if err := stream.Send(&types.Event{
				Id:        e.ID,
				Type:      e.Type,
				Timestamp: tsp,
				Pid:       e.PID,
				Status:    uint32(e.Status),
			}); err != nil {
				return err
			}
		}
	}
	return nil
}
Exemplo n.º 9
0
func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid string, closer func()) {
	timestamp := time.Now()
	for {
		e, err := events.Recv()
		if err != nil {
			if grpc.ErrorDesc(err) == transport.ErrConnClosing.Desc {
				closer()
				os.Exit(128 + int(syscall.SIGHUP))
			}
			time.Sleep(1 * time.Second)
			tsp, err := ptypes.TimestampProto(timestamp)
			if err != nil {
				closer()
				fmt.Fprintf(os.Stderr, "%s", err.Error())
				os.Exit(1)
			}
			events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp})
			continue
		}
		timestamp, err = ptypes.Timestamp(e.Timestamp)
		if e.Id == id && e.Type == "exit" && e.Pid == pid {
			closer()
			os.Exit(int(e.Status))
		}
	}
}
Exemplo n.º 10
0
func mustTimestampProto(ts time.Time) *timestamp.Timestamp {
	res, err := ptypes.TimestampProto(ts)
	if err != nil {
		panic(err)
	}
	return res
}
Exemplo n.º 11
0
func entryToPBEntry(entry *Entry) (*protologpb.Entry, error) {
	contexts, err := messagesToEntryMessages(entry.Contexts)
	if err != nil {
		return nil, err
	}
	event, err := messageToEntryMessage(entry.Event)
	if err != nil {
		return nil, err
	}
	pbLevel, ok := levelToPB[entry.Level]
	if !ok {
		return nil, fmt.Errorf("protolog: unknown level: %v", entry.Level)
	}
	timestamp, err := ptypes.TimestampProto(entry.Time)
	if err != nil {
		return nil, err
	}
	return &protologpb.Entry{
		Id:           entry.ID,
		Level:        pbLevel,
		Timestamp:    timestamp,
		Context:      contexts,
		Fields:       entry.Fields,
		Event:        event,
		Message:      entry.Message,
		WriterOutput: entry.WriterOutput,
	}, nil
}
Exemplo n.º 12
0
func TestRepoInfoAge(t *testing.T) {
	ts, _ := ptypes.TimestampProto(time.Now().Add(-time.Hour))
	age := RepoInfoAge(&sppb.RepoInfo{
		CrawlingTime: ts,
	})
	assert.ValueShould(t, "age", age, age >= time.Hour && age < time.Hour+time.Minute, "age out of expected range")
}
Exemplo n.º 13
0
Arquivo: main.go Projeto: hyperhq/runv
func containerEvents(c types.APIClient, container string) <-chan *types.Event {
	evChan := make(chan *types.Event)
	ts := time.Now()
	go func() {
		for {
			tsp, err := ptypes.TimestampProto(ts)
			if err != nil {
				close(evChan)
				return
			}
			events, err := c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp})
			if err != nil {
				fmt.Printf("c.Events error: %v", err)
				// TODO try to find a way to kill the process ?
				close(evChan)
				return
			}
			for {
				e, err := events.Recv()
				if err != nil {
					time.Sleep(1 * time.Second)
					break
				}
				ts, err = ptypes.Timestamp(e.Timestamp)
				if e.Id == container {
					evChan <- e
				}
			}
		}
	}()
	return evChan
}
Exemplo n.º 14
0
func bestMomentInHistory() (*Operation, error) {
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2009-11-10 23:00:00 +0000 UTC")
	if err != nil {
		return nil, err
	}
	resp, err := ptypes.TimestampProto(t)
	if err != nil {
		return nil, err
	}
	respAny, err := ptypes.MarshalAny(resp)
	if err != nil {
		return nil, err
	}
	metaAny, err := ptypes.MarshalAny(ptypes.DurationProto(1 * time.Hour))
	return &Operation{
		proto: &pb.Operation{
			Name:     "best-moment",
			Done:     true,
			Metadata: metaAny,
			Result: &pb.Operation_Response{
				Response: respAny,
			},
		},
	}, err
}
Exemplo n.º 15
0
func toLogEntry(e Entry) (*logpb.LogEntry, error) {
	if e.LogName != "" {
		return nil, errors.New("logging: Entry.LogName should be not be set when writing")
	}
	t := e.Timestamp
	if t.IsZero() {
		t = now()
	}
	ts, err := ptypes.TimestampProto(t)
	if err != nil {
		return nil, err
	}
	ent := &logpb.LogEntry{
		Timestamp:   ts,
		Severity:    logtypepb.LogSeverity(e.Severity),
		InsertId:    e.InsertID,
		HttpRequest: fromHTTPRequest(e.HTTPRequest),
		Operation:   e.Operation,
		Labels:      e.Labels,
	}

	switch p := e.Payload.(type) {
	case string:
		ent.Payload = &logpb.LogEntry_TextPayload{p}
	default:
		s, err := toProtoStruct(p)
		if err != nil {
			return nil, err
		}
		ent.Payload = &logpb.LogEntry_JsonPayload{s}
	}
	return ent, nil
}
Exemplo n.º 16
0
func init() {
	var err error
	unixZeroTimestamp, err = ptypes.TimestampProto(time.Unix(0, 0))
	if err != nil {
		panic(err)
	}
}
Exemplo n.º 17
0
func (l *nlog) log(r *pb.Receiver, gkey, ghash []byte, resolved bool) error {
	// Write all st with the same timestamp.
	now := l.now()
	key := stateKey(gkey, r)

	l.mtx.Lock()
	defer l.mtx.Unlock()

	if prevle, ok := l.st[key]; ok {
		// Entry already exists, only overwrite if timestamp is newer.
		// This may with raciness or clock-drift across AM nodes.
		prevts, err := ptypes.Timestamp(prevle.Entry.Timestamp)
		if err != nil {
			return err
		}
		if prevts.After(now) {
			return nil
		}
	}

	ts, err := ptypes.TimestampProto(now)
	if err != nil {
		return err
	}
	expts, err := ptypes.TimestampProto(now.Add(l.retention))
	if err != nil {
		return err
	}

	e := &pb.MeshEntry{
		Entry: &pb.Entry{
			Receiver:  r,
			GroupKey:  gkey,
			GroupHash: ghash,
			Resolved:  resolved,
			Timestamp: ts,
		},
		ExpiresAt: expts,
	}
	l.gossip.GossipBroadcast(gossipData{
		key: e,
	})
	l.st[key] = e

	return nil
}
Exemplo n.º 18
0
func (cs *ContainerdSuite) Events(from time.Time, storedOnly bool, id string) (types.API_EventsClient, error) {
	var (
		ftsp *timestamp.Timestamp
		err  error
	)
	if !from.IsZero() {
		ftsp, err = ptypes.TimestampProto(from)
		if err != nil {
			return nil, err
		}
	}

	return cs.grpcClient.Events(context.Background(), &types.EventsRequest{Timestamp: ftsp, StoredOnly: storedOnly, Id: id})
}
Exemplo n.º 19
0
func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
	startsAt, err := ptypes.TimestampProto(s.StartsAt)
	if err != nil {
		return nil, err
	}
	endsAt, err := ptypes.TimestampProto(s.EndsAt)
	if err != nil {
		return nil, err
	}
	updatedAt, err := ptypes.TimestampProto(s.UpdatedAt)
	if err != nil {
		return nil, err
	}
	sil := &silencepb.Silence{
		Id:        s.ID,
		StartsAt:  startsAt,
		EndsAt:    endsAt,
		UpdatedAt: updatedAt,
	}
	for _, m := range s.Matchers {
		matcher := &silencepb.Matcher{
			Name:    m.Name,
			Pattern: m.Value,
			Type:    silencepb.Matcher_EQUAL,
		}
		if m.IsRegex {
			matcher.Type = silencepb.Matcher_REGEXP
		}
		sil.Matchers = append(sil.Matchers, matcher)
	}
	sil.Comments = append(sil.Comments, &silencepb.Comment{
		Timestamp: updatedAt,
		Author:    s.CreatedBy,
		Comment:   s.Comment,
	})
	return sil, nil
}
Exemplo n.º 20
0
func (r *remote) startEventsMonitor() error {
	// First, get past events
	t := r.getLastEventTimestamp()
	tsp, err := ptypes.TimestampProto(t)
	if err != nil {
		logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
	}
	er := &containerd.EventsRequest{
		Timestamp: tsp,
	}
	events, err := r.apiClient.Events(context.Background(), er, grpc.FailFast(false))
	if err != nil {
		return err
	}
	go r.handleEventStream(events)
	return nil
}
Exemplo n.º 21
0
func (s *Silences) setSilence(sil *pb.Silence) error {
	endsAt, err := ptypes.Timestamp(sil.EndsAt)
	if err != nil {
		return err
	}
	expiresAt, err := ptypes.TimestampProto(endsAt.Add(s.retention))
	if err != nil {
		return err
	}

	msil := &pb.MeshSilence{
		Silence:   sil,
		ExpiresAt: expiresAt,
	}
	st := gossipData{sil.Id: msil}

	s.st.Merge(st)
	s.gossip.GossipBroadcast(st)

	return nil
}
Exemplo n.º 22
0
func TestDoFill(t *testing.T) {
	const (
		site = "github.com"
		path = "daviddengcn/gcse"
	)
	tm := time.Now().Add(-20 * timep.Day)
	cDB := gcse.LoadCrawlerDB()
	cDB.PackageDB.Put(site+"/"+path, gcse.CrawlingEntry{
		ScheduleTime: tm.Add(10 * timep.Day),
	})
	assert.NoError(t, cDB.Sync())

	assert.NoError(t, doFill())

	h, err := store.ReadPackageHistory(site, path)
	assert.NoError(t, err)
	ts, _ := ptypes.TimestampProto(tm)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundTime: ts,
		FoundWay:  "unknown",
	})
}
Exemplo n.º 23
0
func (clnt *client) getContainerLastEvent(id string) (*containerd.Event, error) {
	ev, err := clnt.getContainerLastEventSinceTime(id, clnt.remote.restoreFromTimestamp)
	if err == nil && ev == nil {
		// If ev is nil and the container is running in containerd,
		// we already consumed all the event of the
		// container, included the "exit" one.
		// Thus, we request all events containerd has in memory for
		// this container in order to get the last one (which should
		// be an exit event)
		logrus.Warnf("libcontainerd: client is out of sync, restore was called on a fully synced container (%s).", id)
		// Request all events since beginning of time
		t := time.Unix(0, 0)
		tsp, err := ptypes.TimestampProto(t)
		if err != nil {
			logrus.Errorf("libcontainerd: getLastEventSinceTime() failed to convert timestamp: %q", err)
			return nil, err
		}

		return clnt.getContainerLastEventSinceTime(id, tsp)
	}

	return ev, err
}
Exemplo n.º 24
0
// New creates a fresh instance of libcontainerd remote.
func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
	defer func() {
		if err != nil {
			err = fmt.Errorf("Failed to connect to containerd. Please make sure containerd is installed in your PATH or you have specificed the correct address. Got error: %v", err)
		}
	}()
	r := &remote{
		stateDir:    stateDir,
		daemonPid:   -1,
		eventTsPath: filepath.Join(stateDir, eventTimestampFilename),
	}
	for _, option := range options {
		if err := option.Apply(r); err != nil {
			return nil, err
		}
	}

	if err := sysinfo.MkdirAll(stateDir, 0700); err != nil {
		return nil, err
	}

	if r.rpcAddr == "" {
		r.rpcAddr = filepath.Join(stateDir, containerdSockFilename)
	}

	if r.startDaemon {
		if err := r.runContainerdDaemon(); err != nil {
			return nil, err
		}
	}

	// don't output the grpc reconnect logging
	grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
	dialOpts := append([]grpc.DialOption{grpc.WithInsecure()},
		grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
			return net.DialTimeout("unix", addr, timeout)
		}),
	)
	conn, err := grpc.Dial(r.rpcAddr, dialOpts...)
	if err != nil {
		return nil, fmt.Errorf("error connecting to containerd: %v", err)
	}

	r.rpcConn = conn
	r.apiClient = containerd.NewAPIClient(conn)

	// Get the timestamp to restore from
	t := r.getLastEventTimestamp()
	tsp, err := ptypes.TimestampProto(t)
	if err != nil {
		logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
	}
	r.restoreFromTimestamp = tsp

	go r.handleConnectionChange()

	if err := r.startEventsMonitor(); err != nil {
		return nil, err
	}

	return r, nil
}
Exemplo n.º 25
0
			Usage: "get events from a specific time stamp in RFC3339Nano format",
		},
	},
	Action: func(context *cli.Context) {
		var (
			t = time.Time{}
			c = getClient(context)
		)
		if ts := context.String("timestamp"); ts != "" {
			from, err := time.Parse(time.RFC3339Nano, ts)
			if err != nil {
				fatal(err.Error(), 1)
			}
			t = from
		}
		tsp, err := ptypes.TimestampProto(t)
		if err != nil {
			fatal(err.Error(), 1)
		}
		events, err := c.Events(netcontext.Background(), &types.EventsRequest{
			Timestamp: tsp,
		})
		if err != nil {
			fatal(err.Error(), 1)
		}
		w := tabwriter.NewWriter(os.Stdout, 31, 1, 1, ' ', 0)
		fmt.Fprint(w, "TIME\tTYPE\tID\tPID\tSTATUS\n")
		w.Flush()
		for {
			e, err := events.Recv()
			if err != nil {
Exemplo n.º 26
0
func convertToPb(st *runtime.Stat) *types.StatsResponse {
	tsp, _ := ptypes.TimestampProto(st.Timestamp)
	pbSt := &types.StatsResponse{
		Timestamp:   tsp,
		CgroupStats: &types.CgroupStats{},
	}
	systemUsage, _ := getSystemCPUUsage()
	pbSt.CgroupStats.CpuStats = &types.CpuStats{
		CpuUsage: &types.CpuUsage{
			TotalUsage:        st.CPU.Usage.Total,
			PercpuUsage:       st.CPU.Usage.Percpu,
			UsageInKernelmode: st.CPU.Usage.Kernel,
			UsageInUsermode:   st.CPU.Usage.User,
		},
		ThrottlingData: &types.ThrottlingData{
			Periods:          st.CPU.Throttling.Periods,
			ThrottledPeriods: st.CPU.Throttling.ThrottledPeriods,
			ThrottledTime:    st.CPU.Throttling.ThrottledTime,
		},
		SystemUsage: systemUsage,
	}
	pbSt.CgroupStats.MemoryStats = &types.MemoryStats{
		Cache: st.Memory.Cache,
		Usage: &types.MemoryData{
			Usage:    st.Memory.Usage.Usage,
			MaxUsage: st.Memory.Usage.Max,
			Failcnt:  st.Memory.Usage.Failcnt,
			Limit:    st.Memory.Usage.Limit,
		},
		SwapUsage: &types.MemoryData{
			Usage:    st.Memory.Swap.Usage,
			MaxUsage: st.Memory.Swap.Max,
			Failcnt:  st.Memory.Swap.Failcnt,
			Limit:    st.Memory.Swap.Limit,
		},
		KernelUsage: &types.MemoryData{
			Usage:    st.Memory.Kernel.Usage,
			MaxUsage: st.Memory.Kernel.Max,
			Failcnt:  st.Memory.Kernel.Failcnt,
			Limit:    st.Memory.Kernel.Limit,
		},
		Stats: st.Memory.Raw,
	}
	pbSt.CgroupStats.BlkioStats = &types.BlkioStats{
		IoServiceBytesRecursive: convertBlkioEntryToPb(st.Blkio.IoServiceBytesRecursive),
		IoServicedRecursive:     convertBlkioEntryToPb(st.Blkio.IoServicedRecursive),
		IoQueuedRecursive:       convertBlkioEntryToPb(st.Blkio.IoQueuedRecursive),
		IoServiceTimeRecursive:  convertBlkioEntryToPb(st.Blkio.IoServiceTimeRecursive),
		IoWaitTimeRecursive:     convertBlkioEntryToPb(st.Blkio.IoWaitTimeRecursive),
		IoMergedRecursive:       convertBlkioEntryToPb(st.Blkio.IoMergedRecursive),
		IoTimeRecursive:         convertBlkioEntryToPb(st.Blkio.IoTimeRecursive),
		SectorsRecursive:        convertBlkioEntryToPb(st.Blkio.SectorsRecursive),
	}
	pbSt.CgroupStats.HugetlbStats = make(map[string]*types.HugetlbStats)
	for k, st := range st.Hugetlb {
		pbSt.CgroupStats.HugetlbStats[k] = &types.HugetlbStats{
			Usage:    st.Usage,
			MaxUsage: st.Max,
			Failcnt:  st.Failcnt,
		}
	}
	pbSt.CgroupStats.PidsStats = &types.PidsStats{
		Current: st.Pids.Current,
		Limit:   st.Pids.Limit,
	}
	return pbSt
}
Exemplo n.º 27
0
func TestFromLogEntry(t *testing.T) {
	now := time.Now()
	res := &mrpb.MonitoredResource{Type: "global"}
	ts, err := ptypes.TimestampProto(now)
	if err != nil {
		t.Fatal(err)
	}
	logEntry := logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Payload:   &logpb.LogEntry_TextPayload{"hello"},
		Timestamp: ts,
		Severity:  logtypepb.LogSeverity_INFO,
		InsertId:  "123",
		HttpRequest: &logtypepb.HttpRequest{
			RequestMethod:                  "GET",
			RequestUrl:                     "http:://example.com/path?q=1",
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			Latency:                        &durpb.Duration{Seconds: 100},
			UserAgent:                      "user-agent",
			RemoteIp:                       "127.0.0.1",
			Referer:                        "referer",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
	}
	u, err := url.Parse("http:://example.com/path?q=1")
	if err != nil {
		t.Fatal(err)
	}
	want := &logging.Entry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: now.In(time.UTC),
		Severity:  logging.Info,
		Payload:   "hello",
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
		InsertID: "123",
		HTTPRequest: &logging.HTTPRequest{
			Request: &http.Request{
				Method: "GET",
				URL:    u,
				Header: map[string][]string{
					"User-Agent": []string{"user-agent"},
					"Referer":    []string{"referer"},
				},
			},
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			Latency:                        100 * time.Second,
			RemoteIP:                       "127.0.0.1",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
	}
	got, err := fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	// Test sub-values separately because %+v and %#v do not follow pointers.
	// TODO(jba): use a differ or pretty-printer.
	if !reflect.DeepEqual(got.HTTPRequest.Request, want.HTTPRequest.Request) {
		t.Fatalf("HTTPRequest.Request:\ngot  %+v\nwant %+v", got.HTTPRequest.Request, want.HTTPRequest.Request)
	}
	if !reflect.DeepEqual(got.HTTPRequest, want.HTTPRequest) {
		t.Fatalf("HTTPRequest:\ngot  %+v\nwant %+v", got.HTTPRequest, want.HTTPRequest)
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("FullEntry:\ngot  %+v\nwant %+v", got, want)
	}

	// Proto payload.
	alog := &audit.AuditLog{
		ServiceName:  "svc",
		MethodName:   "method",
		ResourceName: "shelves/S/books/B",
	}
	any, err := ptypes.MarshalAny(alog)
	if err != nil {
		t.Fatal(err)
	}
	logEntry = logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: ts,
		Payload:   &logpb.LogEntry_ProtoPayload{any},
	}
	got, err = fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(got.Payload, alog) {
		t.Errorf("got %+v, want %+v", got.Payload, alog)
	}

	// JSON payload.
	jstruct := &structpb.Struct{map[string]*structpb.Value{
		"f": &structpb.Value{&structpb.Value_NumberValue{3.1}},
	}}
	logEntry = logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: ts,
		Payload:   &logpb.LogEntry_JsonPayload{jstruct},
	}
	got, err = fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(got.Payload, jstruct) {
		t.Errorf("got %+v, want %+v", got.Payload, jstruct)
	}
}
Exemplo n.º 28
0
func TestFromLogEntry(t *testing.T) {
	now := time.Now()
	res := &mrpb.MonitoredResource{Type: "global"}
	ts, err := ptypes.TimestampProto(now)
	if err != nil {
		t.Fatal(err)
	}
	logEntry := logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Payload:   &logpb.LogEntry_TextPayload{"hello"},
		Timestamp: ts,
		Severity:  logtypepb.LogSeverity_INFO,
		InsertId:  "123",
		HttpRequest: &logtypepb.HttpRequest{
			RequestMethod:                  "GET",
			RequestUrl:                     "http:://example.com/path?q=1",
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			UserAgent:                      "user-agent",
			RemoteIp:                       "127.0.0.1",
			Referer:                        "referer",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
	}
	u, err := url.Parse("http:://example.com/path?q=1")
	if err != nil {
		t.Fatal(err)
	}
	want := &logging.Entry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: now.In(time.UTC),
		Severity:  logging.Info,
		Payload:   "hello",
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
		InsertID: "123",
		HTTPRequest: &logging.HTTPRequest{
			Request: &http.Request{
				Method: "GET",
				URL:    u,
				Header: map[string][]string{
					"User-Agent": []string{"user-agent"},
					"Referer":    []string{"referer"},
				},
			},
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			RemoteIP:                       "127.0.0.1",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
	}
	got, err := fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	// Test sub-values separately because %+v and %#v do not follow pointers.
	// TODO(jba): use a differ or pretty-printer.
	if !reflect.DeepEqual(got.HTTPRequest.Request, want.HTTPRequest.Request) {
		t.Fatalf("HTTPRequest.Request:\ngot  %+v\nwant %+v", got.HTTPRequest.Request, want.HTTPRequest.Request)
	}
	if !reflect.DeepEqual(got.HTTPRequest, want.HTTPRequest) {
		t.Fatalf("HTTPRequest:\ngot  %+v\nwant %+v", got.HTTPRequest, want.HTTPRequest)
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("FullEntry:\ngot  %+v\nwant %+v", got, want)
	}
}
Exemplo n.º 29
0
func (s *Silences) nowProto() (*timestamp.Timestamp, error) {
	now := s.now()
	return ptypes.TimestampProto(now)
}