En introduktion til metrics-driven udvikling: Hvad er metrics, og hvorfor skal du bruge dem?

En af de sejeste ting, jeg har lært det sidste år, er, hvordan man konstant leverer værdi i produktionen uden at forårsage for meget kaos.

I dette indlæg forklarer jeg metrics-driven udviklingstilgang, og hvordan det hjalp mig med at opnå det. I slutningen af ​​indlægget kan du besvare følgende spørgsmål:

  • Hvad er metrics, og hvorfor skal jeg bruge dem
  • Hvad er de forskellige typer målinger
  • Hvilke værktøjer kunne jeg bruge til at gemme og vise metrics
  • Hvad er et virkeligt eksempel på metrics-driven udvikling

Hvad er metrics, og hvorfor skal jeg bruge dem?

Metrics giver dig mulighed for at indsamle oplysninger om et aktivt kørende system uden at ændre dets kode.

Det giver dig mulighed for at få værdifulde data om din applikations opførsel, mens den kører, så du kan tage datadrevne beslutninger baseret på ægte kundefeedback og brug i produktionen.

Hvilke typer målinger er tilgængelige for mig?

Dette er de mest almindelige målinger, der bruges i dag:

  • Counter - repræsenterer en monotont stigende værdi.

I dette eksempel bruges en modmåling til at beregne hastigheden af ​​begivenheder over tid ved at tælle begivenheder pr. Sekund

  • Gauge - repræsenterer en enkelt værdi, der kan gå op eller ned.

I dette eksempel bruges en målemetrik til at overvåge brugerens CPU i procent

  • Histogram - En optælling af observationer (som anmodningens varighed eller størrelser) i konfigurerbare spande.

I dette eksempel bruges en histogrammåling til at beregne de 75. og 90. percentiler af en HTTP-anmodningsvarighed.

Bits og bytes af typerne: tæller, histogram og måler kan være ret forvirrende. Prøv at læse om det yderligere her.

Hvilke værktøjer kan jeg bruge til at gemme og vise metrics?

De fleste overvågningssystemer består af et par dele:

  1. Tidsseriedatabase - En databasesoftware, der optimerer lagring og servering af tidsseriedata. To eksempler på denne type database er Whisper og Prometheus.
  2. Forespørgselsmotor (med et forespørgselssprog) - To eksempler på almindelige forespørgselsmotorer er: Grafit og PromQL
  3. Alarmeringssystem - Den mekanisme, der giver dig mulighed for at konfigurere alarmer baseret på grafer oprettet af forespørgselssproget. Systemet kan sende disse alarmer til Mail, Slack, PagerDuty. To eksempler på almindelige alarmsystemer er: Grafana og Prometheus.
  4. UI - Giver dig mulighed for at se de grafer, der er genereret af de indgående data, og konfigurere forespørgsler og alarmer. To eksempler på almindelige brugergrænsefladesystemer er: Grafit og Grafana

Den opsætning, vi bruger i dag i BigPanda Engineering er

  • Telegraf - bruges som en StatsD-server.
  • Prometheus - bruges som vores skrotmotor, tidsseriedatabase og forespørgselsmotor.
  • Grafana - bruges til Alerting og UI

Og de begrænsninger, vi havde i tankerne, da vi valgte denne stak var:

  • Vi ønsker skalerbar og elastisk metrisk skrabning
  • Vi vil have en performant forespørgselsmotor
  • Vi ønsker muligheden for at forespørge vores metrics ved hjælp af brugerdefinerede tags (såsom servicenavne, værter osv.)

Et eksempel på den virkelige verden af ​​metrics-driven udvikling af en sentimentanalysetjeneste

Lad os udvikle en ny pipeline-tjeneste, der beregner følelser baseret på tekstindgange og gør det på en metrics-driven udvikling måde!

Lad os sige, at jeg har brug for at udvikle denne pipeline-tjeneste:

Og dette er min sædvanlige udviklingsproces:

Så jeg skriver følgende implementering:

let senService: SentimentAnalysisService = new SentimentAnalysisService(); while (true) { let tweetInformation = kafkaConsumer.consume() let deserializedTweet: { msg: string } = deSerialize(tweetInformation) let sentimentResult = senService.calculateSentiment(deserializedTweet.msg) let serializedSentimentResult = serialize(sentimentResult) sentimentStore.store(sentimentResult); kafkaProducer.produce(serializedSentimentResult, 'sentiment_topic', 0); } 

