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

feat: add metric feature support in goframe #3138

Merged
merged 67 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
71dfe07
add package gmetric
gqcn Nov 6, 2023
43e5302
up
gqcn Nov 7, 2023
dee36b7
up
gqcn Nov 8, 2023
e1bc1f5
up
gqcn Nov 8, 2023
2fd8b59
up
gqcn Nov 8, 2023
9e3f1e5
up
gqcn Nov 8, 2023
4b1a0e7
up
gqcn Nov 8, 2023
b9f5eb9
up
gqcn Nov 8, 2023
b5c324c
up
gqcn Nov 8, 2023
63a3247
go mod tidy
gqcn Nov 8, 2023
58d2821
go mod tidy; ci updates
gqcn Nov 8, 2023
c458fe4
go mod tidy
gqcn Nov 8, 2023
b840df3
up
gqcn Nov 8, 2023
75c2013
README updates for package otelmetric
gqcn Nov 8, 2023
feb6ae9
go mod tidy
gqcn Nov 8, 2023
7c2a976
Add Callback feature for metric
gqcn Nov 9, 2023
a24881d
up
gqcn Nov 10, 2023
adc6d70
add global callback feature for metric
gqcn Nov 13, 2023
f37d772
comment update
gqcn Nov 13, 2023
73e44fd
ut updates for package mssql
gqcn Nov 13, 2023
8ec9e93
improve global callback feature for metric
gqcn Nov 13, 2023
0a5ded6
up
gqcn Nov 13, 2023
54da127
up
gqcn Nov 14, 2023
76b86cf
up
gqcn Nov 14, 2023
9b08803
up
gqcn Nov 15, 2023
c5c8f82
up
gqcn Nov 16, 2023
b3aaac9
Merge branch 'master' into feature/metric
houseme Jan 15, 2024
8c46fa4
feat: improve upgrade package version
houseme Jan 15, 2024
ed30b69
feat: upgrade otel version v1.22.0
houseme Jan 18, 2024
168d175
feat: upgrade example module otel version
houseme Jan 18, 2024
185b8b1
Merge branch 'master' into feature/metric
houseme Jan 22, 2024
b034d2d
up
gqcn Mar 7, 2024
14358a8
up
gqcn Mar 7, 2024
438d4c4
up
gqcn Mar 7, 2024
414e60b
merge master
gqcn Mar 7, 2024
92f1381
up
gqcn Mar 7, 2024
9df9c75
add Stats for gdb.DB
gqcn Mar 11, 2024
d1cb812
improve callback feature for metrics
gqcn Mar 11, 2024
efd834e
up
gqcn Mar 11, 2024
a83f876
up
gqcn Mar 13, 2024
bf85ada
up
gqcn Mar 13, 2024
a01b91d
up
gqcn Mar 14, 2024
3656a9f
up
gqcn Mar 14, 2024
40af0e4
up
gqcn Mar 15, 2024
e4046ee
Merge branch 'master' of github.com:gogf/gf into feature/metric
gqcn Mar 15, 2024
dd61c20
up
gqcn Mar 15, 2024
d851064
up
gqcn Mar 15, 2024
9a0b835
up
gqcn Mar 19, 2024
4f65ce9
improve code
houseme Mar 19, 2024
41d7adc
ip
gqcn Mar 19, 2024
f627f0f
pull
gqcn Mar 19, 2024
c7af27e
fix
houseme Mar 19, 2024
3938c53
up
gqcn Mar 19, 2024
b5d4d4c
Merge branch 'feature/metric' of github.com:gogf/gf into feature/metric
gqcn Mar 19, 2024
c394153
up
gqcn Mar 20, 2024
8afd6e1
Merge branch 'master' of github.com:gogf/gf into feature/metric
gqcn Mar 20, 2024
3f084b9
example updates
gqcn Mar 20, 2024
e12956d
up
gqcn Mar 20, 2024
60f887b
Merge branch 'master' of github.com:gogf/gf into feature/metric
gqcn Mar 21, 2024
14c798f
up
gqcn Mar 21, 2024
4bcf6e9
feat: metrics
gqcn Mar 24, 2024
141bea8
up
gqcn Mar 24, 2024
471dd60
up
gqcn Mar 24, 2024
dd6bc9c
up
gqcn Mar 24, 2024
e171a6f
up
gqcn Mar 24, 2024
85d4ba8
up
gqcn Mar 24, 2024
a962016
up
gqcn Mar 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci-main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ for file in `find . -name go.mod`; do
fi
fi

