mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-22 03:38:42 +02:00
sdk/metric: Log empty meter name (#4153)
* sdk/metric: Log empty meter name * Avoid magic numbers * Lint * Apply suggestions from code review Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log message * Apply suggestion --------- Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
parent
f9565872df
commit
8cddf8cf1f
@ -17,6 +17,7 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric"
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/metric/embedded"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
@ -65,6 +66,10 @@ func NewMeterProvider(options ...Option) *MeterProvider {
|
||||
//
|
||||
// This method is safe to call concurrently.
|
||||
func (mp *MeterProvider) Meter(name string, options ...metric.MeterOption) metric.Meter {
|
||||
if name == "" {
|
||||
global.Warn("Invalid Meter name.", "name", name)
|
||||
}
|
||||
|
||||
c := metric.NewMeterConfig(options...)
|
||||
s := instrumentation.Scope{
|
||||
Name: name,
|
||||
|
@ -16,9 +16,14 @@ package metric
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-logr/logr/funcr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
)
|
||||
|
||||
func TestMeterConcurrentSafe(t *testing.T) {
|
||||
@ -74,3 +79,17 @@ func TestMeterProviderReturnsSameMeter(t *testing.T) {
|
||||
assert.Same(t, mtr, mp.Meter(""))
|
||||
assert.NotSame(t, mtr, mp.Meter("diff"))
|
||||
}
|
||||
|
||||
func TestEmptyMeterName(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
warnLevel := 1
|
||||
l := funcr.New(func(prefix, args string) {
|
||||
_, _ = buf.WriteString(fmt.Sprint(prefix, args))
|
||||
}, funcr.Options{Verbosity: warnLevel})
|
||||
otel.SetLogger(l)
|
||||
mp := NewMeterProvider()
|
||||
|
||||
mp.Meter("")
|
||||
|
||||
assert.Contains(t, buf.String(), `"level"=1 "msg"="Invalid Meter name." "name"=""`)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user