Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[connector/exceptions] Always add span name as a dimension in the output metrics and log records #32211

Merged
27 changes: 27 additions & 0 deletions .chloggen/always-add-span-name-dimension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: exceptionsconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Make span name a default dimension for ouput metrics and log records.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32162]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions connector/exceptionsconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Generate metrics and logs from recorded [application exceptions](https://github.

Each **metric** and **log** will have _at least_ the following dimensions:
- Service name
- Span name
- Span kind
- Status code

Expand Down
3 changes: 2 additions & 1 deletion connector/exceptionsconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Dimension struct {
type Config struct {
// Dimensions defines the list of additional dimensions on top of the provided:
// - service.name
// - span.name
// - span.kind
// - status.code
// The dimensions will be fetched from the span's attributes. Examples of some conventionally used attributes:
Expand All @@ -40,7 +41,7 @@ func (c Config) Validate() error {
// validateDimensions checks duplicates for reserved dimensions and additional dimensions.
func validateDimensions(dimensions []Dimension) error {
labelNames := make(map[string]struct{})
for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey} {
for _, key := range []string{serviceNameKey, spanKindKey, spanNameKey, statusCodeKey} {
labelNames[key] = struct{}{}
}

Expand Down
1 change: 1 addition & 0 deletions connector/exceptionsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
exceptionStacktraceKey = conventions.AttributeExceptionStacktrace
// TODO(marctc): formalize these constants in the OpenTelemetry specification.
spanKindKey = "span.kind" // OpenTelemetry non-standard constant.
spanNameKey = "span.name" // OpenTelemetry non-standard constant.
statusCodeKey = "status.code" // OpenTelemetry non-standard constant.
eventNameExc = "exception" // OpenTelemetry non-standard constant.
)
Expand Down
1 change: 1 addition & 0 deletions connector/exceptionsconnector/connector_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (c *logsConnector) attrToLogRecord(sl plog.ScopeLogs, serviceName string, s
spanAttrs.CopyTo(logRecord.Attributes())

// Add common attributes to the log record.
logRecord.Attributes().PutStr(spanNameKey, span.Name())
logRecord.Attributes().PutStr(spanKindKey, traceutil.SpanKindStr(span.Kind()))
logRecord.Attributes().PutStr(statusCodeKey, traceutil.StatusCodeStr(span.Status().Code()))
logRecord.Attributes().PutStr(serviceNameKey, serviceName)
Expand Down
1 change: 1 addition & 0 deletions connector/exceptionsconnector/connector_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.S
dims := pcommon.NewMap()
dims.EnsureCapacity(3 + len(dimensions))
dims.PutStr(serviceNameKey, serviceName)
dims.PutStr(spanNameKey, span.Name())
dims.PutStr(spanKindKey, traceutil.SpanKindStr(span.Kind()))
dims.PutStr(statusCodeKey, traceutil.StatusCodeStr(span.Status().Code()))
for _, d := range dimensions {
Expand Down
3 changes: 3 additions & 0 deletions connector/exceptionsconnector/connector_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// metricID represents the minimum attributes that uniquely identifies a metric in our tests.
type metricID struct {
service string
spanName string
kind string
statusCode string
}
Expand Down Expand Up @@ -196,6 +197,8 @@ func verifyMetricLabels(dp metricDataPoint, t testing.TB, seenMetricIDs map[metr
switch k {
case serviceNameKey:
mID.service = v.Str()
case spanNameKey:
mID.spanName = v.Str()
case spanKindKey:
mID.kind = v.Str()
case statusCodeKey:
Expand Down
5 changes: 5 additions & 0 deletions connector/exceptionsconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type serviceSpans struct {
}

type span struct {
name string
kind ptrace.SpanKind
statusCode ptrace.StatusCode
}
Expand All @@ -49,10 +50,12 @@ func buildSampleTrace() ptrace.Traces {
serviceName: "service-a",
spans: []span{
{
name: "svc-a-ep1",
kind: ptrace.SpanKindServer,
statusCode: ptrace.StatusCodeError,
},
{
name: "svc-a-ep2",
kind: ptrace.SpanKindClient,
statusCode: ptrace.StatusCodeError,
},
Expand All @@ -63,6 +66,7 @@ func buildSampleTrace() ptrace.Traces {
serviceName: "service-b",
spans: []span{
{
name: "svc-b-ep1",
kind: ptrace.SpanKindServer,
statusCode: ptrace.StatusCodeError,
},
Expand All @@ -85,6 +89,7 @@ func initServiceSpans(serviceSpans serviceSpans, spans ptrace.ResourceSpans) {

func initSpan(span span, s ptrace.Span) {
s.SetKind(span.kind)
s.SetName(span.name)
s.Status().SetCode(span.statusCode)
now := time.Now()
s.SetStartTimestamp(pcommon.NewTimestampFromTime(now))
Expand Down
1 change: 1 addition & 0 deletions connector/exceptionsconnector/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exceptions/full:
# - span.name
# - span.kind
# - status.code
# - exception.stacktrace (Only for log records)
dimensions:
- name: exception.type
- name: exception.message
9 changes: 9 additions & 0 deletions connector/exceptionsconnector/testdata/logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ resourceLogs:
- key: arrayAttrName
value:
arrayValue: {}
- key: span.name
value:
stringValue: svc-a-ep1
- key: span.kind
value:
stringValue: SPAN_KIND_SERVER
Expand Down Expand Up @@ -67,6 +70,9 @@ resourceLogs:
- key: arrayAttrName
value:
arrayValue: {}
- key: span.name
value:
stringValue: svc-a-ep2
- key: span.kind
value:
stringValue: SPAN_KIND_CLIENT
Expand Down Expand Up @@ -115,6 +121,9 @@ resourceLogs:
- key: arrayAttrName
value:
arrayValue: {}
- key: span.name
value:
stringValue: svc-b-ep1
- key: span.kind
value:
stringValue: SPAN_KIND_SERVER
Expand Down
Loading