Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcsauter committed Dec 24, 2024
1 parent e639f30 commit 0af7580
Show file tree
Hide file tree
Showing 8 changed files with 489 additions and 226 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/push.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
name: build
on: [push, pull_request]
on:
push:
pull_request:

jobs:
lint:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker://golangci/golangci-lint:v1.31.0
with:
args: golangci-lint run
env:
CGO_ENABLED: 0
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.15
go-version: 1.23.4
- name: Run Unit tests
run: go test -covermode atomic -coverprofile=profile.cov $(go list -m)/...
- name: Send coverage
Expand All @@ -26,4 +31,3 @@ jobs:
run: |
GO111MODULE=off go get github.com/mattn/goveralls
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
90 changes: 90 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
run:
tests: false
linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- hugeParam
gocyclo:
min-complexity: 15
revive:
confidence: 0.8
lll:
line-length: 140
misspell:
locale: US
nolintlint:
require-explanation: true
allow-no-explanation:
- gocognit
- funlen
- gocyclo

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- funlen
- nolintlint
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- revive
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- prealloc
- protogetter
- rowserrcheck
- copyloopvar
- staticcheck
- stylecheck
- sqlclosecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl

issues:
exclude:
# Very commonly not checked.
- 'Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked'
- 'G104:.*'
- 'exported method (.*\.MarshalJSON|.*\.UnmarshalJSON|.*\.MarshalText|.*\.UnmarshalText|.*\.LogValue|.*\.MarshalLogObject) should have comment or be unexported'
- 'shadow: declaration of "err" shadows declaration.*'
max-same-issues: 0
exclude-use-default: false
exclude-dirs:
- .github
- build
- web
- .go
- vendor
81 changes: 41 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/postfinance/profiler)](https://goreportcard.com/report/github.com/postfinance/profiler)
[![GoDoc](https://godoc.org/github.com/postfinance/profiler?status.svg)](https://godoc.org/github.com/postfinance/profiler)
[![Build Status](https://github.com/postfinance/profiler/workflows/build/badge.svg)](https://github.com/postfinance/profiler/actions)
[![Coverage Status](https://coveralls.io/repos/github/postfinance/profiler/badge.svg?branch=master)](https://coveralls.io/github/postfinance/profiler?branch=master)


<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [profiler](#profiler)
- [Usage](#usage)
- [Start the pprof endpoint](#start-the-pprof-endpoint)
- [Collect pprof data](#collect-pprof-data)
- [Usage with kubernetes services](#usage-with-kubernetes-services)
- [Start the pprof endpoint](#start-the-pprof-endpoint-1)
- [Check log](#check-log)
- [Port-forward](#port-forward)
- [Collect pprof data](#collect-pprof-data-1)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
[![Build](https://github.com/postfinance/profiler/actions/workflows/build.yml/badge.svg)](https://github.com/postfinance/profiler/actions/workflows/build.yml)
[![Coverage](https://coveralls.io/repos/github/postfinance/profiler/badge.svg?branch=master)](https://coveralls.io/github/postfinance/profiler?branch=master)

# profiler

## Usage

Add the following line to your Go code:

```go
// create and start the profiler handler
profiler.New().Start()
Expand All @@ -37,56 +21,73 @@ profiler.New(
)
```

Defaults:
- Signal *HUP*
- Listen *:6666*
- Timeout *10m*
## Defaults

| Parameter | Default |
|-----------|---------|
| Signal | *HUP* |
| Listen | *:6666* |
| Timeout | *10m* |

### Start the pprof endpoint
```bash

```shell
pkill -HUP <your Go program>
```
After *timeout* the endpoint will shutdown.

> After *timeout* the endpoint will shutdown.
### Collect pprof data
```bash

```shell
go tool pprof -http $(hostname):8080 http://localhost:6666/debug/pprof/profile
```

## Usage with kubernetes services
... or ...

```shell
go tool pprof -http localhost:7007 http://localhost:8080/debug/pprof/profile
```

## Kubernetes

### Start the pprof endpoint
```bash
$ k get pods

```shell
kubectl get pods
NAME READY STATUS RESTARTS AGE
...

$ k exec -ti <your pod> sh
kubectl exec -ti <your pod> sh
/ # pkill -HUP <your Go program>
/ #
```
After *timeout* the endpoint will shutdown.

> After *timeout* the endpoint will shutdown.
### Check log
```bash
$ k logs <your pod> -f
...
2020/02/10 16:37:09 start pprof endpoint on ":6666"
...

```shell
kubectl logs <your pod> -f | grep 'start debug endpoint'
```

### Port-forward
```bash
$ k port-forward <your pod> 8080:6666

```shell
kubectl port-forward <your pod> 8080:6666
Forwarding from 127.0.0.1:8080 -> 6666
Forwarding from [::1]:8080 -> 6666
Handling connection for 8080
```

### Collect pprof data
```bash
$ go tool pprof -http $(hostname):8888 http://localhost:8080/debug/pprof/profile

```shell
go tool pprof -http $(hostname):8888 http://localhost:8080/debug/pprof/profile
```

... or ...

```shell
go tool pprof -http localhost:7007 http://localhost:8080/debug/pprof/profile
```
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/postfinance/profiler

go 1.15
go 1.23

require github.com/stretchr/testify v1.6.1

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
44 changes: 44 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package profiler

import (
"os"
"time"
)

// Option is a Profiler functional option
type Option func(*Profiler)

// WithSignal sets the signal to activate the pprof handler
func WithSignal(s os.Signal) Option {
return func(p *Profiler) {
p.signal = s
}
}

// WithAddress sets the listen address of the pprof handler
func WithAddress(address string) Option {
return func(p *Profiler) {
p.address = address
}
}

// WithTimeout sets the timeout after the pprof handler will be shutdown
func WithTimeout(timeout time.Duration) Option {
return func(p *Profiler) {
p.timeout = timeout
}
}

// WithEventHandler registers a custom event handler
func WithEventHandler(evt EventHandler) Option {
return func(p *Profiler) {
p.evt = evt
}
}

// WithHooks registers the Profiler hooks
func WithHooks(hooks ...Hooker) Option {
return func(p *Profiler) {
p.hooks = append(p.hooks, hooks...)
}
}
Loading

0 comments on commit 0af7580

Please sign in to comment.