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

V1.1.7 #129

Merged
merged 5 commits into from
Apr 14, 2024
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
31 changes: 21 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@ on:
pull_request:
branches: [ "**" ]

env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}

jobs:

build:
runs-on: ubuntu-latest

strategy:
matrix:
go: [ '1.22.0', '1.22.1', '1.22.2' ]
go: [ '1.22.0', '1.22.2' ]
dynamodb-local: [ '2.3.0' ]
s3mock: [ '3.6.0' ]
elasticsearch-v7: [ '7.17.20' ]
elasticsearch-v8: [ '8.13.0' ]
mongo: [ '7.0.8' ]
redis: [ '7.2.4' ]
mysql: [ '5.7.44', '8.3.0' ]
postgres: [ '16.2-alpine' ]

name: Go ${{ matrix.go }}
name: golang:${{ matrix.go }}, amazon/dynamodb-local:${{ matrix.dynamodb-local }}, adobe/s3mock:${{ matrix.s3mock }}, elasticsearch:${{ matrix.elasticsearch-v7 }}, elasticsearch:${{ matrix.elasticsearch-v8 }}, mongo:${{ matrix.mongo }}, redis:${{ matrix.redis }}, mysql:${{ matrix.mysql }}, postgres:${{ matrix.postgres }}

steps:
- uses: actions/checkout@v4
Expand All @@ -33,15 +44,15 @@ jobs:

- name: Run docker image
run: |
docker run --name dynamodb --detach --publish 8000:8000 --env "-jar DynamoDBLocal.jar -sharedDb -inMemory" amazon/dynamodb-local:2.3.0
docker run --name s3mock --detach --publish 9090:9090 -p 9191:9191 adobe/s3mock:3.5.2
docker run --name elasticsearch-v7 --detach --publish 19200:9200 -p 19300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" elasticsearch:7.17.18
docker run --name dynamodb --detach --publish 8000:8000 --env "-jar DynamoDBLocal.jar -sharedDb -inMemory" amazon/dynamodb-local:${{ matrix.dynamodb-local }}
docker run --name s3mock --detach --publish 9090:9090 -p 9191:9191 adobe/s3mock:${{ matrix.s3mock }}
docker run --name elasticsearch-v7 --detach --publish 19200:9200 -p 19300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" elasticsearch:${{ matrix.elasticsearch-v7 }}
docker network create elastic
docker run --name elasticsearch-v8 --net elastic --detach --publish 29200:9200 -p 29300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" --env xpack.security.enabled=false elasticsearch:8.12.2
docker run --name mongodb --detach --publish 27017:27017 mongo:7.0.7
docker run --name redis --detach --publish 6379:6379 redis:7.2.4
docker run --name mysql --detach --publish 3306:3306 --env MYSQL_ROOT_PASSWORD=root mysql:8.3.0
docker run --name postgres --detach --publish 5432:5432 --env POSTGRES_PASSWORD=postgres postgres:16.2-alpine
docker run --name elasticsearch-v8 --net elastic --detach --publish 29200:9200 -p 29300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" --env xpack.security.enabled=false elasticsearch:${{ matrix.elasticsearch-v8 }}
docker run --name mongodb --detach --publish 27017:27017 mongo:${{ matrix.mongo }}
docker run --name redis --detach --publish 6379:6379 redis:${{ matrix.redis }}
docker run --name mysql --detach --publish 3306:3306 --env MYSQL_ROOT_PASSWORD=root mysql:${{ matrix.mysql }}
docker run --name postgres --detach --publish 5432:5432 --env POSTGRES_PASSWORD=postgres postgres:${{ matrix.postgres }}

