Skip to content

Commit

Permalink
fix: instanceName consistency (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
fracasula authored May 19, 2023
1 parent 9eeee52 commit b831f9b
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 199 deletions.
3 changes: 1 addition & 2 deletions stats/internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ func (m *Manager) Shutdown(ctx context.Context) error {

// NewResource allows the creation of an OpenTelemetry resource
// https://opentelemetry.io/docs/concepts/glossary/#resource
func NewResource(svcName, instanceID, svcVersion string, attrs ...attribute.KeyValue) (*resource.Resource, error) {
func NewResource(svcName, svcVersion string, attrs ...attribute.KeyValue) (*resource.Resource, error) {
defaultAttrs := []attribute.KeyValue{
semconv.ServiceNameKey.String(svcName),
semconv.ServiceVersionKey.String(svcVersion),
semconv.ServiceInstanceIDKey.String(instanceID),
}
return resource.Merge(
resource.Default(),
Expand Down
185 changes: 101 additions & 84 deletions stats/internal/otel/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,41 @@ const (
metricsPort = "8889"
)

var (
globalDefaultAttrs = []*promClient.LabelPair{
{Name: ptr("service_version"), Value: ptr("v1.2.3")},
{Name: ptr("telemetry_sdk_language"), Value: ptr("go")},
{Name: ptr("telemetry_sdk_name"), Value: ptr("opentelemetry")},
{Name: ptr("telemetry_sdk_version"), Value: ptr("1.14.0")},
{Name: ptr("instanceName"), Value: ptr("my-instance-id")},
}
globalGRPCDefaultAttrs = append(globalDefaultAttrs,
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
&promClient.LabelPair{Name: ptr("label1"), Value: ptr("value1")},
)
)

// see https://opentelemetry.io/docs/collector/getting-started/
func TestMetrics(t *testing.T) {
var (
ctx = context.Background()
meterName = "some-meter-name"
svcName = "TestMetrics"
svcInstanceName = "my-instance-id"
svcVersion = "v0.10.0"
ctx = context.Background()
meterName = "some-meter-name"
svcName = "TestMetrics"
)
scenarios := []testCase{
{
name: "grpc",
additionalLabels: []*promClient.LabelPair{
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
{Name: ptr("label1"), Value: ptr("value1")},
},
name: "grpc",
additionalLabels: globalGRPCDefaultAttrs,
setupMeterProvider: func(t testing.TB, _ ...MeterProviderOption) (*sdkmetric.MeterProvider, string) {
cwd, err := os.Getwd()
require.NoError(t, err)
container, grpcEndpoint := statsTest.StartOTelCollector(t, metricsPort,
filepath.Join(cwd, "testdata", "otel-collector-config.yaml"),
)

res, err := NewResource(svcName, svcInstanceName, svcVersion)
res, err := NewResource(svcName, "v1.2.3",
attribute.String("instanceName", "my-instance-id"),
)
require.NoError(t, err)
var om Manager
tp, mp, err := om.Setup(ctx, res,
Expand All @@ -79,11 +90,14 @@ func TestMetrics(t *testing.T) {
},
},
{
name: "prometheus",
name: "prometheus",
additionalLabels: globalDefaultAttrs,
setupMeterProvider: func(t testing.TB, _ ...MeterProviderOption) (*sdkmetric.MeterProvider, string) {
registry := prometheus.NewRegistry()

res, err := NewResource(svcName, svcInstanceName, svcVersion)
res, err := NewResource(svcName, "v1.2.3",
attribute.String("instanceName", "my-instance-id"),
)
require.NoError(t, err)
var om Manager
tp, mp, err := om.Setup(ctx, res,
Expand Down Expand Up @@ -138,20 +152,22 @@ func TestMetrics(t *testing.T) {
require.EqualValues(t, ptr(promClient.MetricType_COUNTER), metrics["foo"].Type)
require.Len(t, metrics["foo"].Metric, 1)
require.EqualValues(t, &promClient.Counter{Value: ptr(1.0)}, metrics["foo"].Metric[0].Counter)
require.ElementsMatch(t, append([]*promClient.LabelPair{
{Name: ptr("hello"), Value: ptr("world")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...), metrics["foo"].Metric[0].Label)
require.ElementsMatch(t, append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("hello"), Value: ptr("world")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
), metrics["foo"].Metric[0].Label)

require.EqualValues(t, ptr("bar"), metrics["bar"].Name)
require.EqualValues(t, ptr(promClient.MetricType_COUNTER), metrics["bar"].Type)
require.Len(t, metrics["bar"].Metric, 1)
require.EqualValues(t, &promClient.Counter{Value: ptr(5.0)}, metrics["bar"].Metric[0].Counter)
require.ElementsMatch(t, append([]*promClient.LabelPair{
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...), metrics["bar"].Metric[0].Label)
require.ElementsMatch(t, append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
), metrics["bar"].Metric[0].Label)

requireHistogramEqual(t, metrics["baz"], histogram{
name: "baz", count: 1, sum: 20,
Expand All @@ -161,11 +177,12 @@ func TestMetrics(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(30.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("a"), Value: ptr("b")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("a"), Value: ptr("b")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})

requireHistogramEqual(t, metrics["qux"], histogram{
Expand All @@ -176,38 +193,34 @@ func TestMetrics(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(3.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("c"), Value: ptr("d")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("c"), Value: ptr("d")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})
})
}
}

func TestHistogramBuckets(t *testing.T) {
var (
ctx = context.Background()
svcName = "TestHistogramBuckets"
svcInstanceName = "my-instance-id"
svcVersion = "v0.10.0"
ctx = context.Background()
svcName = "TestHistogramBuckets"
)
scenarios := []testCase{
{
name: "grpc",
additionalLabels: []*promClient.LabelPair{
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
{Name: ptr("label1"), Value: ptr("value1")},
},
name: "grpc",
additionalLabels: globalGRPCDefaultAttrs,
setupMeterProvider: func(t testing.TB, opts ...MeterProviderOption) (*sdkmetric.MeterProvider, string) {
cwd, err := os.Getwd()
require.NoError(t, err)
container, grpcEndpoint := statsTest.StartOTelCollector(t, metricsPort,
filepath.Join(cwd, "testdata", "otel-collector-config.yaml"),
)

res, err := NewResource(svcName, svcInstanceName, svcVersion)
res, err := NewResource(svcName, "v1.2.3", attribute.String("instanceName", "my-instance-id"))
require.NoError(t, err)
var om Manager
_, mp, err := om.Setup(ctx, res,
Expand All @@ -226,11 +239,12 @@ func TestHistogramBuckets(t *testing.T) {
},
},
{
name: "prometheus",
name: "prometheus",
additionalLabels: globalDefaultAttrs,
setupMeterProvider: func(t testing.TB, opts ...MeterProviderOption) (*sdkmetric.MeterProvider, string) {
registry := prometheus.NewRegistry()

res, err := NewResource(svcName, svcInstanceName, svcVersion)
res, err := NewResource(svcName, "v1.2.3", attribute.String("instanceName", "my-instance-id"))
require.NoError(t, err)
var om Manager
tp, mp, err := om.Setup(ctx, res,
Expand Down Expand Up @@ -282,11 +296,12 @@ func TestHistogramBuckets(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(30.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("a"), Value: ptr("b")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("a"), Value: ptr("b")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})

requireHistogramEqual(t, metrics["bar"], histogram{
Expand All @@ -297,11 +312,12 @@ func TestHistogramBuckets(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(30.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("c"), Value: ptr("d")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("c"), Value: ptr("d")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})
})

Expand Down Expand Up @@ -337,11 +353,12 @@ func TestHistogramBuckets(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(30.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("a"), Value: ptr("b")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("a"), Value: ptr("b")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})

requireHistogramEqual(t, metrics["bar"], histogram{
Expand All @@ -352,11 +369,12 @@ func TestHistogramBuckets(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(60.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("c"), Value: ptr("d")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("c"), Value: ptr("d")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})

requireHistogramEqual(t, metrics["baz"], histogram{
Expand All @@ -367,11 +385,12 @@ func TestHistogramBuckets(t *testing.T) {
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(90.0)},
{CumulativeCount: ptr(uint64(1)), UpperBound: ptr(math.Inf(1))},
},
labels: append([]*promClient.LabelPair{
{Name: ptr("e"), Value: ptr("f")},
{Name: ptr("job"), Value: &svcName},
{Name: ptr("instance"), Value: &svcInstanceName},
}, scenario.additionalLabels...),
labels: append(
scenario.additionalLabels,
&promClient.LabelPair{Name: ptr("e"), Value: ptr("f")},
&promClient.LabelPair{Name: ptr("job"), Value: &svcName},
&promClient.LabelPair{Name: ptr("service_name"), Value: &svcName},
),
})
})
})
Expand Down Expand Up @@ -404,7 +423,7 @@ func TestCollectorGlobals(t *testing.T) {
ctx = context.Background()
endpoint = fmt.Sprintf("localhost:%d", grpcPort)
)
res, err := NewResource(t.Name(), "my-instance-id", "1.0.0")
res, err := NewResource(t.Name(), "v1.2.3", attribute.String("instanceName", "my-instance-id"))
require.NoError(t, err)
tp, mp, err := om.Setup(ctx, res,
WithInsecure(),
Expand All @@ -421,7 +440,9 @@ func TestNonBlockingConnection(t *testing.T) {
grpcPort, err := testhelper.GetFreePort()
require.NoError(t, err)

res, err := NewResource(t.Name(), "my-instance-id", "1.0.0")
res, err := NewResource(t.Name(), "v1.2.3",
attribute.String("instanceName", "my-instance-id"),
)
require.NoError(t, err)

var (
Expand Down Expand Up @@ -465,28 +486,24 @@ func TestNonBlockingConnection(t *testing.T) {
metricsEndpoint := fmt.Sprintf("http://localhost:%d/metrics", dt.GetHostPort(t, metricsPort, container))
metrics := requireMetrics(t, metricsEndpoint, "foo", "bar")

defaultAttrs := append(globalGRPCDefaultAttrs,
&promClient.LabelPair{Name: ptr("job"), Value: ptr("TestNonBlockingConnection")},
&promClient.LabelPair{Name: ptr("service_name"), Value: ptr("TestNonBlockingConnection")},
)

require.EqualValues(t, ptr("foo"), metrics["foo"].Name)
require.EqualValues(t, ptr(promClient.MetricType_COUNTER), metrics["foo"].Type)
require.Len(t, metrics["foo"].Metric, 1)
require.EqualValues(t, &promClient.Counter{Value: ptr(123.0)}, metrics["foo"].Metric[0].Counter)
require.ElementsMatch(t, []*promClient.LabelPair{
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
{Name: ptr("label1"), Value: ptr("value1")},
{Name: ptr("hello"), Value: ptr("world")},
{Name: ptr("job"), Value: ptr("TestNonBlockingConnection")},
{Name: ptr("instance"), Value: ptr("my-instance-id")},
}, metrics["foo"].Metric[0].Label)
require.ElementsMatch(t, append(defaultAttrs,
&promClient.LabelPair{Name: ptr("hello"), Value: ptr("world")},
), metrics["foo"].Metric[0].Label)

require.EqualValues(t, ptr("bar"), metrics["bar"].Name)
require.EqualValues(t, ptr(promClient.MetricType_COUNTER), metrics["bar"].Type)
require.Len(t, metrics["bar"].Metric, 1)
require.EqualValues(t, &promClient.Counter{Value: ptr(456.0)}, metrics["bar"].Metric[0].Counter)
require.ElementsMatch(t, []*promClient.LabelPair{
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
{Name: ptr("label1"), Value: ptr("value1")},
{Name: ptr("job"), Value: ptr("TestNonBlockingConnection")},
{Name: ptr("instance"), Value: ptr("my-instance-id")},
}, metrics["bar"].Metric[0].Label)
require.ElementsMatch(t, defaultAttrs, metrics["bar"].Metric[0].Label)
}

func requireMetrics(
Expand Down
11 changes: 0 additions & 11 deletions stats/internal/otel/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type config struct {
disableTargetInfo bool
withoutUnits bool
aggregation metric.AggregationSelector
disableScopeInfo bool
logger logger
}

Expand Down Expand Up @@ -102,16 +101,6 @@ func WithoutUnits() Option {
})
}

// WithoutScopeInfo configures the Exporter to not export the otel_scope_info metric.
// If not specified, the Exporter will create a otel_scope_info metric containing
// the metrics' Instrumentation Scope, and also add labels about Instrumentation Scope to all metric points.
func WithoutScopeInfo() Option {
return optionFunc(func(cfg config) config {
cfg.disableScopeInfo = true
return cfg
})
}

// WithLogger enables the logger
func WithLogger(l logger) Option {
return optionFunc(func(cfg config) config {
Expand Down
Loading

0 comments on commit b831f9b

Please sign in to comment.