# package otelmetric needs golang >= v1.20
if [ "otelmetric" = $(basename $dirpath) ]; then
if ! go version|grep -qE "go1.[2-9][0-9]"; then
echo "ignore otelmetric as go version: $(go version)"
continue 1
fi
fi

cd $dirpath
go mod tidy
go build ./...
Expand Down
14 changes: 14 additions & 0 deletions cmd/gf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.4 h1:ggkj93Ln7ULOyoDGIGaphkg/qV8zDDSbkFVn6EljjNQ=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.4/go.mod h1:J7CWaKo4mIYv1BqfHFpTPAjs5Sm7eZrCjA2UMAfM98E=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.4 h1:S22r+bWoSSESNJh/IT9tpbgDhmWltxztiQ5ONn75BRo=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.4/go.mod h1:NFhR+wl4BF1W/ddH9hKXARL+g3nP+r1qeAgxqOl75bA=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.4 h1:ScG3YcYMDEP/UrwNtwQPt2noySa5ZpoV7BxrwaeBaro=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.4/go.mod h1:oFFE9u1zHkxVXk7ZkNipXQR9JFyDZDiixZy243ywhfQ=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.4 h1:9bOuNKHZ/ZBNuuZT2Qc0V+ypkKI2k8ZkSwcnqH+ikEY=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.4/go.mod h1:tK8W1hDZdwjfWh3GlNdYoBWLs6unfey2+XW8FFnWICM=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.4 h1:a+zIohGG1mv+1/9lBb/C0coM+MbmPYOWKo2SL1tJhTU=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.4/go.mod h1:I7TGm/kilpNGwT8h8Frc5vGWRLgn5Vn9ce2gIBHcfbg=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4 h1:eJYZCJIqybMV6oJY7J7clWTNqx0yzZI7CaUn31igBGc=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4/go.mod h1:3q/YKn6NL7KkUdT6nw93eM5d1QjmX/+b+CPiIm/aLiA=
github.com/gogf/gf/v2 v2.6.4 h1:w7HXdH9mcTsn/aE13CkaDbRArmAL1KS3FuQqDi6u74Y=
github.com/gogf/gf/v2 v2.6.4/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down
7 changes: 3 additions & 4 deletions cmd/gf/internal/cmd/testdata/build/varmap/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyM
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
Expand Down
8 changes: 4 additions & 4 deletions container/gtype/gtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// Package gtype provides high performance and concurrent-safe basic variable types.
package gtype

// New is alias of NewInterface.
// See NewInterface.
func New(value ...interface{}) *Interface {
return NewInterface(value...)
// New is alias of NewAny.
// See NewAny, NewInterface.
func New(value ...interface{}) *Any {
return NewAny(value...)
}
20 changes: 20 additions & 0 deletions container/gtype/gtype_any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gtype

// Any is a struct for concurrent-safe operation for type any.
type Any = Interface

// NewAny creates and returns a concurrent-safe object for any type,
// with given initial value `value`.
func NewAny(value ...any) *Any {
t := &Any{}
if len(value) > 0 && value[0] != nil {
t.value.Store(value[0])
}
return t
}
74 changes: 74 additions & 0 deletions container/gtype/gtype_z_unit_any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gtype_test

import (
"testing"

"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)

func Test_Any(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t1 := Temp{Name: "gf", Age: 18}
t2 := Temp{Name: "gf", Age: 19}
i := gtype.New(t1)
iClone := i.Clone()
t.AssertEQ(iClone.Set(t2), t1)
t.AssertEQ(iClone.Val().(Temp), t2)

// empty param test
i1 := gtype.New()
t.AssertEQ(i1.Val(), nil)

i2 := gtype.New("gf")
t.AssertEQ(i2.String(), "gf")
copyVal := i2.DeepCopy()
i2.Set("goframe")
t.AssertNE(copyVal, iClone.Val())
i2 = nil
copyVal = i2.DeepCopy()
t.AssertNil(copyVal)
})
}

func Test_Any_JSON(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := "i love gf"
i := gtype.New(s)
b1, err1 := json.Marshal(i)
b2, err2 := json.Marshal(i.Val())
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(b1, b2)

i2 := gtype.New()
err := json.UnmarshalUseNumber(b2, &i2)
t.AssertNil(err)
t.Assert(i2.Val(), s)
})
}