- name: Sleep for 20 seconds
run: sleep 20s
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ go get -u github.com/common-library/go
- DynamoDB
- `docker run --name dynamodb --detach --publish 8000:8000 --env "-jar DynamoDBLocal.jar -sharedDb -inMemory" amazon/dynamodb-local:2.3.0`
- S3
- `docker run --name s3mock --detach --publish 9090:9090 -p 9191:9191 adobe/s3mock:3.5.2`
- `docker run --name s3mock --detach --publish 9090:9090 -p 9191:9191 adobe/s3mock:3.6.0`
- Elasticsearch v7
- `docker run --name elasticsearch-v7 --detach --publish 19200:9200 -p 19300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" elasticsearch:7.17.18`
- `docker run --name elasticsearch-v7 --detach --publish 19200:9200 -p 19300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" elasticsearch:7.17.20`
- Elasticsearch v8
- `docker network create elastic`
- `docker run --name elasticsearch-v8 --net elastic --detach --publish 29200:9200 -p 29300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" --env xpack.security.enabled=false elasticsearch:8.12.2`
- `docker run --name elasticsearch-v8 --net elastic --detach --publish 29200:9200 -p 29300:9300 --env discovery.type=single-node --env ES_JAVA_OPTS="-Xms500m -Xmx500m" --env xpack.security.enabled=false elasticsearch:8.13.0`
- MongoDB
- `docker run --name mongodb --detach --publish 27017:27017 mongo:7.0.7`
- `docker run --name mongodb --detach --publish 27017:27017 mongo:7.0.8`
- Redis
- `docker run --name redis --detach --publish 6379:6379 redis:7.2.4`
- SQL
Expand Down
4 changes: 3 additions & 1 deletion ai/gemini/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func TestChat(t *testing.T) {
return
if len(test.API_KEY) == 0 {
return
}

chat := gemini.Chat{}
if err := chat.Start(test.API_KEY); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions ai/gemini/gemini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func TestQuestion(t *testing.T) {
return
if len(test.API_KEY) == 0 {
return
}

if answer, err := gemini.Question(test.API_KEY, "who are you?", nil); err != nil {
t.Fatal(err)
Expand All @@ -24,7 +26,9 @@ func TestQuestion(t *testing.T) {
}

func TestQuestionStream(t *testing.T) {
return
if len(test.API_KEY) == 0 {
return
}

if channel, err := gemini.QuestionStream(test.API_KEY, "please say something encouraging", nil); err != nil {
t.Fatal(err)
Expand Down
4 changes: 3 additions & 1 deletion ai/gemini/test-data/api_key.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package test

const API_KEY = ""
import "os"

var API_KEY = os.Getenv("GEMINI_API_KEY")
59 changes: 59 additions & 0 deletions database/prometheus/exporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Package exporter provides prometheus exporter implementations.
package exporter

import (
net_http "net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// New is creates a Collector.
//
// ex) collector01 := exporter.New([]exporter.Metric{...})
func New(metrics []Metric) prometheus.Collector {
return &collector{metrics: metrics}
}

// Register registers the Collectors.
//
// ex) err := exporter.Register(collector01)
func Register(collectors ...prometheus.Collector) error {
for _, collector := range collectors {
if err := prometheus.Register(collector); err != nil {
return err
}
}

return nil
}

// UnRegister unregister the Collectors.
//
// ex) result := exporter.UnRegister(collector01)
func UnRegister(collectors ...prometheus.Collector) bool {
for _, collector := range collectors {
if prometheus.Unregister(collector) == false {
return false
}
}

return true
}

// Start is start the server.
//
// ex) err := exporter.Start(":10000", "metrics", func(err error) { klog.ErrorS(err, "") })
func Start(address, urlPath string, listenAndServeFailureFunc func(error)) error {
server.RegisterHandlerFunc(urlPath, net_http.MethodGet, promhttp.Handler().ServeHTTP)

return server.Start(address, listenAndServeFailureFunc)
}

// Stop is stop the server.
//
// ex) err := exporter.Stop(60)
func Stop(timeout time.Duration) error {
return server.Stop(timeout)
}
78 changes: 78 additions & 0 deletions database/prometheus/exporter/exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package exporter_test

import (
"math/rand/v2"
"strconv"
"testing"
"time"

"github.com/common-library/go/database/prometheus/exporter"
"github.com/prometheus/client_golang/prometheus/testutil"
)

func TestNew(t *testing.T) {
sample01Collector := exporter.New([]exporter.Metric{&metric01{}})

if _, err := testutil.CollectAndLint(sample01Collector); err != nil {
t.Fatal(err)
}

if count := testutil.CollectAndCount(sample01Collector); count != 3 {
t.Fatal("invalid -", count)
}

if err := testutil.CollectAndCompare(sample01Collector, (&metric01{}).getExpected()); err != nil {
t.Fatal(err)
}
}

func TestRegister(t *testing.T) {
sample01Collector := exporter.New([]exporter.Metric{&metric01{}})

if err := exporter.Register(sample01Collector); err != nil {
t.Fatal(err)
}

if exporter.UnRegister(sample01Collector) == false {
t.Fatal("UnRegister false")
}
}

func TestUnRegister(t *testing.T) {
TestRegister(t)
}

func TestStart(t *testing.T) {
address := ":" + strconv.Itoa(10000+rand.IntN(1000))
path := "/metrics"

sample01Collector := exporter.New([]exporter.Metric{&metric01{}})

if err := exporter.Register(sample01Collector); err != nil {
t.Fatal(err)
}
defer func() {
if exporter.UnRegister(sample01Collector) == false {
t.Fatal("UnRegister false")
}
}()

listenAndServeFailureFunc := func(err error) { t.Fatal(err) }
if err := exporter.Start(address, path, listenAndServeFailureFunc); err != nil {
t.Fatal(err)
}
defer func() {
if err := exporter.Stop(60); err != nil {
t.Fatal(err)
}
}()
time.Sleep(100 * time.Millisecond)

if err := testutil.ScrapeAndCompare("http://"+address+"/metrics", (&metric01{}).getExpected(), "sample01_metric01"); err != nil {
t.Fatal(err)
}
}

func TestStop(t *testing.T) {
TestStart(t)
}
40 changes: 40 additions & 0 deletions database/prometheus/exporter/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package exporter provides prometheus exporter implementations.
package exporter

import (
"github.com/common-library/go/http"
"github.com/prometheus/client_golang/prometheus"
)

var server http.Server

// Metric is an interface that provides the information to be collected.
type Metric interface {
GetDesc() *prometheus.Desc
GetValueType() prometheus.ValueType
GetValues() []Value
}

// value is a struct that provides the value to collect.
type Value struct {
Value float64
LabelValues []string
}

type collector struct {
metrics []Metric
}

func (this *collector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range this.metrics {
ch <- metric.GetDesc()
}
}

func (this *collector) Collect(ch chan<- prometheus.Metric) {
for _, metric := range this.metrics {
for _, value := range metric.GetValues() {
ch <- prometheus.MustNewConstMetric(metric.GetDesc(), metric.GetValueType(), value.Value, value.LabelValues...)
}
}
}
45 changes: 45 additions & 0 deletions database/prometheus/exporter/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package exporter_test

import (
"io"
"strings"

"github.com/common-library/go/database/prometheus/exporter"
"github.com/prometheus/client_golang/prometheus"
)

type metric01 struct {
}

func (this *metric01) GetDesc() *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName("sample01", "", "metric01"),
"metric01 of sample01",
[]string{"label_01", "label_02"},
prometheus.Labels{
"const_label_01": "const-value-01",
"const_label_02": "const-value-02"})
}

func (this *metric01) GetValueType() prometheus.ValueType {
return prometheus.GaugeValue
}

func (this *metric01) GetValues() []exporter.Value {
return []exporter.Value{
exporter.Value{Value: 1, LabelValues: []string{"value-01", "value-02"}},
exporter.Value{Value: 1, LabelValues: []string{"value-03", "value-04"}},
exporter.Value{Value: 2, LabelValues: []string{"value-05", "value-06"}},
}
}

func (this *metric01) getExpected() io.Reader {
expected := `# HELP sample01_metric01 metric01 of sample01
# TYPE sample01_metric01 gauge
sample01_metric01{const_label_01="const-value-01",const_label_02="const-value-02",label_01="value-01",label_02="value-02"} 1
sample01_metric01{const_label_01="const-value-01",const_label_02="const-value-02",label_01="value-03",label_02="value-04"} 1
sample01_metric01{const_label_01="const-value-01",const_label_02="const-value-02",label_01="value-05",label_02="value-06"} 2
`

return strings.NewReader(expected)
}
Loading