Skip to content

Commit

Permalink
[open-telemetryGH-16344] spanmetricsconnector added tests and fixed s…
Browse files Browse the repository at this point in the history
…pelling after review
  • Loading branch information
LaserPhaser committed Jun 7, 2023
1 parent 188b884 commit 69ac20d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions connector/spanmetricsconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following settings can be optionally configured:

- `histogram` (default: `explicit`): Use to configure the type of histogram to record
calculated from spans duration measurements. Must be either `explicit` or `exponential`.
- `disable` (default: `false`): Use to disable histogram metrics at all.
- `disable` (default: `false`): Disable all histogram metrics.
- `unit` (default: `ms`): The time unit for recording duration measurements.
calculated from spans duration measurements. One of either: `ms` or `s`.
- `explicit`:
Expand All @@ -91,7 +91,7 @@ The following settings can be optionally configured:
If the `name`d attribute is missing in the span, the optional provided `default` is used.

If no `default` is provided, this dimension will be **omitted** from the metric.
- `exclude_dimensions`: the list of dimensions to be excluded from default. Use to exclude unneeded data from metrics.
- `exclude_dimensions`: the list of dimensions to be excluded from the default set of dimensions. Use to exclude unneeded data from metrics.
- `dimensions_cache_size` (default: `1000`): the size of cache for storing Dimensions to improve collectors memory usage. Must be a positive number.
- `aggregation_temporality` (default: `AGGREGATION_TEMPORALITY_CUMULATIVE`): Defines the aggregation temporality of the generated metrics.
One of either `AGGREGATION_TEMPORALITY_CUMULATIVE` or `AGGREGATION_TEMPORALITY_DELTA`.
Expand Down
16 changes: 12 additions & 4 deletions connector/spanmetricsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,18 @@ func concatDimensionValue(dest *bytes.Buffer, value string, prefixSep bool) {
// The metric key is a simple concatenation of dimension values, delimited by a null character.
func (p *connectorImp) buildKey(serviceName string, span ptrace.Span, optionalDims []dimension, resourceAttrs pcommon.Map) metrics.Key {
p.keyBuf.Reset()
concatDimensionValue(p.keyBuf, serviceName, false)
concatDimensionValue(p.keyBuf, span.Name(), true)
concatDimensionValue(p.keyBuf, traceutil.SpanKindStr(span.Kind()), true)
concatDimensionValue(p.keyBuf, traceutil.StatusCodeStr(span.Status().Code()), true)
if !contains(p.config.ExcludeDimensions, serviceNameKey) {
concatDimensionValue(p.keyBuf, serviceName, false)
}
if !contains(p.config.ExcludeDimensions, spanNameKey) {
concatDimensionValue(p.keyBuf, span.Name(), true)
}
if !contains(p.config.ExcludeDimensions, spanKindKey) {
concatDimensionValue(p.keyBuf, traceutil.SpanKindStr(span.Kind()), true)
}
if !contains(p.config.ExcludeDimensions, statusCodeKey) {
concatDimensionValue(p.keyBuf, traceutil.StatusCodeStr(span.Status().Code()), true)
}

for _, d := range optionalDims {
if v, ok := getDimensionValue(d, span.Attributes(), resourceAttrs); ok {
Expand Down
26 changes: 26 additions & 0 deletions connector/spanmetricsconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,32 @@ func TestBuildKeySameServiceNameCharSequence(t *testing.T) {
assert.Equal(t, metrics.Key("a\u0000bc\u0000SPAN_KIND_UNSPECIFIED\u0000STATUS_CODE_UNSET"), k1)
}

func TestBuildKeyExcludeDimensionsAll(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.ExcludeDimensions = []string{"span.kind", "service.name", "span.name", "status.code"}
c, err := newConnector(zaptest.NewLogger(t), cfg, nil)
require.NoError(t, err)

span0 := ptrace.NewSpan()
span0.SetName("spanName")
k0 := c.buildKey("serviceName", span0, nil, pcommon.NewMap())
assert.Equal(t, metrics.Key(""), k0)
}

func TestBuildKeyExcludeWrongDimensions(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.ExcludeDimensions = []string{"span.kind", "service.name.wrong.name", "span.name", "status.code"}
c, err := newConnector(zaptest.NewLogger(t), cfg, nil)
require.NoError(t, err)

span0 := ptrace.NewSpan()
span0.SetName("spanName")
k0 := c.buildKey("serviceName", span0, nil, pcommon.NewMap())
assert.Equal(t, metrics.Key("serviceName"), k0)
}

func TestBuildKeyWithDimensions(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
Expand Down

0 comments on commit 69ac20d

Please sign in to comment.