Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
大可 committed Sep 26, 2024
0 parents commit 48bc920
Show file tree
Hide file tree
Showing 25 changed files with 1,755 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: 2

updates:
- package-ecosystem: gomod
directories:
- /
labels:
- dependencies
schedule:
interval: "weekly"
groups:
gomod-normal-deps:
update-types:
- patch
- minor
gomod-breaking-deps:
update-types:
- major

- package-ecosystem: "github-actions"
directory: "/"
labels:
- dependencies
schedule:
interval: "weekly"
groups:
actions-deps:
patterns:
- "*"
15 changes: 15 additions & 0 deletions .github/semantic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
titleOnly: true
allowRevertCommits: true
types:
- feat
- fix
- docs
- style
- refactor
- perf
- test
- build
- ci
- chore
- revert
- change
35 changes: 35 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build and Test

on:
push:
branches:
- 'main'
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: [stable]

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.version }}
cache: true

- name: Install Task
uses: arduino/setup-task@v2
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: generate and test
run: |
task
31 changes: 31 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: golangci-lint
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
strategy:
matrix:
go: [stable, oldstable]
os: [ubuntu-latest, macos-latest, windows-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=5m
version: v1.60
17 changes: 17 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Lint PR"

on:
pull_request:
types:
- opened
- edited
- synchronize

jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9 changes: 9 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'
env:
GOBIN: { sh: pwd }

tasks:
default:
cmds:
- go mod tidy
- go test ./...
37 changes: 37 additions & 0 deletions client/ketcdv3/etcdv3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ketcdv3

import (
"context"
"fmt"
"time"

"dario.cat/mergo"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
)

type Config struct {
Endpoints []string
Timeout time.Duration
}

func (r Config) Build(_ context.Context) (*clientv3.Client, error) {
err := mergo.Merge(&r, Config{
Endpoints: []string{"localhost:2379"},
Timeout: 3 * time.Second,
})
if err != nil {
return nil, fmt.Errorf("failed to merge config: %w", err)
}

etcd, err := clientv3.New(clientv3.Config{
Endpoints: r.Endpoints,
DialTimeout: r.Timeout,
DialOptions: []grpc.DialOption{},
})
if err != nil {
return nil, fmt.Errorf("failed to create etcd client: %w", err)
}

return etcd, nil
}
29 changes: 29 additions & 0 deletions client/kgorm/gorm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kgorm

import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/plugin/opentelemetry/tracing"
)

type Config struct {
Dsn string
}

func (c Config) Build() *DB {
db, err := gorm.Open(mysql.Open(c.Dsn), &gorm.Config{})
if err != nil {
panic(err)
}

if err := db.Use(tracing.NewPlugin()); err != nil {
panic(err)
}

return db
}

type (
// DB is an alias for gorm.DB.
DB = gorm.DB
)
59 changes: 59 additions & 0 deletions client/kgrpc/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package kgrpc

import (
"context"
"time"

"dario.cat/mergo"
"github.com/samber/lo"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/go-kod/kod-ext/registry"
)

type ClientConn = grpc.ClientConn

type Config struct {
Target string
Timeout time.Duration

registry registry.Registry
}

func (c Config) WithRegistry(r registry.Registry) Config {
c.registry = r
return c
}

func (c Config) Build(opts ...grpc.DialOption) *ClientConn {
lo.Must0(mergo.Merge(&c, Config{
Timeout: 3 * time.Second,
}))

ctx := context.Background()

defaultOpts := []grpc.DialOption{
grpc.WithNoProxy(),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}

if c.registry != nil {
builder, err := c.registry.ResolveBuilder(ctx)
if err != nil {
panic(err)
}
defaultOpts = append(defaultOpts, grpc.WithResolvers(builder))
}

defaultOpts = append(defaultOpts, opts...)

cc, err := grpc.NewClient(c.Target, defaultOpts...)
if err != nil {
panic(err)
}

return cc
}
38 changes: 38 additions & 0 deletions client/khttp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package http

import (
"context"
"net/http"
"net/http/httptrace"
"time"

"dario.cat/mergo"
"github.com/samber/lo"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type Client = http.Client

type ClientConfig struct {
Address string
Timeout time.Duration
}

func (c ClientConfig) Build() *Client {

lo.Must0(mergo.Merge(&c, ClientConfig{
Timeout: 3 * time.Second,
}))

defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport.Proxy = nil

return &http.Client{
Transport: otelhttp.NewTransport(defaultTransport,
otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace {
return otelhttptrace.NewClientTrace(ctx)
})),
Timeout: c.Timeout,
}
}
45 changes: 45 additions & 0 deletions client/kpyroscope/pyroscope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package kpyroscope

import (
"context"
"fmt"
"os"

"github.com/go-kod/kod"
pyroscope "github.com/grafana/pyroscope-go"
)

type Config struct {
ServerAddress string
}

func (c Config) Build(ctx context.Context) (*pyroscope.Profiler, error) {
k := kod.FromContext(ctx)

p, err := pyroscope.Start(pyroscope.Config{
ApplicationName: k.Config().Name,
// Logger: pyroscope.StandardLogger,
ServerAddress: c.ServerAddress,
Tags: map[string]string{"hostname": os.Getenv("HOSTNAME")},
ProfileTypes: []pyroscope.ProfileType{
// these profile types are enabled by default:
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,

// these profile types are optional:
pyroscope.ProfileGoroutines,
pyroscope.ProfileMutexCount,
pyroscope.ProfileMutexDuration,
pyroscope.ProfileBlockCount,
pyroscope.ProfileBlockDuration,
},
})
if err != nil {
return nil, fmt.Errorf("failed to start pyroscope: %w", err)
}

return p, nil
}
Loading

0 comments on commit 48bc920

Please sign in to comment.