Den fulde kerne kan findes her.

Og hans metode fungerer helt fint .

Men hvad sker der, når det ikke gør det ?

Virkeligheden er, at mens vi arbejder (i en agil udviklingsproces) laver vi fejl. Det er en kendsgerning i livet.

Jeg tror, ​​at den virkelige udfordring ved at lave fejl ikke er at undgå dem, men snarere at optimere, hvor hurtigt vi opdager og reparerer dem. Så vi er nødt til at få evnen til hurtigt at opdage vores fejl.  

Det er tid til MDD-måde.

Metrics Driven Development (MDD) måde

The MDD approach is heavily inspired by the Three Commandments of Production (which I had learned about the hard way).

The Three Commandments of Production are:

  1. There are mistakes and bugs in the code you write and deploy.
  2. The data flowing in production is unpredictable and unique!
  3. Perfect your code from real customer feedback and usage in production.

And since we now know the Commandments, it's time to go over the 4 step plan of the Metrics-Driven development process.

The 4-step plan for a successful MDD

Develop code 

I write the code, and whenever possible, wrap it with a feature flag that  allows me to gradually open it for users.

Metrics

This consists of two parts:

Add metrics on relevant parts

In this part, I ask myself what are the success or failure metrics I can define to make sure my feature works? In this case, does my new pipeline application perform its logic correctly?

Add alerts on top of them so that I’ll be alerted when a bug occurs

In this part, I ask myself What metric could alert me if I forgot something or did not implement it correctly?

Deployment

I deploy the code and immediately monitor it to verify that it’s behaving as I have anticipated.

Iterate this process to perfection

And that's it! Now that we have learned the process, let's tackle an important task inside it.

Metrics to Report — what should we monitor?

One of the toughest questions for me, when I’m doing MDD, is: “what should I monitor”?

In order to answer the question, lets try to zoom out and look at the big picture.

All the possible information available to monitor can be divided into two parts:

  1. Applicative information — Information that has an applicative context and meaning. An example of this will be — “How many tweets did we classify as positive in the last hour”?
  2. Operational information — Information that is related to the infrastructure that surrounds our application — Cloud data, CPU and disk utilization, network usage, etc.

Now, since we cannot monitor everything, we need to choose what applicative and operational information we want to monitor.

  • The operational part really depends on your ops stack and has built-in solutions for (almost) all your monitoring needs.
  • The applicative part is more unique to your needs, and I'll try to explain how I think about it later in this post.

After we do that, we can ask ourselves the question: what alerts do we want to set up on top of the metrics we just defined?

The diagram (of information, metrics, alerts) can be drawn like this:

Applicative metrics

I usually add applicative metrics out of two needs:

To answer questions

A question is something like, “When my service misbehaves, what information would be helpful to know about?”

Some answers to that question can be — latencies of all IO calls, processing rate, throughput, etc…

Most of these questions will be helpful while you are searching for the answer. But once you found it, chances are you will not look at it again (since you already know the answer).

These questions are usually driven by RND and are (usually) used to gather information internally.

To add Alerts

This may sound backward, but I usually add applicative metrics in order to define alerts on top of them. Meaning, we define the list of alerts and then deduce from them what are the applicative metrics to report.

These alerts are derived from the SLA of the product and are usually treated with mission-critical importance.

Common types of alerts

Alerts can be broken down into three parts:

SLA Alerts

SLA alerts surround the places in our system where an SLA is specified to meet explicit customer or internal requirements (i.e availability, throughput, latency, etc.). SLA breaches involve paging RND and waking people up, so try to keep the alerts in this list to a minimum.

Also, we can define Degradation Alerts in addition to SLA Alerts.

Degradation alerts are defined with lower thresholds then SLA alerts, and are therefore useful in reducing the amount of SLA breaches — by giving you a proper heads-up before they happen.

An example of an SLA alert would be, “All sentiment requests must finish in under 500ms.”

An example of a Degradation Alert will be: “All sentiment requests must finish in under 400ms”.

