Giter VIP home page Giter VIP logo

measure-cpu's Introduction

Measure CPU on the clients

The problem in a nutshell

Our clients sometimes have high CPU usage. We don't know what causes this, and we would like to collect analytical data, that can show us the reason.

The idea is to collect important events from the client, and by analyzing it, show the possible causes. These important events are periodic CPU usage events, file transfer events (start, end, progress), client app opened/closed, and anything that could help us pinpoint the problem.

These events then ingested into an analytical engine, that tries to show us the most likely cause. A very simple method is to show correlation between the derivative of the CPU usage is high, and the occurrence of an event type. It could be further narrowed down with grouping by client OS, client version, OS version, or anything that distributes the outcome. This should take us much closer to identifying the cause.

Assumptions

  • We have clients around the world, and we don't want them to have huge latencies.
  • We already have a method for authenticating the clients (e.g. JWT)

Not investigated

  • GDPR (what data can we collect, how we must store them)

Client

The client sends the events through a REST API. This API is provided in multiple regions, and the DNS routes the client to the appropriate endpoint. The client also sends metadata about itself in HTTP headers.

Sending the events is a two-step process. As the client might not be able to reach the service (e.g. no internet connection, our service is down, not enough CPU to send events), it first stores the events in local storage. We assume that this has a high chance to succeed. The client has a separate service that checks the events written to local storage, and knows the last event sent (if it doesn't know it yet, it can query through the REST API), and sends the events that haven't been sent yet.

Server

The server has distributed event ingestion in multiple regions. The ingestion just puts the events onto a message bus (e.g. Kafka), and the events on the message bus are collected into one computing region (e.g. Kafka Mirrormaker).

The first step in the computing region is to deduplicate the events. Events can get duplicated because of client retries, or by Kafka itself (at least once guarantee). We consider the (timestamp, eventType, clientId) tuple unique, and the deduplication can be handled with Bloom filters. From this point we can use a message bus that can give us exactly once guarantee (e.g. Kafka Transactions) , so we don't have to worry about duplication.

The next step is to store the data in a storage solution, where the performance characteristics match our expectations. As we probably need to query time series of events, Prometheus could be a good fit, but it's hard to tell without building of proof of concept and measuring our queries.

The final step is a tool that helps us analyze the data. In case of Prometheus, Grafana is a straightforward choice. We can build graphs that can visualize the relation between the events and the CPU usage.

The ingestion has to know the last event we have seen for a client in any region. Fortunately, eventual consistency is enough in this case. Any cheap database that can handle this multi-region situation with eventual consistency is good enough (e.g. AWS Dynamo DB).

The above steps can be implemented in separate microservices, such that we can scale them differently.

Advantages of this design

  • The API is designed in a CQRS fashion. This allows us to scale the writes ( event ingestion) and the reads (data analyzation) differently, as these have quite different characteristics.
  • The client and the server can make progress without the other
  • The services are stateless, which makes restarting and scaling horizontally easier
  • A partitioned message bus can balance the load nicely

Architecture diagram

Architecture Diagram

Implementation

The implementation in this repository contains an API, a client that periodically sends fake CPU measurement events, and a server where each microservice is its own typeclass. Each of them has a very simple implementation, and some very basic analytics queries are implemented as well. This implementation does not try to be complete, it rather tries to provide a proof of concept and prove that the shapes fit together nicely.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.