西维蜀黍

【Golang】Linters - gofmt

Introduction

Gofmt is a tool that automatically formats Go source code.

Gofmt’d code is:

  • easier to write: never worry about minor formatting concerns while hacking away,
  • easier to read: when all code looks the same you need not mentally convert others’ formatting style into something you can understand.
  • easier to maintain: mechanical changes to the source don’t cause unrelated changes to the file’s formatting; diffs show only the real changes.
  • uncontroversial: never have a debate about spacing or brace position ever again!
  ...


【Golang】Linters

Go Focused

  • GolangCI - Open Source SaaS service for running linters on Github pull requests. Free for Open Source.

  • Go Report Card - Go repo report card.

  ...


【Golang】Test - Unit Test - Testify

assert package

The assert package provides some helpful methods that allow you to write better test code in Go.

  • Prints friendly, easy to read failure descriptions
  • Allows for very readable code
  • Optionally annotate each assertion with a message
  ...


【Golang】Test - Unit Test

Unit Tests

Typically, the code we’re testing would be in a source file named something like intutils.go, and the test file for it would then be named intutils_test.go.

  ...


【Prometheus】Built-in Functions

rate() - per-second average rate

rate(v range-vector) calculates the per-second average rate of increase of the time series in the range vector(每秒增量的平均值).

Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for.

  • Also, the calculation extrapolates to the ends of the time range, allowing for missed scrapes or imperfect alignment of scrape cycles with the range’s time period.

The following example expression returns the per-second rate of HTTP requests as measured over the last 5 minutes, per time series in the range vector:

rate(http_requests_total{job="api-server"}[5m])
  ...


【Prometheus】Counter

  ...


【Prometheus】Metric Name

Metric Names

A metric name…

  • …must comply with the data model for valid characters.
  • …should have a (single-word) application prefix relevant to the domain the metric belongs to. The prefix is sometimes referred to as namespace by client libraries. For metrics specific to an application, the prefix is usually the application name itself. Sometimes, however, metrics are more generic, like standardized metrics exported by client libraries. Examples:
    • prometheus_notifications_total (specific to the Prometheus server)
    • process_cpu_seconds_total (exported by many client libraries)
    • http_request_duration_seconds` (for all HTTP requests)
  • …must have a single unit (i.e. do not mix seconds with milliseconds, or seconds with bytes).
  • …should use base units (e.g. seconds, bytes, meters - not milliseconds, megabytes, kilometers).
  • …should have a suffix describing the unit, in plural form. Note that an accumulating count has total as a suffix, in addition to the unit if applicable.
    • http_request_duration_**seconds**
    • node_memory_usage_**bytes**
    • http_requests_**total** (for a unit-less accumulating count)
    • process_cpu_**seconds_total** (for an accumulating count with unit)
    • foobar_build**_info** (for a pseudo-metric that provides metadata about the running binary)
  • …should represent the same logical thing-being-measured across all label dimensions.
    • request duration
    • bytes of data transfer
    • instantaneous resource usage as a percentage

As a rule of thumb, either the sum() or the avg() over all dimensions of a given metric should be meaningful (though not necessarily useful). If it is not meaningful, split the data up into multiple metrics. For example, having the capacity of various queues in one metric is good, while mixing the capacity of a queue with the current number of elements in the queue is not.

  ...


【Engineering】Monitoring System

Context

While there is a certain intellectual satisfaction to be had by getting a system just right, the purpose of monitoring is to help you run whatever it is that you’re monitoring. That could be a website used directly by your customers, a backend only used internally, or even industrial systems. Fundamentally your customers don’t care how well your monitoring is working, they care how well the services they’re interacting with function. Part of you providing that service should involve some level of monitoring (in the broadest sense of the word) to ensure that things aren’t going badly wrong, but that’s just one part of providing the service.

  ...


【Golang】源码 - json - Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the JSON encoding of v.

Marshal traverses the value v recursively. If an encountered value implements the Marshaler interface and is not a nil pointer, Marshal calls its MarshalJSON method to produce JSON. If no MarshalJSON method is present but the value implements encoding.TextMarshaler instead, Marshal calls its MarshalText method and encodes the result as a JSON string. The nil pointer exception is not strictly necessary but mimics a similar, necessary exception in the behavior of UnmarshalJSON.

  ...


【Golang】关键字 - switch

switch

A switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression.

Go’s switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only runs the selected case, not all the cases that follow. In effect, the break statement that is needed at the end of each case in those languages is provided automatically in Go. Another important difference is that Go’s switch cases need not be constants, and the values involved need not be integers.

  ...