These are the alerts I defined:

  1. Latency — I expect the 90th percentile of a single request duration not to exceed 300ms.
  2. Success/Failure ratio of requests — I expect the number of failures per second, success per second, to remain under 0.01.
  3. Throughput — I expect that the number of operations per second (ops) that the application handles will be > 200
  4. Data Size — I expect the amount of data that we store in a single day should not exceed 2GB.
200 ops * 60 bytes(Size of Sentiment Result)* 86400 sec in a day = 1GB < 2GB

Baseline Breaching Alerts

These alerts usually involve measuring and defining a baseline and making sure it doesn’t (dramatically) change over time with alerts.

For example, the 99th processing latency for an event must stay relatively the same across time unless we have made dramatic changes to the logic.

These are the alerts I defined:

  1. Amount of Positive or Neutral or Negative Sentiment tweets — If for whatever reason, the sum of Positive tweets has increased or decreased dramatically, I might have a bug somewhere in my application.
  2. All latency \ Success ratio of requests \ Throughput \ Data size must not increase\decrease dramatically over time.

Runtime Properties Alerts

I’ve given a talk about Property-Based Tests and their insane strength. As it turns out, collecting metrics allows us to run property-based tests on our system in production!

Some properties of our system:

  1. Since we consume messages from a Kafka topic, the handled offset must monotonically increase over time.
  2. 1 ≥ sentiment score ≥ 0
  3. A tweet should classify as either Negative \ Positive \ Neutral.
  4. A tweet classification must be unique.

These alerts helped me validate that:

  1. We are reading with the same group-id. Changing consumer group ids by mistake in deployment is a common mistake when using Kafka. It causes a lot of mayhem in production.
  2. The sentiment score is consistently between 0 and 1.
  3. Tweet category length should always be 1.

In order to define these alerts, you need to submit metrics from your application. Go here for the complete metrics list.

Using these metrics, I can create alerts that will “page” me whenever one of these properties do not hold anymore in production.

Let’s take a look at a possible implementation of all these metrics

import SDC = require("statsd-client"); let sdc = new SDC({ host: 'localhost' }); let senService: SentimentAnalysisService; //... while (true) { let tweetInformation = kafkaConsumer.consume() sdc.increment('incoming_requests_count') let deserializedTweet: { msg: string } = deSerialize(tweetInformation) sdc.histogram('request_size_chars', deserializedTweet.msg.length); let sentimentResult = senService.calculateSentiment(deserializedTweet.msg) if (sentimentResult !== undefined) { let serializedSentimentResult = serialize(sentimentResult) sdc.histogram('outgoing_event_size_chars', serializedSentimentResult.length); sentimentStore.store(sentimentResult) kafkaProducer.produce(serializedSentimentResult, 'sentiment_topic', 0); } } 

The full code can be found here

A few thoughts on the code example above:

  1. There has been a staggering amount of metrics added to this codebase.
  2. Metrics add complexity to the codebase, so, like all good things, add them responsibly and in moderation.
  3. Choosing correct metric names is hard. Take your time selecting proper names. Here’s an excellent post about this.
  4. You still need to collect these metrics and display them in a monitoring system (like Grafana), plus add alerts on top of them, but that’s a topic for a different post.

Did we reach the initial goal of identifying issues and resolving them faster?

We can now make sure the application latency and throughput do not degrade over time. Also, adding alerts on these metrics allows for a much faster issue discovery and resolution.

Conclusion

Metrics-driven development goes hand in hand with CI\CD, DevOps, and agile development process. If you are using any of the above keywords, then you are in the right place.

When done right, metrics make you feel more confident in your deployment in the same way that seeing passing unit-tests in your build makes you feel confident in the code you write.

Tilføjelse af metrics giver dig mulighed for at implementere kode og føle dig sikker på, at dit produktionsmiljø er stabilt, og at din applikation opfører sig som forventet over tid. Så jeg opfordrer dig til at prøve det!

Nogle referencer

  1. Her er et link til koden, der vises i dette indlæg, og her er den fulde liste over metrics beskrevet.
  2. Hvis du er ivrig efter at prøve at skrive nogle målinger og forbinde dem til et overvågningssystem, skal du tjekke Prometheus, Grafana og muligvis dette indlæg
  3. Denne fyr skrev et dejligt indlæg om metrics-driven udvikling. Gå og læs det.