You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/07/10 07:15:38 UTC

[skywalking-go] branch main updated: Support metrics report API for plugin and implement runtime metrics report (#66)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 2e70c50  Support metrics report API for plugin and implement runtime metrics report (#66)
2e70c50 is described below

commit 2e70c500c3d5f694ddc279bdef11b39bbfec30ba
Author: mrproliu <74...@qq.com>
AuthorDate: Mon Jul 10 07:15:33 2023 +0000

    Support metrics report API for plugin and implement runtime metrics report (#66)
---
 .github/workflows/plugin-tests.yaml                |   1 +
 .golangci.yml                                      |   3 +-
 agent/core/compile.go                              |   2 +
 .../development-guide.md                           |  81 +++++
 go.work                                            |   2 +
 plugins/core/context.go                            |   1 +
 plugins/core/instrument/enhance.go                 |   7 +
 plugins/core/metrics.go                            | 377 +++++++++++++++++++++
 plugins/core/metrics/bridge.go                     | 229 +++++++++++++
 plugins/core/metrics/metrics.go                    | 128 +++++++
 plugins/core/operator/common.go                    |   3 +
 plugins/core/operator/{common.go => metrics.go}    |  32 +-
 plugins/core/reporter/api.go                       |  24 +-
 plugins/core/reporter/grpc/grpc.go                 | 130 ++++++-
 plugins/core/reporter/grpc/grpc_opts_enhance.go    |   2 +-
 plugins/core/span_tracing.go                       |   2 +-
 plugins/core/test_base.go                          |  13 +-
 plugins/core/tracer.go                             |  16 +-
 plugins/runtimemetrics/go.mod                      |   3 +
 plugins/runtimemetrics/go.sum                      |   0
 .../common.go => runtimemetrics/instrument.go}     |  51 +--
 plugins/runtimemetrics/registers.go                | 284 ++++++++++++++++
 plugins/sql/go.sum                                 |   2 +
 .../scenarios/runtime_metrics/bin/startup.sh       |  24 ++
 .../scenarios/runtime_metrics/config/excepted.yml  |  31 ++
 test/plugins/scenarios/runtime_metrics/go.mod      |   3 +
 test/plugins/scenarios/runtime_metrics/go.sum      |   0
 test/plugins/scenarios/runtime_metrics/main.go     |  60 ++++
 test/plugins/scenarios/runtime_metrics/plugin.yml  |  26 ++
 tools/go-agent/config/agent.default.yaml           |   3 +
 tools/go-agent/config/loader.go                    |   5 +
 tools/go-agent/instrument/agentcore/instrument.go  |  11 +-
 tools/go-agent/instrument/consts/directive.go      |   3 +
 tools/go-agent/instrument/consts/operator.go       |   7 +
 tools/go-agent/instrument/plugins/instrument.go    |  50 ++-
 tools/go-agent/instrument/plugins/register.go      |   2 +
 .../go-agent/instrument/plugins/rewrite/context.go |  17 +-
 tools/go-agent/instrument/plugins/rewrite/func.go  |  38 ++-
 tools/go-agent/instrument/runtime/instrument.go    |  62 ++++
 tools/go-agent/tools/copy.go                       |   4 +-
 tools/go-agent/tools/dst.go                        |  18 +-
 41 files changed, 1675 insertions(+), 82 deletions(-)

diff --git a/.github/workflows/plugin-tests.yaml b/.github/workflows/plugin-tests.yaml
index 96f6496..21b57c3 100644
--- a/.github/workflows/plugin-tests.yaml
+++ b/.github/workflows/plugin-tests.yaml
@@ -80,6 +80,7 @@ jobs:
           - mongo
           - mysql
           - plugin_exclusion
+          - runtime_metrics
     steps:
       - uses: actions/checkout@v2
         with:
diff --git a/.golangci.yml b/.golangci.yml
index 75f042d..0fde5ef 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -93,9 +93,10 @@ linters:
     - typecheck
     - unconvert
     - unparam
-    - unused
     - varcheck
     - whitespace
+  disable:
+    - unused
 
 service:
   golangci-lint-version: 1.20.x
diff --git a/agent/core/compile.go b/agent/core/compile.go
index dae80cf..5f13941 100644
--- a/agent/core/compile.go
+++ b/agent/core/compile.go
@@ -29,6 +29,8 @@ import (
 	_ "reflect"
 	_ "runtime"
 	_ "runtime/debug"
+	_ "runtime/metrics"
+	_ "sort"
 	_ "strconv"
 	_ "strings"
 	_ "sync"
diff --git a/docs/en/development-and-contribution/development-guide.md b/docs/en/development-and-contribution/development-guide.md
index 4f70d24..f20bc2d 100644
--- a/docs/en/development-and-contribution/development-guide.md
+++ b/docs/en/development-and-contribution/development-guide.md
@@ -452,6 +452,87 @@ Typically, use APIs as following to control or switch the context:
 2. Propagate the snapshot context to any other goroutine in your plugin.
 3. Use `tracing.ContinueContext(snapshot)` to continue the snapshot context in the target goroutine.
 
+### Meter API
+
+The Meter API is used to record the metrics of the target program, and currently supports the following methods:
+
+```go
+// NewCounter creates a new counter metrics.
+// name is the name of the metrics
+// opts is the options for the metrics
+func NewCounter(name string, opts ...Opt) Counter
+
+// NewGauge creates a new gauge metrics.
+// name is the name of the metrics
+// getter is the function to get the value of the gauge meter
+// opts is the options for the metrics
+func NewGauge(name string, getter func() float64, opts ...Opt) Gauge
+
+// NewHistogram creates a new histogram metrics.
+// name is the name of the metrics
+// steps is the buckets of the histogram
+// opts is the options for the metrics
+func NewHistogram(name string, steps []float64, opts ...Opt) Histogram
+
+// NewHistogramWithMinValue creates a new histogram metrics.
+// name is the name of the metrics
+// minVal is the min value of the histogram bucket
+// steps is the buckets of the histogram
+// opts is the options for the metrics
+func NewHistogramWithMinValue(name string, minVal float64, steps []float64, opts ...Opt) Histogram
+
+// RegisterBeforeCollectHook registers a hook function which will be called before metrics collect.
+func RegisterBeforeCollectHook(f func())
+```
+
+#### Meter Option
+
+The Meter Options can be passed when creating a Meter to configure the information in the Meter.
+
+```go
+// WithLabel adds a label to the metrics.
+func WithLabel(key, value string) Opt
+```
+
+#### Meter Type
+
+##### Counter
+
+Counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase.
+
+```go
+type Counter interface {
+	// Get returns the current value of the counter.
+	Get() float64
+	// Inc increments the counter with value.
+	Inc(val float64)
+}
+```
+
+##### Gauge
+
+Gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
+
+```go
+type Gauge interface {
+    // Get returns the current value of the gauge.
+    Get() float64
+}
+```
+
+##### Histogram
+
+Histogram is a metric that represents the distribution of a set of values.
+
+```go
+type Histogram interface {
+	// Observe find the value associate bucket and add 1.
+	Observe(val float64)
+	// ObserveWithCount find the value associate bucket and add specific count.
+	ObserveWithCount(val float64, count int64)
+}
+```
+
 ## Import Plugin
 
 Once you have finished developing the plugin, you need to import the completed module into the Agent program and [define it in the corresponding file](../../../tools/go-agent/instrument/plugins/register.go). 
diff --git a/go.work b/go.work
index 30fefe5..0e4fc6e 100644
--- a/go.work
+++ b/go.work
@@ -15,6 +15,7 @@ use (
 	./plugins/microv4
 	./plugins/mongo
 	./plugins/sql
+	./plugins/runtimemetrics
 
 	./test/benchmark-codebase/consumer
 	./test/benchmark-codebase/provider
@@ -34,6 +35,7 @@ use (
 	./test/plugins/scenarios/mysql
 
 	./test/plugins/scenarios/plugin_exclusion
+	./test/plugins/scenarios/runtime_metrics
 
 	./tools/go-agent
 
diff --git a/plugins/core/context.go b/plugins/core/context.go
index d1dae7c..fe1c561 100644
--- a/plugins/core/context.go
+++ b/plugins/core/context.go
@@ -25,6 +25,7 @@ var (
 	SetGlobalOperator = func(interface{}) {}
 	GetGlobalOperator = func() interface{} { return nil }
 	GetInitNotify     = func() []func() { return nil }
+	MetricsObtain     = func() ([]interface{}, []func()) { return nil, nil }
 )
 
 type ContextSnapshoter interface {
diff --git a/plugins/core/instrument/enhance.go b/plugins/core/instrument/enhance.go
index 117e392..fbc9c1d 100644
--- a/plugins/core/instrument/enhance.go
+++ b/plugins/core/instrument/enhance.go
@@ -24,6 +24,7 @@ type EnhanceType int
 var (
 	EnhanceTypeMethod EnhanceType = 1
 	EnhanceTypeStruct EnhanceType = 2
+	EnhanceTypeForce  EnhanceType = 3
 )
 
 type MethodFilterOption func(decl *dst.FuncDecl, files []*dst.File) bool
@@ -35,6 +36,8 @@ type EnhanceMatcher struct {
 	Receiver      string
 	MethodFilters []MethodFilterOption
 	StructFilters []StructFilterOption
+
+	ForceEnhance bool
 }
 
 // NewStaticMethodEnhance creates a new EnhanceMatcher for static method.
@@ -52,6 +55,10 @@ func NewStructEnhance(name string, filters ...StructFilterOption) *EnhanceMatche
 	return &EnhanceMatcher{Type: EnhanceTypeStruct, Name: name, StructFilters: filters}
 }
 
+func NewForceEnhance() *EnhanceMatcher {
+	return &EnhanceMatcher{Type: EnhanceTypeForce, ForceEnhance: true}
+}
+
 func verifyTypeName(exp dst.Expr, val string) bool {
 	data := generateTypeNameByExp(exp)
 	return data == val
diff --git a/plugins/core/metrics.go b/plugins/core/metrics.go
new file mode 100644
index 0000000..db97404
--- /dev/null
+++ b/plugins/core/metrics.go
@@ -0,0 +1,377 @@
+// 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 core
+
+import (
+	"math"
+	"sort"
+	"strings"
+	"sync/atomic"
+	"time"
+
+	"github.com/apache/skywalking-go/plugins/core/reporter"
+)
+
+func (t *Tracer) Metrics() interface{} {
+	return t
+}
+
+func (t *Tracer) initMetricsCollect(meterCollectSecond int) {
+	collectDuration := time.Duration(meterCollectSecond) * time.Second
+	go func() {
+		for {
+			time.Sleep(collectDuration)
+
+			t.reachNotInitMetrics()
+
+			t.sendMetrics()
+		}
+	}()
+}
+
+func (t *Tracer) reachNotInitMetrics() {
+	registers, hooks := MetricsObtain()
+	if len(registers) == 0 && len(hooks) == 0 {
+		return
+	}
+	for _, meter := range registers {
+		switch m := meter.(type) {
+		case NoInitCounter:
+			counter := newCounter(m.Name(), m.Labels(), m.Get())
+			m.ChangeFunctions(counter.Inc, counter.Get)
+			t.registerMetrics(m.Name(), m.Labels(), counter)
+		case NoInitGauge:
+			gauge := newGauge(m.Name(), m.Labels(), m.Getter())
+			t.registerMetrics(m.Name(), m.Labels(), gauge)
+		case NoInitHistogram:
+			histogram := newHistogramFromExistingBuckets(m.Name(), m.Labels(), m.Buckets())
+			m.ChangeFunctions(histogram.Observe, histogram.ObserveWithCount)
+			t.registerMetrics(m.Name(), m.Labels(), histogram)
+		}
+	}
+	for _, hook := range hooks {
+		t.AddCollectHook(hook)
+	}
+}
+
+func (t *Tracer) sendMetrics() {
+	meters := make([]reporter.ReportedMeter, 0)
+	// call collect hook
+	for _, hook := range t.meterCollectListeners {
+		hook()
+	}
+	t.meterMap.Range(func(key, value interface{}) bool {
+		if m, ok := value.(reporter.ReportedMeter); ok {
+			meters = append(meters, m)
+		} else {
+			t.Log.Errorf("unknown meter type: %T", value)
+		}
+		return true
+	})
+
+	t.Reporter.SendMetrics(meters)
+}
+
+func (t *Tracer) NewCounter(name string, opt interface{}) interface{} {
+	counter := newCounter(name, nil, 0)
+	if o, ok := opt.(meterOpts); ok && o != nil {
+		counter.labels = o.GetLabels()
+	}
+	t.registerMetrics(name, counter.labels, counter)
+	return counter
+}
+
+func (t *Tracer) NewGauge(name string, getter func() float64, opt interface{}) interface{} {
+	gauge := newGauge(name, nil, getter)
+	if o, ok := opt.(meterOpts); ok && o != nil {
+		gauge.labels = o.GetLabels()
+	}
+	t.registerMetrics(name, gauge.labels, gauge)
+	return gauge
+}
+
+func (t *Tracer) NewHistogram(name string, minValue float64, steps []float64, opt interface{}) interface{} {
+	histogram := newHistogramFromSteps(name, nil, minValue, steps)
+	if o, ok := opt.(meterOpts); ok && o != nil {
+		histogram.labels = o.GetLabels()
+	}
+	t.registerMetrics(name, histogram.labels, histogram)
+	return histogram
+}
+
+func (t *Tracer) AddCollectHook(f func()) {
+	t.meterCollectListeners = append(t.meterCollectListeners, f)
+}
+
+func (t *Tracer) registerMetrics(name string, labels map[string]string, meter interface{}) {
+	var sb strings.Builder
+	for k, v := range labels {
+		sb.WriteString(k)
+		sb.WriteString("=")
+		sb.WriteString(v)
+		sb.WriteString(",")
+	}
+	sb.WriteString(name)
+
+	t.meterMap.Store(sb.String(), meter)
+}
+
+type meterOpts interface {
+	GetLabels() map[string]string
+}
+
+type counterImpl struct {
+	name   string
+	labels map[string]string
+
+	valBits uint64
+	valInt  uint64
+}
+
+func newCounter(name string, labels map[string]string, val float64) *counterImpl {
+	var valBits, valInt uint64
+	ival := uint64(val)
+	if float64(ival) == val {
+		valInt = ival
+	} else {
+		valBits = math.Float64bits(val)
+	}
+
+	return &counterImpl{
+		name:    name,
+		labels:  labels,
+		valBits: valBits,
+		valInt:  valInt,
+	}
+}
+
+func (c *counterImpl) Name() string {
+	return c.name
+}
+
+func (c *counterImpl) Labels() map[string]string {
+	return c.labels
+}
+
+func (c *counterImpl) Value() float64 {
+	return c.Get()
+}
+
+func (c *counterImpl) Get() float64 {
+	fval := math.Float64frombits(atomic.LoadUint64(&c.valBits))
+	ival := atomic.LoadUint64(&c.valInt)
+	return fval + float64(ival)
+}
+
+func (c *counterImpl) Inc(val float64) {
+	if val < 0 {
+		return
+	}
+
+	ival := uint64(val)
+	if float64(ival) == val {
+		atomic.AddUint64(&c.valInt, ival)
+		return
+	}
+
+	for {
+		oldBits := atomic.LoadUint64(&c.valBits)
+		newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
+		if atomic.CompareAndSwapUint64(&c.valBits, oldBits, newBits) {
+			return
+		}
+	}
+}
+
+type gaugeImpl struct {
+	name   string
+	labels map[string]string
+	getter func() float64
+}
+
+func newGauge(name string, labels map[string]string, getter func() float64) *gaugeImpl {
+	return &gaugeImpl{
+		name:   name,
+		labels: labels,
+		getter: getter,
+	}
+}
+
+func (g *gaugeImpl) Name() string {
+	return g.name
+}
+
+func (g *gaugeImpl) Labels() map[string]string {
+	return g.labels
+}
+
+func (g *gaugeImpl) Get() float64 {
+	return g.getter()
+}
+
+func (g *gaugeImpl) Value() float64 {
+	return g.getter()
+}
+
+type histogramImpl struct {
+	name   string
+	labels map[string]string
+
+	buckets []*histogramBucket
+}
+
+func (h *histogramImpl) Name() string {
+	return h.name
+}
+
+func (h *histogramImpl) Labels() map[string]string {
+	return h.labels
+}
+
+func (h *histogramImpl) BucketValues() []reporter.ReportedMeterBucketValue {
+	var values []reporter.ReportedMeterBucketValue
+	for _, b := range h.buckets {
+		values = append(values, b)
+	}
+	return values
+}
+
+func (h *histogramImpl) Observe(v float64) {
+	if b := h.findBucket(v); b != nil {
+		atomic.AddInt64(b.value, 1)
+	}
+}
+
+func (h *histogramImpl) ObserveWithCount(v float64, c int64) {
+	if b := h.findBucket(v); b != nil {
+		atomic.AddInt64(b.value, c)
+	}
+}
+
+func (h *histogramImpl) findBucket(v float64) *histogramBucket {
+	var low, high = 0, len(h.buckets) - 1
+
+	for low <= high {
+		var mid = (low + high) / 2
+		if h.buckets[mid].bucket < v {
+			low = mid + 1
+		} else if h.buckets[mid].bucket > v {
+			high = mid - 1
+		} else {
+			return h.buckets[mid]
+		}
+	}
+
+	low--
+
+	if low < len(h.buckets) && low >= 0 {
+		return h.buckets[low]
+	}
+	return nil
+}
+
+type histogramBucket struct {
+	bucket float64
+	value  *int64
+}
+
+func newHistogramFromExistingBuckets(name string, labels map[string]string, buckets []interface{}) *histogramImpl {
+	result := &histogramImpl{
+		name:   name,
+		labels: labels,
+	}
+
+	result.buckets = make([]*histogramBucket, 0, len(buckets))
+	for i, b := range buckets {
+		bucket := b.(NoInitHistogramBucket)
+		result.buckets[i] = &histogramBucket{
+			bucket: bucket.Bucket(),
+			value:  bucket.Value(),
+		}
+	}
+
+	return result
+}
+
+func newHistogramFromSteps(name string, labels map[string]string, minVal float64, steps []float64) *histogramImpl {
+	result := &histogramImpl{
+		name:   name,
+		labels: labels,
+	}
+
+	// sort steps and check for duplicates
+	sort.Float64s(steps)
+	prevVal := steps[0]
+	for i := 1; i < len(steps); i++ {
+		if prevVal == steps[i] {
+			panic("duplicate histogram bucket value")
+		} else {
+			prevVal = steps[i]
+		}
+	}
+	if minVal != steps[0] {
+		steps = append([]float64{minVal}, steps...)
+	}
+
+	result.buckets = make([]*histogramBucket, len(steps))
+	for i, s := range steps {
+		result.buckets[i] = &histogramBucket{
+			bucket: s,
+			value:  new(int64),
+		}
+	}
+
+	return result
+}
+
+func (h *histogramBucket) Bucket() float64 {
+	return h.bucket
+}
+
+func (h *histogramBucket) Count() int64 {
+	return *h.value
+}
+
+func (h *histogramBucket) IsNegativeInfinity() bool {
+	return false
+}
+
+type NoInitCounter interface {
+	Name() string
+	Labels() map[string]string
+	Get() float64
+	ChangeFunctions(add func(v float64), get func() float64)
+}
+
+type NoInitGauge interface {
+	Name() string
+	Labels() map[string]string
+	Getter() func() float64
+}
+
+type NoInitHistogram interface {
+	Name() string
+	Labels() map[string]string
+	Buckets() []interface{}
+	ChangeFunctions(observe func(v float64), observeWithCount func(v float64, c int64))
+}
+
+type NoInitHistogramBucket interface {
+	Bucket() float64
+	Value() *int64
+}
diff --git a/plugins/core/metrics/bridge.go b/plugins/core/metrics/bridge.go
new file mode 100644
index 0000000..4b4342c
--- /dev/null
+++ b/plugins/core/metrics/bridge.go
@@ -0,0 +1,229 @@
+// 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 metrics
+
+func newMeterOpts() *Opts {
+	return &Opts{labels: make(map[string]string)}
+}
+
+func (b *Opts) GetLabels() map[string]string {
+	return b.labels
+}
+
+type counterImpl struct {
+	name   string
+	val    float64
+	labels map[string]string
+
+	getFunc func() float64
+	addFunc func(float64)
+}
+
+func newDefaultCounter(name string, opt ...Opt) *counterImpl {
+	result := &counterImpl{name: name, val: 0}
+	opts := newMeterOpts()
+	for _, o := range opt {
+		o(opts)
+	}
+	result.labels = opts.GetLabels()
+
+	result.getFunc = func() float64 {
+		return result.val
+	}
+	result.addFunc = func(f float64) {
+		result.val += f
+	}
+	return result
+}
+
+func (c *counterImpl) Name() string {
+	return c.name
+}
+
+func (c *counterImpl) Get() float64 {
+	return c.getFunc()
+}
+
+func (c *counterImpl) Labels() map[string]string {
+	return c.labels
+}
+
+func (c *counterImpl) Inc(val float64) {
+	c.addFunc(val)
+}
+
+func (c *counterImpl) ChangeFunctions(add func(v float64), get func() float64) {
+	c.addFunc = add
+	c.getFunc = get
+}
+
+type gaugeImpl struct {
+	name   string
+	labels map[string]string
+	getter func() float64
+}
+
+func newDefaultGauge(name string, getter func() float64, opt ...Opt) *gaugeImpl {
+	result := &gaugeImpl{name: name, getter: getter}
+	opts := newMeterOpts()
+	for _, o := range opt {
+		o(opts)
+	}
+	result.labels = opts.GetLabels()
+	return result
+}
+
+func (g *gaugeImpl) Name() string {
+	return g.name
+}
+
+func (g *gaugeImpl) Labels() map[string]string {
+	return g.labels
+}
+
+func (g *gaugeImpl) Get() float64 {
+	return g.getter()
+}
+
+func (g *gaugeImpl) Getter() func() float64 {
+	return g.getter
+}
+
+type histogramImpl struct {
+	name   string
+	labels map[string]string
+
+	buckets []*histogramBucket
+
+	observeFunc          func(float64)
+	observeWithCountFunc func(float64, int64)
+}
+
+type histogramBucket struct {
+	bucket float64
+	val    *int64
+}
+
+func newDefaultHistogram(name string, minVal float64, steps []float64, opt ...Opt) *histogramImpl {
+	result := &histogramImpl{name: name}
+	opts := newMeterOpts()
+	for _, o := range opt {
+		o(opts)
+	}
+	result.labels = opts.GetLabels()
+
+	result.initBuckets(minVal, steps)
+
+	result.observeFunc = func(f float64) {
+		b := result.findBucket(f)
+		if b != nil {
+			*b.val++
+		}
+	}
+	result.observeWithCountFunc = func(f float64, c int64) {
+		b := result.findBucket(f)
+		if b != nil {
+			*b.val += c
+		}
+	}
+	return result
+}
+
+func (h *histogramImpl) Observe(val float64) {
+	h.observeFunc(val)
+}
+
+func (h *histogramImpl) ObserveWithCount(val float64, c int64) {
+	h.observeWithCountFunc(val, c)
+}
+
+func (h *histogramImpl) findBucket(v float64) *histogramBucket {
+	var low, high = 0, len(h.buckets) - 1
+
+	for low <= high {
+		var mid = (low + high) / 2
+		if h.buckets[mid].bucket < v {
+			low = mid + 1
+		} else if h.buckets[mid].bucket > v {
+			high = mid - 1
+		} else {
+			return h.buckets[mid]
+		}
+	}
+
+	low--
+
+	if low < len(h.buckets) && low >= 0 {
+		return h.buckets[low]
+	}
+	return nil
+}
+
+func (h *histogramImpl) initBuckets(minVal float64, steps []float64) {
+	n := len(steps)
+	for i := 1; i < n; i++ {
+		key := steps[i]
+		j := i - 1
+		if steps[j] == key {
+			panic("duplicate steps found")
+		}
+		for j >= 0 && steps[j] > key {
+			steps[j+1] = steps[j]
+			j--
+		}
+		steps[j+1] = key
+	}
+	if steps[0] != minVal {
+		steps = append([]float64{minVal}, steps...)
+	}
+
+	buckets := make([]*histogramBucket, len(steps))
+	for i, step := range steps {
+		buckets[i] = &histogramBucket{bucket: step, val: new(int64)}
+	}
+	h.buckets = buckets
+}
+
+func (h *histogramImpl) Name() string {
+	return h.name
+}
+
+func (h *histogramImpl) Labels() map[string]string {
+	return h.labels
+}
+
+func (h *histogramImpl) Buckets() []interface{} {
+	result := make([]interface{}, len(h.buckets))
+	for i, b := range h.buckets {
+		result[i] = b
+	}
+	return result
+}
+
+func (h *histogramImpl) ChangeFunctions(observe func(v float64), observeWithCount func(v float64, c int64)) {
+	h.observeFunc = observe
+	h.observeWithCountFunc = observeWithCount
+}
+
+func (h *histogramBucket) Bucket() float64 {
+	return h.bucket
+}
+
+func (h *histogramBucket) Value() *int64 {
+	return h.val
+}
diff --git a/plugins/core/metrics/metrics.go b/plugins/core/metrics/metrics.go
new file mode 100644
index 0000000..24ffade
--- /dev/null
+++ b/plugins/core/metrics/metrics.go
@@ -0,0 +1,128 @@
+// 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 metrics
+
+import "github.com/apache/skywalking-go/plugins/core/operator"
+
+type Opt func(opts *Opts)
+
+type Opts struct {
+	labels map[string]string
+}
+
+// WithLabel adds a label to the metrics.
+func WithLabel(key, value string) Opt {
+	return func(meter *Opts) {
+		meter.labels[key] = value
+	}
+}
+
+type Counter interface {
+	// Get returns the current value of the counter.
+	Get() float64
+	// Inc increments the counter with value.
+	Inc(val float64)
+}
+
+type Gauge interface {
+	// Get returns the current value of the gauge.
+	Get() float64
+}
+
+type Histogram interface {
+	// Observe find the value associate bucket and add 1.
+	Observe(val float64)
+	// ObserveWithCount find the value associate bucket and add specific count.
+	ObserveWithCount(val float64, count int64)
+}
+
+// NewCounter creates a new counter metrics.
+// name is the name of the metrics
+// opts is the options for the metrics
+func NewCounter(name string, opts ...Opt) Counter {
+	op := operator.GetOperator()
+	if op == nil {
+		tmpCounter := newDefaultCounter(name, opts...)
+		operator.MetricsAppender(tmpCounter)
+		return tmpCounter
+	}
+
+	opt := newMeterOpts()
+	for _, o := range opts {
+		o(opt)
+	}
+	return op.Metrics().(operator.MetricsOperator).NewCounter(name, opt).(Counter)
+}
+
+// NewGauge creates a new gauge metrics.
+// name is the name of the metrics
+// getter is the function to get the value of the gauge meter
+// opts is the options for the metrics
+func NewGauge(name string, getter func() float64, opts ...Opt) Gauge {
+	op := operator.GetOperator()
+	if op == nil {
+		tmpGauge := newDefaultGauge(name, getter, opts...)
+		operator.MetricsAppender(tmpGauge)
+		return tmpGauge
+	}
+
+	opt := newMeterOpts()
+	for _, o := range opts {
+		o(opt)
+	}
+	return op.Metrics().(operator.MetricsOperator).NewGauge(name, getter, opt).(Gauge)
+}
+
+// NewHistogram creates a new histogram metrics.
+// name is the name of the metrics
+// steps is the buckets of the histogram
+// opts is the options for the metrics
+func NewHistogram(name string, steps []float64, opts ...Opt) Histogram {
+	return NewHistogramWithMinValue(name, 0, steps, opts...)
+}
+
+// NewHistogramWithMinValue creates a new histogram metrics.
+// name is the name of the metrics
+// minVal is the min value of the histogram bucket
+// steps is the buckets of the histogram
+// opts is the options for the metrics
+func NewHistogramWithMinValue(name string, minVal float64, steps []float64, opts ...Opt) Histogram {
+	op := operator.GetOperator()
+	if op == nil {
+		tmpHistogram := newDefaultHistogram(name, minVal, steps, opts...)
+		operator.MetricsAppender(tmpHistogram)
+		return tmpHistogram
+	}
+
+	opt := newMeterOpts()
+	for _, o := range opts {
+		o(opt)
+	}
+	return op.Metrics().(operator.MetricsOperator).NewHistogram(name, minVal, steps, opt).(Histogram)
+}
+
+// RegisterBeforeCollectHook registers a hook function which will be called before metrics collect.
+func RegisterBeforeCollectHook(f func()) {
+	op := operator.GetOperator()
+	if op == nil {
+		operator.MetricsCollectAppender(f)
+		return
+	}
+
+	op.Metrics().(operator.MetricsOperator).AddCollectHook(f)
+}
diff --git a/plugins/core/operator/common.go b/plugins/core/operator/common.go
index 56f2a1c..55260bb 100644
--- a/plugins/core/operator/common.go
+++ b/plugins/core/operator/common.go
@@ -19,6 +19,8 @@ package operator
 
 var GetOperator = func() Operator { return nil }
 var AppendInitNotify = func(func()) {}
+var MetricsAppender = func(interface{}) {}
+var MetricsCollectAppender = func(func()) {}
 
 type Operator interface {
 	Tracing() interface{} // to TracingOperator
@@ -26,6 +28,7 @@ type Operator interface {
 	Tools() interface{}   // to ToolsOperator
 	DebugStack() []byte   // Getting the stack of the current goroutine, for getting details when plugin broken.
 	Entity() interface{}  // Get the entity of the service
+	Metrics() interface{} // to MetricsOperator
 }
 
 type Entity interface {
diff --git a/plugins/core/operator/common.go b/plugins/core/operator/metrics.go
similarity index 53%
copy from plugins/core/operator/common.go
copy to plugins/core/operator/metrics.go
index 56f2a1c..b422bbc 100644
--- a/plugins/core/operator/common.go
+++ b/plugins/core/operator/metrics.go
@@ -17,31 +17,9 @@
 
 package operator
 
-var GetOperator = func() Operator { return nil }
-var AppendInitNotify = func(func()) {}
-
-type Operator interface {
-	Tracing() interface{} // to TracingOperator
-	Logger() interface{}  // to LogOperator
-	Tools() interface{}   // to ToolsOperator
-	DebugStack() []byte   // Getting the stack of the current goroutine, for getting details when plugin broken.
-	Entity() interface{}  // Get the entity of the service
-}
-
-type Entity interface {
-	GetServiceName() string
-	GetInstanceName() string
-}
-
-// OperateError reduce the "fmt" package import
-type OperateError struct {
-	message string
-}
-
-func (e OperateError) Error() string {
-	return e.message
-}
-
-func NewError(message string) error {
-	return OperateError{message: message}
+type MetricsOperator interface {
+	NewCounter(name string, opts interface{}) interface{}
+	NewGauge(name string, getter func() float64, opts interface{}) interface{}
+	NewHistogram(name string, minValue float64, steps []float64, opts interface{}) interface{}
+	AddCollectHook(func())
 }
diff --git a/plugins/core/reporter/api.go b/plugins/core/reporter/api.go
index b3a0910..ce3b30a 100644
--- a/plugins/core/reporter/api.go
+++ b/plugins/core/reporter/api.go
@@ -62,6 +62,27 @@ type ReportedSpan interface {
 	ComponentID() int32
 }
 
+type ReportedMeter interface {
+	Name() string
+	Labels() map[string]string
+}
+
+type ReportedMeterSingleValue interface {
+	ReportedMeter
+	Value() float64
+}
+
+type ReportedMeterBucketValue interface {
+	Bucket() float64
+	Count() int64
+	IsNegativeInfinity() bool
+}
+
+type ReportedMeterHistogram interface {
+	ReportedMeter
+	BucketValues() []ReportedMeterBucketValue
+}
+
 type Entity struct {
 	ServiceName         string
 	ServiceInstanceName string
@@ -85,7 +106,8 @@ var (
 
 type Reporter interface {
 	Boot(entity *Entity, cdsWatchers []AgentConfigChangeWatcher)
-	Send(spans []ReportedSpan)
+	SendTracing(spans []ReportedSpan)
+	SendMetrics(metrics []ReportedMeter)
 	ConnectionStatus() ConnectionStatus
 	Close()
 }
diff --git a/plugins/core/reporter/grpc/grpc.go b/plugins/core/reporter/grpc/grpc.go
index 604bdf2..8013f67 100644
--- a/plugins/core/reporter/grpc/grpc.go
+++ b/plugins/core/reporter/grpc/grpc.go
@@ -46,7 +46,8 @@ const (
 func NewGRPCReporter(logger operator.LogOperator, serverAddr string, opts ...ReporterOption) (reporter.Reporter, error) {
 	r := &gRPCReporter{
 		logger:           logger,
-		sendCh:           make(chan *agentv3.SegmentObject, maxSendQueueSize),
+		tracingSendCh:    make(chan *agentv3.SegmentObject, maxSendQueueSize),
+		metricsSendCh:    make(chan []*agentv3.MeterData, maxSendQueueSize),
 		checkInterval:    defaultCheckInterval,
 		cdsInterval:      defaultCDSInterval, // cds default on
 		connectionStatus: reporter.ConnectionStatusConnected,
@@ -69,6 +70,7 @@ func NewGRPCReporter(logger operator.LogOperator, serverAddr string, opts ...Rep
 	}
 	r.conn = conn
 	r.traceClient = agentv3.NewTraceSegmentReportServiceClient(r.conn)
+	r.metricsClient = agentv3.NewMeterReportServiceClient(r.conn)
 	r.managementClient = managementv3.NewManagementServiceClient(r.conn)
 	if r.cdsInterval > 0 {
 		r.cdsClient = configuration.NewConfigurationDiscoveryServiceClient(r.conn)
@@ -80,9 +82,11 @@ func NewGRPCReporter(logger operator.LogOperator, serverAddr string, opts ...Rep
 type gRPCReporter struct {
 	entity           *reporter.Entity
 	logger           operator.LogOperator
-	sendCh           chan *agentv3.SegmentObject
+	tracingSendCh    chan *agentv3.SegmentObject
+	metricsSendCh    chan []*agentv3.MeterData
 	conn             *grpc.ClientConn
 	traceClient      agentv3.TraceSegmentReportServiceClient
+	metricsClient    agentv3.MeterReportServiceClient
 	managementClient managementv3.ManagementServiceClient
 	checkInterval    time.Duration
 	cdsInterval      time.Duration
@@ -109,7 +113,7 @@ func (r *gRPCReporter) ConnectionStatus() reporter.ConnectionStatus {
 	return r.connectionStatus
 }
 
-func (r *gRPCReporter) Send(spans []reporter.ReportedSpan) {
+func (r *gRPCReporter) SendTracing(spans []reporter.ReportedSpan) {
 	spanSize := len(spans)
 	if spanSize < 1 {
 		return
@@ -167,21 +171,93 @@ func (r *gRPCReporter) Send(spans []reporter.ReportedSpan) {
 		segmentObject.Spans[i].Refs = srr
 	}
 	defer func() {
-		// recover the panic caused by close sendCh
+		// recover the panic caused by close tracingSendCh
 		if err := recover(); err != nil {
 			r.logger.Errorf("reporter segment err %v", err)
 		}
 	}()
 	select {
-	case r.sendCh <- segmentObject:
+	case r.tracingSendCh <- segmentObject:
 	default:
-		r.logger.Errorf("reach max send buffer")
+		r.logger.Errorf("reach max tracing send buffer")
 	}
 }
 
+func (r *gRPCReporter) SendMetrics(metrics []reporter.ReportedMeter) {
+	if len(metrics) == 0 {
+		return
+	}
+	meters := make([]*agentv3.MeterData, len(metrics))
+	for i, m := range metrics {
+		meter := &agentv3.MeterData{}
+		switch data := m.(type) {
+		case reporter.ReportedMeterSingleValue:
+			meter.Metric = &agentv3.MeterData_SingleValue{
+				SingleValue: &agentv3.MeterSingleValue{
+					Name:   data.Name(),
+					Labels: r.convertLabels(data.Labels()),
+					Value:  data.Value(),
+				},
+			}
+		case reporter.ReportedMeterHistogram:
+			buckets := make([]*agentv3.MeterBucketValue, len(data.BucketValues()))
+			for i, b := range data.BucketValues() {
+				buckets[i] = &agentv3.MeterBucketValue{
+					Bucket:             b.Bucket(),
+					Count:              b.Count(),
+					IsNegativeInfinity: b.IsNegativeInfinity(),
+				}
+			}
+			meter.Metric = &agentv3.MeterData_Histogram{
+				Histogram: &agentv3.MeterHistogram{
+					Name:   data.Name(),
+					Labels: r.convertLabels(data.Labels()),
+					Values: buckets,
+				},
+			}
+		}
+
+		meters[i] = meter
+	}
+
+	meters[0].Service = r.entity.ServiceName
+	meters[0].ServiceInstance = r.entity.ServiceInstanceName
+	meters[0].Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
+	defer func() {
+		// recover the panic caused by close tracingSendCh
+		if err := recover(); err != nil {
+			r.logger.Errorf("reporter metrics err %v", err)
+		}
+	}()
+	select {
+	case r.metricsSendCh <- meters:
+	default:
+		r.logger.Errorf("reach max metrics send buffer")
+	}
+}
+
+func (r *gRPCReporter) convertLabels(labels map[string]string) []*agentv3.Label {
+	if len(labels) == 0 {
+		return nil
+	}
+	ls := make([]*agentv3.Label, 0)
+	for k, v := range labels {
+		ls = append(ls, &agentv3.Label{
+			Name:  k,
+			Value: v,
+		})
+	}
+	return ls
+}
+
 func (r *gRPCReporter) Close() {
-	if r.sendCh != nil && r.bootFlag {
-		close(r.sendCh)
+	if r.bootFlag {
+		if r.tracingSendCh != nil {
+			close(r.tracingSendCh)
+		}
+		if r.metricsSendCh != nil {
+			close(r.metricsSendCh)
+		}
 	} else {
 		r.closeGRPCConn()
 	}
@@ -208,19 +284,42 @@ func (r *gRPCReporter) initSendPipeline() {
 				time.Sleep(5 * time.Second)
 				continue StreamLoop
 			}
-			for s := range r.sendCh {
+			for s := range r.tracingSendCh {
 				err = stream.Send(s)
 				if err != nil {
 					r.logger.Errorf("send segment error %v", err)
-					r.closeStream(stream)
+					r.closeTracingStream(stream)
 					continue StreamLoop
 				}
 			}
-			r.closeStream(stream)
+			r.closeTracingStream(stream)
 			r.closeGRPCConn()
 			break
 		}
 	}()
+	go func() {
+	StreamLoop:
+		for {
+			stream, err := r.metricsClient.CollectBatch(metadata.NewOutgoingContext(context.Background(), r.md))
+			if err != nil {
+				r.logger.Errorf("open stream error %v", err)
+				time.Sleep(5 * time.Second)
+				continue StreamLoop
+			}
+			for s := range r.metricsSendCh {
+				err = stream.Send(&agentv3.MeterDataCollection{
+					MeterData: s,
+				})
+				if err != nil {
+					r.logger.Errorf("send metrics error %v", err)
+					r.closeMetricsStream(stream)
+					continue StreamLoop
+				}
+			}
+			r.closeMetricsStream(stream)
+			break
+		}
+	}()
 }
 
 func (r *gRPCReporter) initCDS(cdsWatchers []reporter.AgentConfigChangeWatcher) {
@@ -259,7 +358,14 @@ func (r *gRPCReporter) initCDS(cdsWatchers []reporter.AgentConfigChangeWatcher)
 	}()
 }
 
-func (r *gRPCReporter) closeStream(stream agentv3.TraceSegmentReportService_CollectClient) {
+func (r *gRPCReporter) closeTracingStream(stream agentv3.TraceSegmentReportService_CollectClient) {
+	_, err := stream.CloseAndRecv()
+	if err != nil && err != io.EOF {
+		r.logger.Errorf("send closing error %v", err)
+	}
+}
+
+func (r *gRPCReporter) closeMetricsStream(stream agentv3.MeterReportService_CollectBatchClient) {
 	_, err := stream.CloseAndRecv()
 	if err != nil && err != io.EOF {
 		r.logger.Errorf("send closing error %v", err)
diff --git a/plugins/core/reporter/grpc/grpc_opts_enhance.go b/plugins/core/reporter/grpc/grpc_opts_enhance.go
index f9231f4..bacd2a1 100644
--- a/plugins/core/reporter/grpc/grpc_opts_enhance.go
+++ b/plugins/core/reporter/grpc/grpc_opts_enhance.go
@@ -42,7 +42,7 @@ func WithCheckInterval(interval time.Duration) ReporterOption {
 // WithMaxSendQueueSize setup send span queue buffer length
 func WithMaxSendQueueSize(maxSendQueueSize int) ReporterOption {
 	return func(r *gRPCReporter) {
-		r.sendCh = make(chan *agentv3.SegmentObject, maxSendQueueSize)
+		r.tracingSendCh = make(chan *agentv3.SegmentObject, maxSendQueueSize)
 	}
 }
 
diff --git a/plugins/core/span_tracing.go b/plugins/core/span_tracing.go
index 1b58b9c..ed558bf 100644
--- a/plugins/core/span_tracing.go
+++ b/plugins/core/span_tracing.go
@@ -352,7 +352,7 @@ func newSegmentRoot(segmentSpan *SegmentSpanImpl) *RootSegmentSpan {
 				break
 			}
 		}
-		s.tracer().Reporter.Send(append(s.segment, s))
+		s.tracer().Reporter.SendTracing(append(s.segment, s))
 	}()
 	return s
 }
diff --git a/plugins/core/test_base.go b/plugins/core/test_base.go
index 8c880e8..148aaf1 100644
--- a/plugins/core/test_base.go
+++ b/plugins/core/test_base.go
@@ -18,6 +18,8 @@
 package core
 
 import (
+	"sync"
+
 	"github.com/apache/skywalking-go/plugins/core/operator"
 	"github.com/apache/skywalking-go/plugins/core/reporter"
 )
@@ -42,7 +44,7 @@ func init() {
 func ResetTracingContext() {
 	SetGLS(nil)
 	Tracing = &Tracer{initFlag: 1, Sampler: NewConstSampler(true), Reporter: &StoreReporter{},
-		ServiceEntity: NewEntity("test", "test-instance")}
+		ServiceEntity: NewEntity("test", "test-instance"), meterMap: &sync.Map{}}
 	SetAsNewGoroutine()
 	ReportConnectionStatus = reporter.ConnectionStatusConnected
 }
@@ -62,7 +64,8 @@ func GetReportedSpans() []reporter.ReportedSpan {
 }
 
 type StoreReporter struct {
-	Spans []reporter.ReportedSpan
+	Spans   []reporter.ReportedSpan
+	Metrics []reporter.ReportedMeter
 }
 
 func NewStoreReporter() *StoreReporter {
@@ -72,10 +75,14 @@ func NewStoreReporter() *StoreReporter {
 func (r *StoreReporter) Boot(entity *reporter.Entity, cdsWatchers []reporter.AgentConfigChangeWatcher) {
 }
 
-func (r *StoreReporter) Send(spans []reporter.ReportedSpan) {
+func (r *StoreReporter) SendTracing(spans []reporter.ReportedSpan) {
 	r.Spans = append(r.Spans, spans...)
 }
 
+func (r *StoreReporter) SendMetrics(metrics []reporter.ReportedMeter) {
+	r.Metrics = append(r.Metrics, metrics...)
+}
+
 func (r *StoreReporter) ConnectionStatus() reporter.ConnectionStatus {
 	return ReportConnectionStatus
 }
diff --git a/plugins/core/tracer.go b/plugins/core/tracer.go
index 82bf720..df0ef68 100644
--- a/plugins/core/tracer.go
+++ b/plugins/core/tracer.go
@@ -22,6 +22,7 @@ import (
 	defLog "log"
 	"os"
 	"reflect"
+	"sync"
 
 	"github.com/apache/skywalking-go/plugins/core/operator"
 	"github.com/apache/skywalking-go/plugins/core/reporter"
@@ -46,9 +47,13 @@ type Tracer struct {
 	cdsWatchers []reporter.AgentConfigChangeWatcher
 	// for plugin tools
 	tools *TracerTools
+	// for all metrics
+	meterMap              *sync.Map
+	meterCollectListeners []func()
 }
 
-func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampler, logger operator.LogOperator) error {
+func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampler, logger operator.LogOperator,
+	meterCollectSecond int) error {
 	t.ServiceEntity = entity
 	t.Reporter = rep
 	t.Sampler = samp
@@ -57,6 +62,7 @@ func (t *Tracer) Init(entity *reporter.Entity, rep reporter.Reporter, samp Sampl
 	}
 	t.Reporter.Boot(entity, t.cdsWatchers)
 	t.initFlag = 1
+	t.initMetricsCollect(meterCollectSecond)
 	// notify the tracer been init success
 	if len(GetInitNotify()) > 0 {
 		for _, fun := range GetInitNotify() {
@@ -101,6 +107,8 @@ func newTracer() *Tracer {
 		Log:         &LogWrapper{newDefaultLogger()},
 		cdsWatchers: make([]reporter.AgentConfigChangeWatcher, 0),
 		tools:       NewTracerTools(),
+
+		meterMap: &sync.Map{},
 	}
 }
 
@@ -120,7 +128,11 @@ func (e *emptyReporter) Boot(entity *reporter.Entity, cdsWatchers []reporter.Age
 }
 
 // nolint
-func (e *emptyReporter) Send(spans []reporter.ReportedSpan) {
+func (e *emptyReporter) SendTracing(spans []reporter.ReportedSpan) {
+}
+
+// nolint
+func (e *emptyReporter) SendMetrics(metrics []reporter.ReportedMeter) {
 }
 
 func (e *emptyReporter) ConnectionStatus() reporter.ConnectionStatus {
diff --git a/plugins/runtimemetrics/go.mod b/plugins/runtimemetrics/go.mod
new file mode 100644
index 0000000..8a6cd1a
--- /dev/null
+++ b/plugins/runtimemetrics/go.mod
@@ -0,0 +1,3 @@
+module github.com/apache/skywalking-go/plugins/runtimemetrics
+
+go 1.18
diff --git a/plugins/runtimemetrics/go.sum b/plugins/runtimemetrics/go.sum
new file mode 100644
index 0000000..e69de29
diff --git a/plugins/core/operator/common.go b/plugins/runtimemetrics/instrument.go
similarity index 53%
copy from plugins/core/operator/common.go
copy to plugins/runtimemetrics/instrument.go
index 56f2a1c..cadf05a 100644
--- a/plugins/core/operator/common.go
+++ b/plugins/runtimemetrics/instrument.go
@@ -15,33 +15,46 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package operator
+package runtimemetrics
 
-var GetOperator = func() Operator { return nil }
-var AppendInitNotify = func(func()) {}
+import (
+	"embed"
 
-type Operator interface {
-	Tracing() interface{} // to TracingOperator
-	Logger() interface{}  // to LogOperator
-	Tools() interface{}   // to ToolsOperator
-	DebugStack() []byte   // Getting the stack of the current goroutine, for getting details when plugin broken.
-	Entity() interface{}  // Get the entity of the service
+	"github.com/apache/skywalking-go/plugins/core/instrument"
+)
+
+//go:embed *
+var fs embed.FS
+
+//skywalking:nocopy
+type Instrument struct {
+}
+
+func NewInstrument() *Instrument {
+	return &Instrument{}
+}
+
+func (r *Instrument) Name() string {
+	return "runtime-metrics"
 }
 
-type Entity interface {
-	GetServiceName() string
-	GetInstanceName() string
+func (r *Instrument) BasePackage() string {
+	return "runtime/metrics"
 }
 
-// OperateError reduce the "fmt" package import
-type OperateError struct {
-	message string
+func (r *Instrument) VersionChecker(version string) bool {
+	return true
 }
 
-func (e OperateError) Error() string {
-	return e.message
+func (r *Instrument) Points() []*instrument.Point {
+	return []*instrument.Point{
+		{
+			PackagePath: "",
+			At:          instrument.NewForceEnhance(),
+		},
+	}
 }
 
-func NewError(message string) error {
-	return OperateError{message: message}
+func (r *Instrument) FS() *embed.FS {
+	return &fs
 }
diff --git a/plugins/runtimemetrics/registers.go b/plugins/runtimemetrics/registers.go
new file mode 100644
index 0000000..26cef85
--- /dev/null
+++ b/plugins/runtimemetrics/registers.go
@@ -0,0 +1,284 @@
+// 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 runtimemetrics
+
+import (
+	"math"
+	original_metrics "runtime/metrics"
+
+	"github.com/apache/skywalking-go/plugins/core/metrics"
+)
+
+//nolint
+var nameReplacing = map[string]*meterInfo{
+	// GC counts
+	"/gc/cycles/automatic:gc-cycles": newMetricsGaugeReplaceInfo("instance_golang_gc_count_labeled", "type", "automatic"),
+	"/gc/cycles/forced:gc-cycles":    newMetricsGaugeReplaceInfo("instance_golang_gc_count_labeled", "type", "forced"),
+	"/gc/cycles/total:gc-cycles":     newMetricsGaugeReplaceInfo("instance_golang_gc_count_labeled", "type", "total"),
+
+	// Heap allocs
+	"/gc/heap/allocs:bytes":   newMetricsGaugeReplaceInfo("instance_golang_heap_alloc_size"),
+	"/gc/heap/allocs:objects": newMetricsGaugeReplaceInfo("instance_golang_heap_alloc_objects"),
+
+	// Heap frees
+	"/gc/heap/frees:bytes":   newMetricsGaugeReplaceInfo("instance_golang_heap_frees"),
+	"/gc/heap/frees:objects": newMetricsGaugeReplaceInfo("instance_golang_heap_frees_objects"),
+
+	// Memory heap
+	"/memory/classes/heap/free:bytes":     newMetricsGaugeReplaceInfo("instance_golang_memory_heap_labeled", "type", "free"),
+	"/memory/classes/heap/objects:bytes":  newMetricsGaugeReplaceInfo("instance_golang_memory_heap_labeled", "type", "objects"),
+	"/memory/classes/heap/released:bytes": newMetricsGaugeReplaceInfo("instance_golang_memory_heap_labeled", "type", "released"),
+	"/memory/classes/heap/stacks:bytes":   newMetricsGaugeReplaceInfo("instance_golang_memory_heap_labeled", "type", "stacks"),
+	"/memory/classes/heap/unused:bytes":   newMetricsGaugeReplaceInfo("instance_golang_memory_heap_labeled", "type", "unused"),
+
+	// Metadata mcache
+	"/memory/classes/metadata/mcache/free:bytes":  newMetricsGaugeReplaceInfo("instance_golang_metadata_mcache_labeled", "type", "free"),
+	"/memory/classes/metadata/mcache/inuse:bytes": newMetricsGaugeReplaceInfo("instance_golang_metadata_mcache_labeled", "type", "inuse"),
+
+	// Metadata mspan
+	"/memory/classes/metadata/mspan/free:bytes":  newMetricsGaugeReplaceInfo("instance_golang_metadata_mspan_labeled", "type", "free"),
+	"/memory/classes/metadata/mspan/inuse:bytes": newMetricsGaugeReplaceInfo("instance_golang_metadata_mspan_labeled", "type", "inuse"),
+
+	// threads
+	"/sched/gomaxprocs:threads":    newMetricsGaugeReplaceInfo("instance_golang_os_threads_num"),
+	"/sched/goroutines:goroutines": newMetricsGaugeReplaceInfo("instance_golang_live_goroutines_num"),
+
+	// Others
+	"/cgo/go-to-c-calls:calls":                newMetricsGaugeReplaceInfo("instance_golang_cgo_calls"),
+	"/gc/heap/goal:bytes":                     newMetricsGaugeReplaceInfo("instance_golang_gc_heap_goal"),
+	"/gc/heap/objects:objects":                newMetricsGaugeReplaceInfo("instance_golang_gc_heap_objects"),
+	"/gc/heap/tiny/allocs:objects":            newMetricsGaugeReplaceInfo("instance_golang_gc_heap_tiny_allocs"),
+	"/gc/limiter/last-enabled:gc-cycle":       newMetricsGaugeReplaceInfo("instance_golang_gc_limiter_last_enabled"),
+	"/gc/stack/starting-size:bytes":           newMetricsGaugeReplaceInfo("instance_golang_gc_stack_starting_size"),
+	"/memory/classes/metadata/other:bytes":    newMetricsGaugeReplaceInfo("instance_golang_memory_metadata_other"),
+	"/memory/classes/os-stacks:bytes":         newMetricsGaugeReplaceInfo("instance_golang_memory_os_stacks"),
+	"/memory/classes/other:bytes":             newMetricsGaugeReplaceInfo("instance_golang_memory_other"),
+	"/memory/classes/profiling/buckets:bytes": newMetricsGaugeReplaceInfo("instance_golang_memory_profiling_buckets"),
+	"/memory/classes/total:bytes":             newMetricsGaugeReplaceInfo("instance_golang_memory_total"),
+
+	// Histogram
+	"/gc/heap/allocs-by-size:bytes": newMetricsHistogramReplaceInfo("instance_golang_gc_heap_allocs_by_size", 1),
+	"/gc/heap/frees-by-size:bytes":  newMetricsHistogramReplaceInfo("instance_golang_gc_heap_frees_by_size", 1),
+	"/gc/pauses:seconds":            newMetricsHistogramReplaceInfo("instance_golang_gc_pauses", 1000_000_000),
+	"/sched/latencies:seconds":      newMetricsHistogramReplaceInfo("instance_golang_sched_latencies", 1000_000_000),
+}
+
+//nolint
+var combinedMetrics = []*meterInfo{
+	newCombinedGaugeInfo("instance_golang_memory_heap_labeled", []string{
+		"/memory/classes/heap/free:bytes", "/memory/classes/heap/objects:bytes", "/memory/classes/heap/released:bytes",
+		"/memory/classes/heap/stacks:bytes", "/memory/classes/heap/unused:bytes",
+	}, "type", "total"),
+}
+
+//nolint
+//skywalking:init
+func registerMetrics() {
+	allMetrics := original_metrics.All()
+	samples := make([]original_metrics.Sample, 0)
+	infos := make(map[string]*meterInfo)
+	combinedInfos := make([]*meterInfo, 0)
+
+	for _, m := range allMetrics {
+		info := nameReplacing[m.Name]
+		if info == nil {
+			continue
+		}
+
+		sample := original_metrics.Sample{Name: m.Name}
+		samples = append(samples, sample)
+		info.init(sample)
+		infos[m.Name] = info
+	}
+
+	for _, info := range combinedMetrics {
+		info.initWithCombined()
+		combinedInfos = append(combinedInfos, info)
+	}
+
+	metrics.RegisterBeforeCollectHook(func() {
+		// reading all samples
+		original_metrics.Read(samples)
+		// updating metrics
+		for _, sample := range samples {
+			if i := infos[sample.Name]; i != nil {
+				i.updateMetricValue(sample)
+			}
+		}
+		for _, info := range combinedInfos {
+			info.updateCombinedMetricValue(samples)
+		}
+	})
+}
+
+type meterInfo struct {
+	// basic info
+	name              string
+	tagOpts           []metrics.Opt
+	isHistogram       bool
+	histogramMultiple int
+
+	// metric value
+	gaugeValue           float64
+	gaugeMetric          metrics.Gauge
+	latestHistogramValue []int64
+	histogramMetric      metrics.Histogram
+	histogramStartInx    int
+
+	// combined metrics
+	needsMetricsNames   []string
+	combinedGaugeMetric metrics.Gauge
+	combinedGaugeValue  float64
+}
+
+func newMetricsGaugeReplaceInfo(name string, tags ...string) *meterInfo {
+	meter := &meterInfo{name: name, isHistogram: false}
+	if len(tags) > 0 {
+		meter.tagOpts = make([]metrics.Opt, 0)
+		for i := 0; i < len(tags); i += 2 {
+			meter.tagOpts = append(meter.tagOpts, metrics.WithLabel(tags[i], tags[i+1]))
+		}
+	}
+	return meter
+}
+
+func newMetricsHistogramReplaceInfo(name string, multiples int) *meterInfo {
+	meter := &meterInfo{name: name, isHistogram: true, histogramMultiple: multiples}
+	return meter
+}
+
+func newCombinedGaugeInfo(name string, needsMetrics []string, tags ...string) *meterInfo {
+	meter := &meterInfo{name: name, isHistogram: false, needsMetricsNames: needsMetrics}
+	if len(tags) > 0 {
+		meter.tagOpts = make([]metrics.Opt, 0)
+		for i := 0; i < len(tags); i += 2 {
+			meter.tagOpts = append(meter.tagOpts, metrics.WithLabel(tags[i], tags[i+1]))
+		}
+	}
+	return meter
+}
+
+func (m *meterInfo) init(sample original_metrics.Sample) {
+	if !m.isHistogram {
+		m.gaugeMetric = metrics.NewGauge(m.name, func() float64 {
+			return m.gaugeValue
+		}, m.tagOpts...)
+		return
+	}
+
+	original_metrics.Read([]original_metrics.Sample{sample})
+	m.initHistogramIfNeeds(sample)
+}
+
+func (m *meterInfo) initWithCombined() {
+	m.combinedGaugeMetric = metrics.NewGauge(m.name, func() float64 {
+		return m.combinedGaugeValue
+	}, m.tagOpts...)
+}
+
+func (m *meterInfo) updateMetricValue(sample original_metrics.Sample) {
+	if !m.isHistogram {
+		if v, ok := m.readingFloat64(sample); ok {
+			m.gaugeValue = v
+		}
+		return
+	}
+
+	m.initHistogramIfNeeds(sample)
+	if m.histogramMetric == nil || m.latestHistogramValue == nil {
+		return
+	}
+	histogram, ok := m.readingHistogram(sample)
+	if !ok {
+		return
+	}
+	for i, val := range histogram.Counts {
+		if i < m.histogramStartInx {
+			continue
+		}
+		if i >= len(m.latestHistogramValue) {
+			break
+		}
+		newestValue := int64(val)
+		if add := newestValue - m.latestHistogramValue[i]; add > 0 {
+			m.histogramMetric.ObserveWithCount(histogram.Buckets[i]*float64(m.histogramMultiple), add)
+			m.latestHistogramValue[i] = newestValue
+		}
+	}
+}
+
+func (m *meterInfo) initHistogramIfNeeds(sample original_metrics.Sample) {
+	if m.histogramMetric != nil {
+		return
+	}
+	histogram, ok := m.readingHistogram(sample)
+	if !ok {
+		return
+	}
+	float64s := make([]float64, 0, len(histogram.Buckets))
+	val := make([]int64, 0, len(histogram.Buckets))
+	var startInx = 0
+	var hasAdded = false
+	for _, b := range histogram.Buckets {
+		if b > math.MaxFloat64 || b < 0 {
+			if !hasAdded {
+				startInx++
+			}
+			continue
+		}
+		hasAdded = true
+		float64s = append(float64s, b*float64(m.histogramMultiple))
+		val = append(val, 0)
+	}
+	m.histogramStartInx = startInx
+	m.histogramMetric = metrics.NewHistogram(m.name, float64s, m.tagOpts...)
+	m.latestHistogramValue = val
+}
+
+func (m *meterInfo) readingFloat64(s original_metrics.Sample) (float64, bool) {
+	switch s.Value.Kind() {
+	case original_metrics.KindUint64:
+		return float64(s.Value.Uint64()), true
+	case original_metrics.KindFloat64:
+		return s.Value.Float64(), true
+	default:
+		return 0, false
+	}
+}
+
+func (m *meterInfo) readingHistogram(s original_metrics.Sample) (*original_metrics.Float64Histogram, bool) {
+	if s.Value.Kind() != original_metrics.KindFloat64Histogram {
+		return nil, false
+	}
+	return s.Value.Float64Histogram(), true
+}
+
+func (m *meterInfo) updateCombinedMetricValue(samples []original_metrics.Sample) {
+	var sum float64
+	for _, name := range m.needsMetricsNames {
+		for _, sample := range samples {
+			if sample.Name == name {
+				if v, ok := m.readingFloat64(sample); ok {
+					sum += v
+				}
+				break
+			}
+		}
+	}
+	m.combinedGaugeValue = sum
+}
diff --git a/plugins/sql/go.sum b/plugins/sql/go.sum
new file mode 100644
index 0000000..fd7ae07
--- /dev/null
+++ b/plugins/sql/go.sum
@@ -0,0 +1,2 @@
+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=
diff --git a/test/plugins/scenarios/runtime_metrics/bin/startup.sh b/test/plugins/scenarios/runtime_metrics/bin/startup.sh
new file mode 100755
index 0000000..c4f9f8b
--- /dev/null
+++ b/test/plugins/scenarios/runtime_metrics/bin/startup.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+# Licensed to the 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.  The 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.
+
+home="$(cd "$(dirname $0)"; pwd)"
+go build ${GO_BUILD_OPTS} -o http
+
+export SW_AGENT_METER_COLLECT_INTERVAL=1
+
+./http
\ No newline at end of file
diff --git a/test/plugins/scenarios/runtime_metrics/config/excepted.yml b/test/plugins/scenarios/runtime_metrics/config/excepted.yml
new file mode 100644
index 0000000..b090973
--- /dev/null
+++ b/test/plugins/scenarios/runtime_metrics/config/excepted.yml
@@ -0,0 +1,31 @@
+# Licensed to the 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.  The 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.
+
+segmentItems: []
+meterItems:
+  - serviceName: runtime_metrics
+    meterSize: ge 1
+    meters:
+      - meterId:
+          name: instance_golang_memory_heap_labeled
+          tags:
+            - { name: type, value: total }
+        singleValue: ge 1
+      - meterId:
+          name: instance_golang_live_goroutines_num
+          tags: []
+        singleValue: ge 1
+logItems: []
\ No newline at end of file
diff --git a/test/plugins/scenarios/runtime_metrics/go.mod b/test/plugins/scenarios/runtime_metrics/go.mod
new file mode 100644
index 0000000..3ab608d
--- /dev/null
+++ b/test/plugins/scenarios/runtime_metrics/go.mod
@@ -0,0 +1,3 @@
+module test/plugins/scenarios/runtime_metrics
+
+go 1.18
diff --git a/test/plugins/scenarios/runtime_metrics/go.sum b/test/plugins/scenarios/runtime_metrics/go.sum
new file mode 100644
index 0000000..e69de29
diff --git a/test/plugins/scenarios/runtime_metrics/main.go b/test/plugins/scenarios/runtime_metrics/main.go
new file mode 100644
index 0000000..23f12ea
--- /dev/null
+++ b/test/plugins/scenarios/runtime_metrics/main.go
@@ -0,0 +1,60 @@
+// 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 main
+
+import (
+	"io"
+	"log"
+	"net/http"
+	"time"
+
+	_ "github.com/apache/skywalking-go"
+)
+
+func providerHandler(w http.ResponseWriter, r *http.Request) {
+	time.Sleep(2 * time.Second) // make sure the meter already uploaded
+	_, _ = w.Write([]byte("success"))
+}
+
+func consumerHandler(w http.ResponseWriter, r *http.Request) {
+	resp, err := http.Get("http://localhost:8080/provider?test=1")
+	if err != nil {
+		log.Printf("request provider error: %v", err)
+		w.WriteHeader(http.StatusInternalServerError)
+		return
+	}
+	defer resp.Body.Close()
+	body, err := io.ReadAll(resp.Body)
+	if err != nil {
+		log.Print(err)
+		w.WriteHeader(http.StatusInternalServerError)
+		return
+	}
+	_, _ = w.Write(body)
+}
+
+func main() {
+	http.HandleFunc("/provider", providerHandler)
+	http.HandleFunc("/consumer", consumerHandler)
+
+	http.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) {
+		writer.WriteHeader(http.StatusOK)
+	})
+
+	_ = http.ListenAndServe(":8080", nil)
+}
diff --git a/test/plugins/scenarios/runtime_metrics/plugin.yml b/test/plugins/scenarios/runtime_metrics/plugin.yml
new file mode 100644
index 0000000..401b195
--- /dev/null
+++ b/test/plugins/scenarios/runtime_metrics/plugin.yml
@@ -0,0 +1,26 @@
+# Licensed to the 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.  The 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.
+
+entry-service: http://${HTTP_HOST}:${HTTP_PORT}/consumer
+health-checker: http://${HTTP_HOST}:${HTTP_PORT}/health
+start-script: ./bin/startup.sh
+framework: go
+export-port: 8080
+support-version:
+  - go: 1.17
+  - go: 1.18
+  - go: 1.19
+  - go: 1.20
diff --git a/tools/go-agent/config/agent.default.yaml b/tools/go-agent/config/agent.default.yaml
index 6e563bd..02fa6ba 100644
--- a/tools/go-agent/config/agent.default.yaml
+++ b/tools/go-agent/config/agent.default.yaml
@@ -24,6 +24,9 @@ agent:
   instance_env_name: SW_AGENT_INSTANCE_NAME
   # Sampling rate of tracing data, which is a floating-point value that must be between 0 and 1.
   sampler: ${SW_AGENT_SAMPLE:1}
+  meter:
+    # The interval of collecting metrics, in seconds.
+    collect_interval: ${SW_AGENT_METER_COLLECT_INTERVAL:20}
 
 reporter:
   grpc:
diff --git a/tools/go-agent/config/loader.go b/tools/go-agent/config/loader.go
index 1ba7250..8d9324d 100644
--- a/tools/go-agent/config/loader.go
+++ b/tools/go-agent/config/loader.go
@@ -48,6 +48,7 @@ type Agent struct {
 	ServiceName     StringValue `yaml:"service_name"`
 	InstanceEnvName StringValue `yaml:"instance_env_name"`
 	Sampler         StringValue `yaml:"sampler"`
+	Meter           Meter       `yaml:"meter"`
 }
 
 type Reporter struct {
@@ -64,6 +65,10 @@ type LogTracing struct {
 	Key     StringValue `yaml:"key"`
 }
 
+type Meter struct {
+	CollectInterval StringValue `yaml:"collect_interval"`
+}
+
 type GRPCReporter struct {
 	BackendService StringValue `yaml:"backend_service"`
 	MaxSendQueue   StringValue `yaml:"max_send_queue"`
diff --git a/tools/go-agent/instrument/agentcore/instrument.go b/tools/go-agent/instrument/agentcore/instrument.go
index 6065e5a..8a0b496 100644
--- a/tools/go-agent/instrument/agentcore/instrument.go
+++ b/tools/go-agent/instrument/agentcore/instrument.go
@@ -146,7 +146,8 @@ func (t *Tracer) InitTracer(extend map[string]interface{}) {
 	}
 	entity := NewEntity({{.Config.Agent.ServiceName.ToGoStringValue}}, {{.Config.Agent.InstanceEnvName.ToGoStringValue}})
 	samp := NewDynamicSampler({{.Config.Agent.Sampler.ToGoFloatValue "loading the agent sampler error"}}, t)
-	if err := t.Init(entity, rep, samp, nil); err != nil {
+	meterCollectInterval := {{.Config.Agent.Meter.CollectInterval.ToGoIntValue "loading the agent meter interval error"}}
+	if err := t.Init(entity, rep, samp, nil, meterCollectInterval); err != nil {
 		t.Log.Errorf("cannot initialize the SkyWalking Tracer: %v", err)
 	}
 }`, struct {
@@ -183,6 +184,9 @@ var {{.GetGoroutineIDLinkMethod}} func() int64
 //go:linkname {{.GetInitNotifyLinkMethod}} {{.GetInitNotifyLinkMethod}}
 var {{.GetInitNotifyLinkMethod}} func() []func()
 
+//go:linkname {{.MetricsObtainMethodName}} {{.MetricsObtainMethodName}}
+var {{.MetricsObtainMethodName}} func() ([]interface{}, []func())
+
 func init() {
 	if {{.TLSGetLinkMethod}} != nil && {{.TLSSetLinkMethod}} != nil {
 		GetGLS = {{.TLSGetLinkMethod}}
@@ -199,6 +203,9 @@ func init() {
 	if {{.GetInitNotifyLinkMethod}} != nil {
 		GetInitNotify = {{.GetInitNotifyLinkMethod}}
 	}
+	if {{.MetricsObtainMethodName}} != nil {
+		MetricsObtain = {{.MetricsObtainMethodName}}
+	}
 }
 `, struct {
 		TLSGetLinkMethod            string
@@ -207,6 +214,7 @@ func init() {
 		GetGlobalOperatorLinkMethod string
 		GetGoroutineIDLinkMethod    string
 		GetInitNotifyLinkMethod     string
+		MetricsObtainMethodName     string
 	}{
 		TLSGetLinkMethod:            consts.TLSGetMethodName,
 		TLSSetLinkMethod:            consts.TLSSetMethodName,
@@ -214,5 +222,6 @@ func init() {
 		GetGlobalOperatorLinkMethod: consts.GlobalTracerGetMethodName,
 		GetGoroutineIDLinkMethod:    consts.CurrentGoroutineIDGetMethodName,
 		GetInitNotifyLinkMethod:     consts.GlobalTracerInitGetNotifyMethodName,
+		MetricsObtainMethodName:     consts.MetricsObtainMethodName,
 	}))
 }
diff --git a/tools/go-agent/instrument/consts/directive.go b/tools/go-agent/instrument/consts/directive.go
index 18e3257..c3fbb48 100644
--- a/tools/go-agent/instrument/consts/directive.go
+++ b/tools/go-agent/instrument/consts/directive.go
@@ -33,4 +33,7 @@ var (
 
 	// DirectiveConfig is the directive for define the config variable
 	DirectiveConfig = "//skywalking:config"
+
+	// DirectiveInit is the directive for define the init function after the agent initialized
+	DirectiveInit = "//skywalking:init"
 )
diff --git a/tools/go-agent/instrument/consts/operator.go b/tools/go-agent/instrument/consts/operator.go
index a7c2eba..da8ff5c 100644
--- a/tools/go-agent/instrument/consts/operator.go
+++ b/tools/go-agent/instrument/consts/operator.go
@@ -22,6 +22,9 @@ const (
 	GlobalLoggerFieldName           = "globalSkyWalkingLogger"
 	GlobalTracerInitNotifyFieldName = "globalSkyWalkingInitNotify"
 
+	MetricsRegisterFieldName = "metricsRegister"
+	MetricsHookFieldName     = "metricsHook"
+
 	GlobalTracerSnapshotInterface = "skywalkingGoroutineSnapshotCreator"
 
 	GlobalTracerSetMethodName              = "_skywalking_set_global_operator"
@@ -31,5 +34,9 @@ const (
 	GlobalTracerInitAppendNotifyMethodName = "_skywalking_global_init_append_notify"
 	GlobalTracerInitGetNotifyMethodName    = "_skywalking_global_init_get_notify"
 
+	MetricsRegisterAppendMethodName = "_skywalking_metrics_register_append"
+	MetricsObtainMethodName         = "_skywalking_metrics_obtain"
+	MetricsHookAppendMethodName     = "_skywalking_metrics_hook_append"
+
 	CurrentGoroutineIDGetMethodName = "_skywalking_get_goid"
 )
diff --git a/tools/go-agent/instrument/plugins/instrument.go b/tools/go-agent/instrument/plugins/instrument.go
index 4667c21..1533ae5 100644
--- a/tools/go-agent/instrument/plugins/instrument.go
+++ b/tools/go-agent/instrument/plugins/instrument.go
@@ -51,9 +51,11 @@ import (
 var templatesFS embed.FS
 
 type Instrument struct {
-	realInst      instrument.Instrument
-	methodFilters []*instrument.Point
-	structFilters []*instrument.Point
+	realInst        instrument.Instrument
+	methodFilters   []*instrument.Point
+	structFilters   []*instrument.Point
+	forceEnhance    bool
+	containsEnhance bool
 
 	compileOpts *api.CompileOptions
 
@@ -118,6 +120,8 @@ func (i *Instrument) CouldHandle(opts *api.CompileOptions) bool {
 					i.methodFilters = append(i.methodFilters, p)
 				case instrument.EnhanceTypeStruct:
 					i.structFilters = append(i.structFilters, p)
+				case instrument.EnhanceTypeForce:
+					i.forceEnhance = true
 				}
 			}
 			return true
@@ -135,6 +139,7 @@ func (i *Instrument) FilterAndEdit(path string, curFile *dst.File, cursor *dstut
 			}
 			i.enhanceStruct(i.realInst, filter, n, path)
 			tools.LogWithStructEnhance(i.compileOpts.Package, n.Name.Name, "", "adding enhanced instance field")
+			i.containsEnhance = true
 			return true
 		}
 	case *dst.FuncDecl:
@@ -149,9 +154,14 @@ func (i *Instrument) FilterAndEdit(path string, curFile *dst.File, cursor *dstut
 				receiver = tools.GenerateTypeNameByExp(n.Recv.List[0].Type)
 			}
 			tools.LogWithMethodEnhance(i.compileOpts.Package, receiver, n.Name.Name, "adding enhanced method")
+			i.containsEnhance = true
 			return true
 		}
 	}
+	if i.forceEnhance && !i.containsEnhance {
+		i.containsEnhance = true
+		return true
+	}
 	return false
 }
 
@@ -194,12 +204,12 @@ func (i *Instrument) AfterEnhanceFile(fromPath, newPath string) error {
 
 func (i *Instrument) WriteExtraFiles(basePath string) ([]string, error) {
 	// if no enhancements or already wrote extra files, then ignore
-	if len(i.enhancements) == 0 || i.extraFilesWrote {
+	if (len(i.enhancements) == 0 && !i.forceEnhance) || i.extraFilesWrote {
 		return nil, nil
 	}
 	i.extraFilesWrote = true
 
-	packageName := ""
+	packageName := filepath.Base(i.compileOpts.Package)
 	for _, e := range i.enhancements {
 		if e.PackageName() != "" {
 			packageName = e.PackageName()
@@ -330,6 +340,7 @@ func (i *Instrument) processPluginConfig(fileContent []byte) error {
 	return nil
 }
 
+//nolint
 func (i *Instrument) copyOperatorsFS(context *rewrite.Context, baseDir, packageName string) ([]string, error) {
 	result := make([]string, 0)
 	var debugBaseDir string
@@ -382,6 +393,12 @@ var {{.OperatorGetLinkMethod}} func() interface{}
 //go:linkname {{.OperatorAppendInitNotifyLinkMethod}} {{.OperatorAppendInitNotifyLinkMethod}}
 var {{.OperatorAppendInitNotifyLinkMethod}} func(func())
 
+//go:linkname {{.MetricsRegisterAppenderLinkMethod}} {{.MetricsRegisterAppenderLinkMethod}}
+var {{.MetricsRegisterAppenderLinkMethod}} func(interface{})
+
+//go:linkname {{.MetricsHookAppenderLinkMethod}} {{.MetricsHookAppenderLinkMethod}}
+var {{.MetricsHookAppenderLinkMethod}} func(func())
+
 func init() {
 	if {{.OperatorGetLinkMethod}} != nil {
 		{{.OperatorGetRealMethod}} = func() {{.OperatorTypeName}} {
@@ -398,6 +415,12 @@ func init() {
 	if {{.OperatorAppendInitNotifyLinkMethod}} != nil {
 		{{.OperatorAppendInitNotifyRealMethod}} = {{.OperatorAppendInitNotifyLinkMethod}}
 	}
+	if {{.MetricsRegisterAppenderLinkMethod}} != nil {
+		{{.MetricsRegisterAppenderRealMethod}} = {{.MetricsRegisterAppenderLinkMethod}}
+	}
+	if {{.MetricsHookAppenderLinkMethod}} != nil {
+		{{.MetricsHookAppenderRealMethod}} = {{.MetricsHookAppenderLinkMethod}}
+	}
 }
 `, struct {
 			PackageName                        string
@@ -406,6 +429,10 @@ func init() {
 			OperatorTypeName                   string
 			OperatorAppendInitNotifyLinkMethod string
 			OperatorAppendInitNotifyRealMethod string
+			MetricsRegisterAppenderLinkMethod  string
+			MetricsRegisterAppenderRealMethod  string
+			MetricsHookAppenderLinkMethod      string
+			MetricsHookAppenderRealMethod      string
 		}{
 			PackageName:                        packageName,
 			OperatorGetLinkMethod:              consts.GlobalTracerGetMethodName,
@@ -413,6 +440,10 @@ func init() {
 			OperatorTypeName:                   rewrite.GlobalOperatorTypeName,
 			OperatorAppendInitNotifyLinkMethod: consts.GlobalTracerInitAppendNotifyMethodName,
 			OperatorAppendInitNotifyRealMethod: rewrite.GlobalOperatorRealAppendTracerInitNotify,
+			MetricsRegisterAppenderLinkMethod:  consts.MetricsRegisterAppendMethodName,
+			MetricsRegisterAppenderRealMethod:  rewrite.MetricsRegisterAppender,
+			MetricsHookAppenderLinkMethod:      consts.MetricsHookAppendMethodName,
+			MetricsHookAppenderRealMethod:      rewrite.MetricsCollectAppender,
 		}),
 	})
 	if err != nil {
@@ -462,6 +493,15 @@ func (i *Instrument) writeDelegatorFile(ctx *rewrite.Context, basePath string) (
 
 	ctx.SingleFile(file)
 
+	if len(ctx.InitFuncDetector) > 0 {
+		for _, fun := range ctx.InitFuncDetector {
+			initFunc.Body.List = append(initFunc.Body.List, &dst.ExprStmt{X: &dst.CallExpr{
+				Fun:  dst.NewIdent(rewrite.GlobalOperatorRealAppendTracerInitNotify),
+				Args: []dst.Expr{dst.NewIdent(fun)},
+			}})
+		}
+	}
+
 	adapterFile := filepath.Join(basePath, "skywalking_delegator.go")
 	if err := tools.WriteDSTFile(adapterFile, file, nil); err != nil {
 		return nil, err
diff --git a/tools/go-agent/instrument/plugins/register.go b/tools/go-agent/instrument/plugins/register.go
index 0089122..7cb9a49 100644
--- a/tools/go-agent/instrument/plugins/register.go
+++ b/tools/go-agent/instrument/plugins/register.go
@@ -29,6 +29,7 @@ import (
 	"github.com/apache/skywalking-go/plugins/kratosv2"
 	"github.com/apache/skywalking-go/plugins/microv4"
 	"github.com/apache/skywalking-go/plugins/mongo"
+	runtime_metrics "github.com/apache/skywalking-go/plugins/runtimemetrics"
 	sql_entry "github.com/apache/skywalking-go/plugins/sql/entry"
 	sql_mysql "github.com/apache/skywalking-go/plugins/sql/mysql"
 )
@@ -45,6 +46,7 @@ func init() {
 	registerFramework(microv4.NewInstrument())
 	registerFramework(goredisv9.NewInstrument())
 	registerFramework(mongo.NewInstrument())
+	registerFramework(runtime_metrics.NewInstrument())
 
 	// gorm related instruments
 	registerFramework(gorm_entry.NewInstrument())
diff --git a/tools/go-agent/instrument/plugins/rewrite/context.go b/tools/go-agent/instrument/plugins/rewrite/context.go
index 3bd866f..47d2a4e 100644
--- a/tools/go-agent/instrument/plugins/rewrite/context.go
+++ b/tools/go-agent/instrument/plugins/rewrite/context.go
@@ -39,7 +39,7 @@ var GenerateCommonPrefix = "skywalking_"
 
 var GenerateMethodPrefix = GenerateCommonPrefix + "enhance_"
 var GenerateVarPrefix = GenerateCommonPrefix + "var_"
-var OperatorDirs = []string{"operator", "log", "tracing", "tools"}
+var OperatorDirs = []string{"operator", "log", "tracing", "tools", "metrics"}
 
 var OperatePrefix = GenerateCommonPrefix + "operator"
 var TypePrefix = OperatePrefix + "Type"
@@ -57,6 +57,8 @@ type Context struct {
 
 	packageImport  map[string]*rewriteImportInfo
 	rewriteMapping *rewriteMapping
+
+	InitFuncDetector []string
 }
 
 func NewContext(compilePkgFullPath, targetPackage string) *Context {
@@ -76,6 +78,10 @@ func NewContext(compilePkgFullPath, targetPackage string) *Context {
 	return c
 }
 
+func (c *Context) appendInitFunction(name string) {
+	c.InitFuncDetector = append(c.InitFuncDetector, name)
+}
+
 type rewriteImportInfo struct {
 	pkgName     string
 	isAgentCore bool
@@ -269,6 +275,11 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType dst.Expr, parent dst.Node
 			} else {
 				panic("binary expr arg index error")
 			}
+		case *dst.CaseClause:
+			if argIndex < 0 {
+				panic("case clause arg index error")
+			}
+			p.List[argIndex] = generateExpr()
 		}
 	case *dst.StarExpr:
 		return c.enhanceTypeNameWhenRewrite(t.X, t, -1)
@@ -281,6 +292,8 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType dst.Expr, parent dst.Node
 			// for struct data, ex: "&xxx{k: v}"
 			if kv, ok := elt.(*dst.KeyValueExpr); ok {
 				c.rewriteVarIfExistingMapping(kv.Value, elt)
+			} else if call, ok := elt.(*dst.CallExpr); ok {
+				c.enhanceTypeNameWhenRewrite(call, t, -1)
 			}
 		}
 		return c.enhanceTypeNameWhenRewrite(t.Type, t, -1)
@@ -328,7 +341,7 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType dst.Expr, parent dst.Node
 
 func (c *Context) typeIsBasicTypeValueOrEnhanceName(name string) bool {
 	if strings.HasPrefix(name, OperatePrefix) || strings.HasPrefix(name, GenerateMethodPrefix) || tools.IsBasicDataType(name) ||
-		name == "nil" || name == "true" || name == "false" || name == "append" || name == "panic" {
+		name == "nil" || name == "true" || name == "false" || name == "append" || name == "panic" || name == "new" {
 		return true
 	}
 	if _, valErr := strconv.ParseFloat(name, 64); valErr == nil {
diff --git a/tools/go-agent/instrument/plugins/rewrite/func.go b/tools/go-agent/instrument/plugins/rewrite/func.go
index 9c7a4bc..eb1ddff 100644
--- a/tools/go-agent/instrument/plugins/rewrite/func.go
+++ b/tools/go-agent/instrument/plugins/rewrite/func.go
@@ -34,6 +34,8 @@ var (
 	GlobalOperatorRealGetMethodName = VarPrefix + "OperatorGetOperator"
 
 	GlobalOperatorRealAppendTracerInitNotify = VarPrefix + "OperatorAppendInitNotify"
+	MetricsRegisterAppender                  = VarPrefix + "OperatorMetricsAppender"
+	MetricsCollectAppender                   = VarPrefix + "OperatorMetricsCollectAppender"
 
 	GlobalOperatorTypeName = TypePrefix + "OperatorOperator"
 )
@@ -68,6 +70,8 @@ func (c *Context) Func(funcDecl *dst.FuncDecl, cursor *dstutil.Cursor) {
 		}
 	}
 
+	c.initFunctionDetector(funcDecl)
+
 	// enhance method parameter and return value
 	c.enhanceFuncParameter(funcDecl.Type.Params)
 	c.enhanceFuncParameter(funcDecl.Type.Results)
@@ -78,6 +82,16 @@ func (c *Context) Func(funcDecl *dst.FuncDecl, cursor *dstutil.Cursor) {
 	}
 }
 
+func (c *Context) initFunctionDetector(f *dst.FuncDecl) {
+	initFunc := tools.FindDirective(f, consts.DirectiveInit)
+	if initFunc != "" {
+		if f.Recv != nil && len(f.Recv.List) > 0 {
+			panic("init function should not have receiver")
+		}
+		c.appendInitFunction(f.Name.Name)
+	}
+}
+
 // nolint
 func (c *Context) enhanceFuncStmt(stmt dst.Stmt) {
 	// for the variables created in the sub statement, ex: if, func(), the temporary variable count should be recorded
@@ -153,7 +167,15 @@ func (c *Context) enhanceFuncStmt(stmt dst.Stmt) {
 				}
 			}
 		case *dst.ValueSpec:
-			c.Var(n, false)
+			for _, n := range n.Names {
+				if k, v := c.enhanceVarNameWhenRewrite(n); k != "" {
+					c.rewriteMapping.addVarMapping(k, v)
+				}
+			}
+			c.enhanceTypeNameWhenRewrite(n.Type, n, -1)
+			for _, subVal := range n.Values {
+				c.enhanceTypeNameWhenRewrite(subVal, n, -1)
+			}
 		case *dst.TypeSwitchStmt:
 			c.enhanceFuncStmt(n.Init)
 			c.enhanceFuncStmt(n.Assign)
@@ -162,9 +184,17 @@ func (c *Context) enhanceFuncStmt(stmt dst.Stmt) {
 					c.enhanceFuncStmt(stmt)
 				}
 			}
+		case *dst.SwitchStmt:
+			c.enhanceFuncStmt(n.Init)
+			c.enhanceTypeNameWhenRewrite(n.Tag, n, -1)
+			if n.Body != nil {
+				for _, stmt := range n.Body.List {
+					c.enhanceFuncStmt(stmt)
+				}
+			}
 		case *dst.CaseClause:
-			for _, stmt := range n.List {
-				c.enhanceTypeNameWhenRewrite(stmt, n, -1)
+			for i, stmt := range n.List {
+				c.enhanceTypeNameWhenRewrite(stmt, n, i)
 			}
 			for _, stmt := range n.Body {
 				c.enhanceFuncStmt(stmt)
@@ -197,7 +227,7 @@ func (c *Context) rewriteVarIfExistingMapping(exp, parent dst.Expr) bool {
 	case *dst.SelectorExpr:
 		if pkg, ok := n.X.(*dst.Ident); ok {
 			if imp := c.packageImport[pkg.Name]; imp != nil {
-				tools.RemovePackageRef(parent, n)
+				tools.RemovePackageRef(parent, n, -1)
 				return true
 			}
 		}
diff --git a/tools/go-agent/instrument/runtime/instrument.go b/tools/go-agent/instrument/runtime/instrument.go
index 9c56380..08d2073 100644
--- a/tools/go-agent/instrument/runtime/instrument.go
+++ b/tools/go-agent/instrument/runtime/instrument.go
@@ -105,6 +105,8 @@ func (r *Instrument) WriteExtraFiles(dir string) ([]string, error) {
 
 import (
 	_ "unsafe"
+
+	atomic "runtime/internal/atomic"
 )
 
 var {{.GlobalTracerFieldName}} interface{}
@@ -113,6 +115,11 @@ var {{.GlobalLoggerFieldName}} interface{}
 
 var {{.GlobalTracerInitNotifyFieldName}} = make([]func(), 0)
 
+var _metricsRegisterLockerVal int32 = 0
+var _metricsRegisterLocker = &_metricsRegisterLockerVal
+var {{.MetricsRegisterFieldName}} = make([]interface{}, 0)
+var {{.MetricsHookFieldName}} = make([]func(), 0)
+
 //go:linkname {{.TLSGetMethod}} {{.TLSGetMethod}}
 var {{.TLSGetMethod}} = _skywalking_tls_get_impl
 
@@ -140,6 +147,12 @@ var {{.GlobalTracerInitNotifyMethodName}} = _skywalking_global_tracer_init_notif
 //go:linkname {{.GlobalTracerInitNotifyGetMethodName}} {{.GlobalTracerInitNotifyGetMethodName}}
 var {{.GlobalTracerInitNotifyGetMethodName}} = _skywalking_global_tracer_init_get_notify_impl
 
+//go:linkname {{.MetricsRegisterAppendMethodName}} {{.MetricsRegisterAppendMethodName}}
+var {{.MetricsRegisterAppendMethodName}} = _skywalking_metrics_register_append_impl
+
+//go:linkname {{.MetricsObtainMethodName}} {{.MetricsObtainMethodName}}
+var {{.MetricsObtainMethodName}} = _skywalking_metrics_obtain_impl
+
 //go:nosplit
 func _skywalking_get_goid_impl() int64 {
 	return {{.GoroutineIDCaster}}
@@ -185,6 +198,45 @@ func _skywalking_global_tracer_init_get_notify_impl() []func() {
 	return {{.GlobalTracerInitNotifyFieldName}}
 }
 
+//go:nosplit
+func _skywalking_metrics_register_append_impl(v interface{}) {
+	for {
+		tmp := atomic.Loadint32(_metricsRegisterLocker)
+		if atomic.Casint32(_metricsRegisterLocker, tmp, tmp+1) {
+			{{.MetricsRegisterFieldName}} = append({{.MetricsRegisterFieldName}}, v)
+			break
+		}
+	}
+}
+
+//go:nosplit
+func _skywalking_metrics_obtain_impl() ([]interface{}, []func()) {
+	for {
+		tmp := atomic.Loadint32(_metricsRegisterLocker)
+		if tmp == 0 {
+			return nil, nil
+		}
+		if atomic.Casint32(_metricsRegisterLocker, tmp, 0) {
+			registers := {{.MetricsRegisterFieldName}}
+			{{.MetricsRegisterFieldName}} = make([]interface{}, 0)
+			hooks := {{.MetricsHookFieldName}}
+			{{.MetricsHookFieldName}} = make([]func(), 0)
+			return registers, hooks
+		}
+	}
+}
+
+//go:nosplit
+func _skywalking_metrics_hook_append_impl(f func()) {
+	for {
+		tmp := atomic.Loadint32(_metricsRegisterLocker)
+		if atomic.Casint32(_metricsRegisterLocker, tmp, tmp+1) {
+			{{.MetricsHookFieldName}} = append({{.MetricsHookFieldName}}, f)
+			break
+		}
+	}
+}
+
 type ContextSnapshoter interface {
 	TakeSnapShot(val interface{}) interface{}
 }
@@ -214,6 +266,11 @@ func goroutineChange(tls interface{}) interface{} {
 			GlobalTracerInitNotifyFieldName     string
 			GlobalTracerInitNotifyMethodName    string
 			GlobalTracerInitNotifyGetMethodName string
+			MetricsRegisterFieldName            string
+			MetricsRegisterAppendMethodName     string
+			MetricsObtainMethodName             string
+			MetricsHookFieldName                string
+			MetricsHookAppendMethodName         string
 		}{
 			TLSFiledName:                        consts.TLSFieldName,
 			TLSGetMethod:                        consts.TLSGetMethodName,
@@ -230,6 +287,11 @@ func goroutineChange(tls interface{}) interface{} {
 			GlobalTracerInitNotifyFieldName:     consts.GlobalTracerInitNotifyFieldName,
 			GlobalTracerInitNotifyMethodName:    consts.GlobalTracerInitAppendNotifyMethodName,
 			GlobalTracerInitNotifyGetMethodName: consts.GlobalTracerInitGetNotifyMethodName,
+			MetricsRegisterFieldName:            consts.MetricsRegisterFieldName,
+			MetricsRegisterAppendMethodName:     consts.MetricsRegisterAppendMethodName,
+			MetricsObtainMethodName:             consts.MetricsObtainMethodName,
+			MetricsHookFieldName:                consts.MetricsHookFieldName,
+			MetricsHookAppendMethodName:         consts.MetricsHookAppendMethodName,
 		}),
 	})
 }
diff --git a/tools/go-agent/tools/copy.go b/tools/go-agent/tools/copy.go
index 422acd9..fa63b93 100644
--- a/tools/go-agent/tools/copy.go
+++ b/tools/go-agent/tools/copy.go
@@ -25,6 +25,8 @@ import (
 
 	"github.com/dave/dst"
 	"github.com/dave/dst/decorator"
+
+	"github.com/apache/skywalking-go/tools/go-agent/instrument/consts"
 )
 
 func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir string,
@@ -49,7 +51,7 @@ func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir string,
 		}
 
 		// ignore nocopy files
-		if bytes.Contains(readFile, []byte("//skywalking:nocopy")) {
+		if bytes.Contains(readFile, []byte(consts.DirecitveNoCopy)) {
 			continue
 		}
 
diff --git a/tools/go-agent/tools/dst.go b/tools/go-agent/tools/dst.go
index 95b858b..0909895 100644
--- a/tools/go-agent/tools/dst.go
+++ b/tools/go-agent/tools/dst.go
@@ -77,7 +77,19 @@ func DeletePackageImports(file dst.Node, imports ...string) {
 				return true
 			}
 			if _, ok := deletedPackages[pkgRefName.Name]; ok {
-				RemovePackageRef(cursor.Parent(), n)
+				RemovePackageRef(cursor.Parent(), n, -1)
+			}
+		case *dst.CaseClause:
+			for i, d := range n.List {
+				if sel, ok := d.(*dst.SelectorExpr); ok {
+					pkgRefName, ok := sel.X.(*dst.Ident)
+					if !ok {
+						return true
+					}
+					if _, ok := deletedPackages[pkgRefName.Name]; ok {
+						RemovePackageRef(n, sel, i)
+					}
+				}
 			}
 		}
 		return true
@@ -90,7 +102,7 @@ func DeletePackageImports(file dst.Node, imports ...string) {
 	}
 }
 
-func RemovePackageRef(parent dst.Node, current *dst.SelectorExpr) {
+func RemovePackageRef(parent dst.Node, current *dst.SelectorExpr, inx int) {
 	switch p := parent.(type) {
 	case *dst.Field:
 		p.Type = dst.NewIdent(current.Sel.Name)
@@ -110,6 +122,8 @@ func RemovePackageRef(parent dst.Node, current *dst.SelectorExpr) {
 		p.Value = dst.NewIdent(current.Sel.Name)
 	case *dst.AssignStmt:
 		p.Rhs = []dst.Expr{dst.NewIdent(current.Sel.Name)}
+	case *dst.CaseClause:
+		p.List[inx] = dst.NewIdent(current.Sel.Name)
 	}
 }