Skip to content

Commit

Permalink
fix health and metrics structure
Browse files Browse the repository at this point in the history
- Adiçao de pipeline para deploy.
- Implementaçao de build makefile
  • Loading branch information
Adriano M. La Selva committed Jan 31, 2022
1 parent 91717d3 commit 4a3a50b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 34 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: build-draethos
on:
push:
branches: [ master ]
tags: [ '*' ]
pull_request:
branches: [ master ]
tags: [ '*' ]
jobs:
build-application:
name: Run
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.13]
os: [ubuntu-latest, macos-latest]
steps:
- name: install-golang
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: checkout-code
uses: actions/checkout@v2
- name: build-application
run: go build -o ./build/${{ matrix.os }}/draethos -v ./init
- name: upload-artifact
uses: actions/upload-artifact@v2
with:
name: draethos
path: build/${{ matrix.os }}/draethos
retention-days: 1
upload-binaries-to-release:
name: upload-binaries-to-release
runs-on: ubuntu-20.04
needs: build-application
steps:
- name: download-binaries-to-release
uses: actions/download-artifact@v2
with:
name: draethos
path: build/
- name: compact-files
uses: montudor/[email protected]
if: ${{ startsWith(github.ref, 'refs/tags') }}
with:
args: "zip -r ./draethos.zip . -i ./build/*"
- name: upload-binaries-to-release
if: ${{ startsWith(github.ref, 'refs/tags') }}
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
file: draethos.zip
tag: ${{ github.ref }}
asset_name: draethos.zip
overwrite: true
file_glob: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
.DS_Store
vendor/
zk-multiple-kafka-single
bifrost
draethos
build/
to-do.txt
go.sum
*.dist
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ run:
deps:
$(GOGET) -d -v ./...
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME) -v
GOOS=linux GOARCH=amd64 $(GOBUILD) -o ./build/unix-x64/$(BINARY_NAME) -v ./init
49 changes: 21 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package cmd

import (
"fmt"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"go.uber.org/zap"
"net/http"
"os"
"syscall"

"draethos.io.com/pkg/color"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

const (
Expand All @@ -31,7 +30,7 @@ func Execute() {
var rootCmd = &cobra.Command{
Use: "draethos",
Version: release,
Long: fmt.Sprintf(TerminalPrint, release),
Long: fmt.Sprintf("%s%s%s", color.Green, fmt.Sprintf(TerminalPrint, release), color.Reset),
}

startCommand.
Expand All @@ -42,6 +41,22 @@ func Execute() {
"",
"file pipelines to be initialized")

startCommand.
PersistentFlags().
StringP(
"liveness",
"l",
"",
"initialize with health check")

startCommand.
PersistentFlags().
StringP(
"metrics",
"m",
"",
"initialize with metrics")

rootCmd.AddCommand(startCommand)

if err := rootCmd.Execute(); err != nil {
Expand All @@ -54,25 +69,3 @@ func initializeLogger() {
logger, _ := zap.NewDevelopment()
zap.ReplaceGlobals(logger)
}

func initializeHealthCheck(checkEndpoint string, liveEndpoint string, port string) {
var health = healthcheck.
NewHandler()

//health.AddLivenessCheck("goroutine-threshold", healthcheck.HTTPGetCheck("", 100))
//health.AddLivenessCheck("goroutine-threshold", healthcheck.GoroutineCountCheck(100))
health.AddReadinessCheck("health-name", func() error {
return nil
})

mux := http.NewServeMux()
mux.HandleFunc(checkEndpoint, health.LiveEndpoint)
mux.HandleFunc(liveEndpoint, health.ReadyEndpoint)

go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), mux)
}

func initializePrometheus(endpoint string, port string) {
http.Handle(endpoint, promhttp.Handler())
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), nil)
}
8 changes: 8 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ var startCommand = &cobra.Command{
configBuilder.SetFile(value)
}

if _, err := cmd.Flags().GetString("liveness"); err == nil {
configBuilder.EnableLiveness()
}

if _, err := cmd.Flags().GetString("metrics"); err == nil {
configBuilder.EnableMetrics()
}

config, err := configBuilder.Build()
if err != nil {
zap.S().Error(err.Error())
Expand Down
63 changes: 59 additions & 4 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package core

import (
"draethos.io.com/pkg/streams/specs"
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"strings"

"draethos.io.com/pkg/streams/specs"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)

type ConfigBuilder interface {
readFile() error
validateExtension() error
SetFile(filePath string) ConfigBuilder
EnableLiveness() ConfigBuilder
EnableMetrics() ConfigBuilder
Build() (*specs.Stream, error)
}

type configBuilder struct {
filePath string
file []byte
filePath string
enableLiveness bool
enableMetrics bool
file []byte
}

func NewConfigBuilder() ConfigBuilder {
Expand All @@ -39,9 +48,33 @@ func (c *configBuilder) Build() (*specs.Stream, error) {
return nil, err
}

if c.enableLiveness {
initializeHealthCheck(stream.Stream.HealthCheck.Endpoint, stream.Stream.Port)
zap.S().Debugf("initialize entpoint health-check: [localhost:%s%s]",
stream.Stream.Port,
stream.Stream.HealthCheck.Endpoint)
}

if c.enableMetrics {
initializePrometheus(stream.Stream.Metrics.Endpoint, stream.Stream.Port)
zap.S().Debugf("initialize endpoint prometheus metrics: [localhost:%s%s]",
stream.Stream.Port,
stream.Stream.Metrics.Endpoint)
}

return stream, nil
}

func (c *configBuilder) EnableLiveness() ConfigBuilder {
c.enableLiveness = true
return c
}

func (c *configBuilder) EnableMetrics() ConfigBuilder {
c.enableMetrics = true
return c
}

func (c *configBuilder) SetFile(filePath string) ConfigBuilder {
c.filePath = filePath
return c
Expand Down Expand Up @@ -77,3 +110,25 @@ func (c *configBuilder) deserializeYaml() (*specs.Stream, error) {

return &stream, nil
}

func initializeHealthCheck(checkEndpoint string, port string) {
var health = healthcheck.
NewHandler()

health.AddLivenessCheck("goroutine-threshold", healthcheck.HTTPGetCheck("", 100))
health.AddLivenessCheck("goroutine-threshold", healthcheck.GoroutineCountCheck(100))
health.AddReadinessCheck("health-name", func() error {
return nil
})

mux := http.NewServeMux()
mux.HandleFunc(checkEndpoint, health.LiveEndpoint)
mux.HandleFunc("health2", health.ReadyEndpoint)

go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), mux)
}

func initializePrometheus(endpoint string, port string) {
http.Handle(endpoint, promhttp.Handler())
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), nil)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
go.uber.org/zap v1.16.0
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/mod v0.4.1 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.0 // indirect
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/confluentinc/confluent-kafka-go.v1 v1.6.1
Expand Down
13 changes: 13 additions & 0 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package color

const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
White = "\033[97m"
)

0 comments on commit 4a3a50b

Please sign in to comment.