Skip to content

Commit

Permalink
feature: add some util functions for tracer pkg and metric pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
anhvietnguyennva committed Aug 17, 2023
1 parent e42bdbc commit efae8be
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/metric/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (

func init() {
if env.BoolFromEnv(constant.EnvKeyOTLPEnabled) {
initProvider()
InitProvider()
}
}
12 changes: 0 additions & 12 deletions pkg/metric/meter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package metric

import (
"context"
"errors"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
metricsdk "go.opentelemetry.io/otel/sdk/metric"

"github.com/KyberNetwork/kyber-trace-go/pkg/constant"
"github.com/KyberNetwork/kyber-trace-go/pkg/util/env"
Expand All @@ -15,11 +11,3 @@ import (
func Meter() metric.Meter {
return otel.GetMeterProvider().Meter(env.StringFromEnv(constant.EnvKeyOTLPServiceName, constant.OTLPDefaultServiceName))
}

func Flush(ctx context.Context) error {
if m, ok := otel.GetMeterProvider().(*metricsdk.MeterProvider); ok {
return m.ForceFlush(ctx)
} else {
return errors.New("no meter provider was initialized")
}
}
6 changes: 5 additions & 1 deletion pkg/metric/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func newResources() *resource.Resource {
)
}

func initProvider() {
func InitProvider() {
lock.Lock()
defer lock.Unlock()

Expand All @@ -98,3 +98,7 @@ func initProvider() {

otel.SetMeterProvider(provider)
}

func Provider() *metric.MeterProvider {
return provider
}
46 changes: 46 additions & 0 deletions pkg/metric/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package metric

import (
"context"
"errors"
)

// Flush flushes all pending telemetry.
//
// This method honors the deadline or cancellation of ctx. An appropriate
// error will be returned in these situations. There is no guaranteed that all
// telemetry be flushed or all resources have been released in these
// situations.
//
// This method is safe to call concurrently.
func Flush(ctx context.Context) error {
if provider != nil {
return provider.ForceFlush(ctx)
} else {
return errors.New("no meter provider was initialized")
}
}

// Shutdown shuts down the MeterProvider flushing all pending telemetry and
// releasing any held computational resources.
//
// This call is idempotent. The first call will perform all flush and
// releasing operations. Subsequent calls will perform no action and will
// return an error stating this.
//
// Measurements made by instruments from meters this MeterProvider created
// will not be exported after Shutdown is called.
//
// This method honors the deadline or cancellation of ctx. An appropriate
// error will be returned in these situations. There is no guaranteed that all
// telemetry be flushed or all resources have been released in these
// situations.
//
// This method is safe to call concurrently.
func Shutdown(ctx context.Context) error {
if provider != nil {
return provider.Shutdown(ctx)
} else {
return errors.New("no meter provider was initialized")
}
}
2 changes: 1 addition & 1 deletion pkg/tracer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (

func init() {
if env.BoolFromEnv(constant.EnvKeyOTLPEnabled) {
initProvider()
InitProvider()
}
}
6 changes: 5 additions & 1 deletion pkg/tracer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func newResources() *resource.Resource {
)
}

func initProvider() {
func InitProvider() {
lock.Lock()
defer lock.Unlock()

Expand All @@ -110,3 +110,7 @@ func initProvider() {
otel.SetTracerProvider(provider)
otel.SetTextMapPropagator(propagation.TraceContext{})
}

func Provider() *trace.TracerProvider {
return provider
}
27 changes: 27 additions & 0 deletions pkg/tracer/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tracer

import (
"context"
"errors"
)

// Shutdown shuts down TracerProvider. All registered span processors are shut down
// in the order they were registered and any held computational resources are released.
// After Shutdown is called, all methods are no-ops.
func Shutdown(ctx context.Context) error {
if provider != nil {
return provider.Shutdown(ctx)
} else {
return errors.New("no tracer provider was initialized")
}
}

// Flush immediately exports all spans that have not yet been exported for
// all the registered span processors
func Flush(ctx context.Context) error {
if provider != nil {
return provider.ForceFlush(ctx)
} else {
return errors.New("no tracer provider was initialized")
}
}

0 comments on commit efae8be

Please sign in to comment.