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: support manual reporting of metrics in toolkit #205

Merged
merged 7 commits into from
Sep 23, 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
1 change: 1 addition & 0 deletions .github/workflows/plugin-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
- irisv12
- trace-activation
- logging-activation
- metric-activation
- fasthttp
- discard-reporter
- fiber
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Release Notes.

* support attaching events to span in the toolkit.
* support record log in the toolkit.
* support manually report metrics in the toolkit.

#### Plugins
* Support [goframev2](https://github.com/gogf/gf) goframev2.
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use (
./test/plugins/scenarios/segmentio-kafka
./test/plugins/scenarios/go-elasticsearchv8
./test/plugins/scenarios/logging-activation
./test/plugins/scenarios/metric-activation
./test/plugins/scenarios/goframe

./tools/go-agent
Expand Down
57 changes: 57 additions & 0 deletions plugins/toolkit-activation/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,66 @@ func (i *Instrument) Points() []*instrument.Point {
// append toolkit/logging related enhancements Point
instPoints = append(instPoints, loggingPoint()...)

// append toolkit/metric related enhancements Point
instPoints = append(instPoints, metricPoint()...)

return instPoints
}

func metricPoint() []*instrument.Point {
return []*instrument.Point{
// Counter metric type related enhancement point
{
PackagePath: "metric", At: instrument.NewStructEnhance("CounterRef"),
},
{
PackagePath: "metric", At: instrument.NewStaticMethodEnhance("NewCounter"),
Interceptor: "NewCounterInterceptor",
},
{
PackagePath: "metric", At: instrument.NewMethodEnhance("*CounterRef", "Get"),
Interceptor: "CounterGetInterceptor",
},
{
PackagePath: "metric", At: instrument.NewMethodEnhance("*CounterRef", "Inc"),
Interceptor: "CounterIncInterceptor",
},
// Gauge metric type related enhancement point
{
PackagePath: "metric", At: instrument.NewStructEnhance("GaugeRef"),
},
{
PackagePath: "metric", At: instrument.NewStaticMethodEnhance("NewGauge"),
Interceptor: "NewGaugeInterceptor",
},
{
PackagePath: "metric", At: instrument.NewMethodEnhance("*GaugeRef", "Get"),
Interceptor: "GaugeGetInterceptor",
},
// Histogram metric type related enhancement point
{
PackagePath: "metric", At: instrument.NewStructEnhance("HistogramRef"),
},
{
PackagePath: "metric", At: instrument.NewStaticMethodEnhance("NewHistogram"),
Interceptor: "NewHistogramInterceptor",
},
{
PackagePath: "metric", At: instrument.NewMethodEnhance("*HistogramRef", "Observe"),
Interceptor: "HistogramObserveInterceptor",
},
{
PackagePath: "metric", At: instrument.NewMethodEnhance("*HistogramRef", "ObserveWithCount"),
Interceptor: "HistogramObserveWithCountInterceptor",
},
// metric options related enhancement point
{
PackagePath: "metric", At: instrument.NewStaticMethodEnhance("WithLabels"),
Interceptor: "WithLabelsInterceptor",
},
}
}

func tracePoint() []*instrument.Point {
return []*instrument.Point{
{
Expand Down
43 changes: 43 additions & 0 deletions plugins/toolkit-activation/metric/counter_get_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type CounterGetInterceptor struct{}

func (h *CounterGetInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *CounterGetInterceptor) AfterInvoke(invocation operator.Invocation, _ ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}

counter, ok := enhanced.GetSkyWalkingDynamicField().(metrics.Counter)
if ok && counter != nil {
val := counter.Get()
invocation.DefineReturnValues(val)
}
return nil
}
42 changes: 42 additions & 0 deletions plugins/toolkit-activation/metric/counter_inc_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type CounterIncInterceptor struct{}

func (h *CounterIncInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *CounterIncInterceptor) AfterInvoke(invocation operator.Invocation, _ ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}

counter, ok := enhanced.GetSkyWalkingDynamicField().(metrics.Counter)
if ok && counter != nil {
counter.Inc(invocation.Args()[0].(float64))
}
return nil
}
43 changes: 43 additions & 0 deletions plugins/toolkit-activation/metric/gauge_get_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type GaugeGetInterceptor struct{}

func (h *GaugeGetInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *GaugeGetInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}

gauge, ok := enhanced.GetSkyWalkingDynamicField().(metrics.Gauge)
if ok && gauge != nil {
val := gauge.Get()
invocation.DefineReturnValues(val)
}
return nil
}
42 changes: 42 additions & 0 deletions plugins/toolkit-activation/metric/histogram_observe_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type HistogramObserveInterceptor struct{}

func (h *HistogramObserveInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *HistogramObserveInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}

histogram, ok := enhanced.GetSkyWalkingDynamicField().(metrics.Histogram)
if ok && histogram != nil {
histogram.Observe(invocation.Args()[0].(float64))
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type HistogramObserveWithCountInterceptor struct{}

func (h *HistogramObserveWithCountInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *HistogramObserveWithCountInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
enhanced, ok := invocation.CallerInstance().(operator.EnhancedInstance)
if !ok {
return nil
}

histogram, ok := enhanced.GetSkyWalkingDynamicField().(metrics.Histogram)
if ok && histogram != nil {
histogram.ObserveWithCount(invocation.Args()[0].(float64), invocation.Args()[1].(int64))
}
return nil
}
37 changes: 37 additions & 0 deletions plugins/toolkit-activation/metric/meter_opt_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
)

type WithLabelsInterceptor struct{}

func (h *WithLabelsInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *WithLabelsInterceptor) AfterInvoke(invocation operator.Invocation, _ ...interface{}) error {
key := invocation.Args()[0].(string)
val := invocation.Args()[1].(string)
withLabelOpt := metrics.WithLabel(key, val)
invocation.DefineReturnValues(withLabelOpt)
return nil
}
44 changes: 44 additions & 0 deletions plugins/toolkit-activation/metric/new_counter_intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package metric

import (
"github.com/apache/skywalking-go/plugins/core/metrics"
"github.com/apache/skywalking-go/plugins/core/operator"
"github.com/apache/skywalking-go/toolkit/metric"
)

type NewCounterInterceptor struct{}

func (h *NewCounterInterceptor) BeforeInvoke(_ operator.Invocation) error {
return nil
}

func (h *NewCounterInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
enhanced := result[0].(operator.EnhancedInstance)
metricName := invocation.Args()[0].(string)
var opts []metrics.Opt
for _, o := range invocation.Args()[1].([]metric.MeterOpt) {
opt := o.(metrics.Opt)
opts = append(opts, opt)
}

counter := metrics.NewCounter(metricName, opts...)
enhanced.SetSkyWalkingDynamicField(counter)
return nil
}
Loading
Loading