-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from common-library/v1.1.x
V1.1.7
- Loading branch information
Showing
16 changed files
with
576 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.