func Test_Any_UnmarshalValue(t *testing.T) {
type V struct {
Name string
Var *gtype.Any
}
gtest.C(t, func(t *gtest.T) {
var v *V
err := gconv.Struct(map[string]any{
"name": "john",
"var": "123",
}, &v)
t.AssertNil(err)
t.Assert(v.Name, "john")
t.Assert(v.Var.Val(), "123")
})
}
3 changes: 2 additions & 1 deletion contrib/drivers/mssql/mssql_z_unit_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ func Test_DB_Insert(t *testing.T) {
t.Assert(one["PASSPORT"].String(), "user_3")
t.Assert(one["PASSWORD"].String(), "25d55ad283aa400af464c76d713c07ad")
t.Assert(one["NICKNAME"].String(), "name_3")
t.Assert(one["CREATE_TIME"].GTime(), timeNow)
t.AssertNE(one["CREATE_TIME"].GTime(), nil)
t.AssertLT(timeNow.Sub(one["CREATE_TIME"].GTime()), 3)

// *struct
timeNow = gtime.Now()
Expand Down
160 changes: 160 additions & 0 deletions contrib/metric/otelmetric/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# GoFrame Metric In OpenTelemetry

## Installation

```
go get -u -v github.com/gogf/gf/contrib/metric/otelmetric/v2
```

suggested using `go.mod`:

```
require github.com/gogf/gf/contrib/metric/otelmetric/v2 latest
```

## Example

### [basic](../../../example/metric/basic/main.go)

```go
package main

import (
"context"

"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"

"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gmetric"
)

var (
meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
Instrument: "github.com/gogf/gf/example/metric/basic",
InstrumentVersion: "v1.0",
})
counter = meter.MustCounter(
"goframe.metric.demo.counter",
gmetric.MetricOption{
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
},
},
)
upDownCounter = meter.MustUpDownCounter(
"goframe.metric.demo.updown_counter",
gmetric.MetricOption{
Help: "This is a simple demo for UpDownCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_2", 2),
},
},
)
histogram = meter.MustHistogram(
"goframe.metric.demo.histogram",
gmetric.MetricOption{
Help: "This is a simple demo for histogram usage",
Unit: "ms",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_3", 3),
},
Buckets: []float64{0, 10, 20, 50, 100, 500, 1000, 2000, 5000, 10000},
},
)
observableCounter = meter.MustObservableCounter(
"goframe.metric.demo.observable_counter",
gmetric.MetricOption{
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_4", 4),
},
},
)
observableUpDownCounter = meter.MustObservableUpDownCounter(
"goframe.metric.demo.observable_updown_counter",
gmetric.MetricOption{
Help: "This is a simple demo for ObservableUpDownCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_5", 5),
},
},
)
observableGauge = meter.MustObservableGauge(
"goframe.metric.demo.observable_gauge",
gmetric.MetricOption{
Help: "This is a simple demo for ObservableGauge usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_6", 6),
},
},
)
)

func main() {
var ctx = gctx.New()

// Callback for observable metrics.
meter.MustRegisterCallback(func(ctx context.Context, obs gmetric.Observer) error {
obs.Observe(observableCounter, 10)
obs.Observe(observableUpDownCounter, 20)
obs.Observe(observableGauge, 30)
return nil
}, observableCounter, observableUpDownCounter, observableGauge)

// Prometheus exporter to export metrics as Prometheus format.
exporter, err := prometheus.New(
prometheus.WithoutCounterSuffixes(),
prometheus.WithoutUnits(),
)
if err != nil {
g.Log().Fatal(ctx, err)
}

// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider.SetAsGlobal()
defer provider.Shutdown(ctx)

// Counter.
counter.Inc(ctx)
counter.Add(ctx, 10)

// UpDownCounter.
upDownCounter.Inc(ctx)
upDownCounter.Add(ctx, 10)
upDownCounter.Dec(ctx)

// Record values for histogram.
histogram.Record(1)
histogram.Record(20)
histogram.Record(30)
histogram.Record(101)
histogram.Record(2000)
histogram.Record(9000)
histogram.Record(20000)

// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
}
```

### [more examples](../../../example/metric/)

## License

`GoFrame Polaris` is licensed under the [MIT License](../../../LICENSE), 100% free and open-source, forever.

44 changes: 44 additions & 0 deletions contrib/metric/otelmetric/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module github.com/gogf/gf/contrib/metric/otelmetric/v2

go 1.20

require (
github.com/gogf/gf/v2 v2.6.1
github.com/prometheus/client_golang v1.19.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.49.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/exporters/prometheus v0.46.0
go.opentelemetry.io/otel/metric v1.24.0
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/gogf/gf/v2 => ../../../