Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #1

Merged
merged 12 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
labels:
- "go"
- "dependencies"
schedule:
interval: "weekly"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "weekly"
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: build
on:
push:
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- 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@v4
- uses: actions/setup-go@v5
with:
go-version: 'stable'
- name: Run Unit tests
run: go test -race -covermode atomic -coverprofile=profile.cov ./...
- name: Send coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
go install github.com/mattn/goveralls@latest
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
29 changes: 0 additions & 29 deletions .github/workflows/push.yml

This file was deleted.

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
85 changes: 43 additions & 42 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 | *SIGUSR1* |
| Listen | *:6666* |
| Timeout | *30m* |

### Start the pprof endpoint
```bash
pkill -HUP <your Go program>

```shell
pkill -SIGUSR1 <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
/ # pkill -HUP <your Go program>
kubectl exec -ti <your pod> sh
/ # pkill -SIGUSR1 <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
```
33 changes: 33 additions & 0 deletions eventhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package profiler

import (
"log/slog"
"os"
)

// EventType represents the event type
type EventType int

// Event types
const (
InfoEvent = iota
ErrorEvent
)

// EventHandler function to handle log events
type EventHandler func(t EventType, v string, args ...any)

func DefaultEventHandler() EventHandler {
l := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))

return func(eventType EventType, msg string, args ...any) {
switch eventType {
case InfoEvent:
l.Info(msg, args...)
case ErrorEvent:
l.Error(msg, args...)
}
}
}
10 changes: 8 additions & 2 deletions 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/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading