Measuring Elapsed Time in Go

Measuring Elapsed Time in Go

To measure elapsed time in Go, you can use following code. package main import ( "fmt" "time" "go.opentelemetry.io/otel/metric" ) func expensiveCall() { } func main() { // setup meter opDurationHistogration, _ := meter.Int64Histogram("operation_duration", metric.WithDescription("Operation duration"), metric.WithUnit("ms")) start := time.Now() expensiveCall() elapsed := time.Since() fmt.Printf("Took %s\n", elapsed) opDurationHistogration.Record(context.TODO(), elapsed) } Output Took 11.583µs

October 29, 2024 · 1 min
Secrets Detection and Prevention in Repository

Secrets Detection and Prevention in Repository

I decided to make the blog repository public to allow writing comments. To do it, I scanned my repo using two programs. detect-secrets detect-secrets detects secrets within a code base however, unlike other similar packages that solely focus on finding secrets, this package is designed with the enterprise client in mind: providing a backwards compatible, systematic means of: Preventing new secrets from entering the code base, Detecting if such preventions are explicitly bypassed, and Providing a checklist of secrets to roll, and migrate off to a more secure storage....

October 27, 2024 · 2 min

Fast k8s Context and Namespace Switching With Kubectx Kubens

kubectx and kubens are both great tools which speeds up working with multiple Kubernetes clusters. kubectx allows quickly changing context. kubens allows changing namespace in given context. kubectx With kubectl you need to type kubectl config use-context my-context With kubectx kubectx my-context If you have fzf installed, you can interactively select cluster by just writing kubectx. kubens Kubens is similar but it allows setting namespace. Instead of doing kubectl config set-context --current --namespace=my-namespace You can do...

October 25, 2024 · 1 min

Go build version information with debug.ReadBuildInfo and ldflags

You may find useful to have Git commit hash and go version in your structured logs with every entry. To achieve it you can use following code. Working code example you can find on GitHub package main import ( "log/slog" "os" "runtime" "runtime/debug" ) var GitCommit = "NOCOMMIT" var GoVersion = runtime.Version() var BuildDate = "" func initVersion() { info, ok := debug.ReadBuildInfo() if !ok { return } modified := false for _, setting := range info....

September 26, 2024 · 4 min

Connecting to HTTP server and receiving data with net.Dial and tls.Dial

This is a continuation from previous article How Does Web Work With Go Part 1. To talk with web server, you need to speak the same language. In this case it is protocol HTTP. Currently there are 3 version of this protocol in use: HTTP 1.1 - TCP text format HTTP 2 - TCP binary format HTTP 3 - UDP binary format In our case we will use HTTP 1.1 which is defined in RFC2616....

September 18, 2024 · 3 min

How Does Web Work With Go Part 1

This evening I was wondering if my 5-year old son would ask me, what is the Internet and how it works, what I would reply to him? Maybe he is a bit young to go into the details, but I would like to explain what I know about it and to structure my knowledge and learn more in the process. Because as Seneka said: while we teach, we learn. I would like to dive deep into the details in this series with some Go examples....

September 13, 2024 · 2 min

ACM Membership Benefits

I would like to recommend an ACM membership with Skills Bundle. This gives an access to O’Reilly, Skillsoft Percipio and Pluralsight. The 2024 cost 2024 is $174 per year or $100 if you are from developing countries. This cost is quite high but still way better than O’Reilly access alone which is $499 yearly. Pluralsight (also $499 yearly standard price) gives an access to huge library of video courses which are useful for certificate preparation....

April 29, 2024 · 1 min

HTTP logger with slog

I wanted to create a logging middleware for HTTP client. Working code example you can find on GitHub The basic structure is type LoggingTransport struct { rt http.RoundTripper } func (t *LoggingTransport) RoundTrip(r *http.Request) (*http.Response, error) { // do before request is sent, ex. start timer, log request resp, err := t.rt.RoundTrip(r) // do after the response is received, ex. end timer, log response return resp, err } I found this code in Kubernetes source and I liked this implementation, I decided to adjust it to my needs....

April 24, 2024 · 5 min

slog access log

Recently I wanted to learn more about Go’s slog package. Also for some other project I needed to create an access logger. I could not find a solution so I decided to create my own. I was inspired by two solutions: rs/zerolog/hlog package great guide at betterstack Working code example can be found on GitHub Requirements In my logger I wanted to have all standard data: request duration, status code, bytes written, method, url, user agent, remote addr plus some extra...

April 8, 2024 · 4 min

Trusted local certificates with mkcert and Go TLS server

When working on local environment, it is useful to use trusted TLS certificates. For this purpose we can use mkcert. mkcert automatically creates and installs a local CA in the system root store, and generates locally-trusted certificates. Working code example can be found on GitHub Instalation First we need to install it ❯ mkcert -install The local CA is already installed in the system trust store! 👍 The local CA is already installed in the Firefox trust store!...

April 5, 2024 · 2 min