Skip to content

Commit

Permalink
[processor/servicegraphprocessor] Add Configurable Database Name Attr…
Browse files Browse the repository at this point in the history
…ibute (open-telemetry#30726)

**Description:**
This PR introduces a new configuration option,
'database_name_attribute', in the ServiceGraphProcessor's Config struct.
The purpose of this change is to allow users to specify a custom
attribute name for identifying the database name in span attributes.
This enhancement increases the flexibility of the servicegraphprocessor
and allows for better customization in diverse deployment environments.
The default value is set to 'db.name' to ensure backward compatibility.

**Documentation:**
- Updated the README.md to include the new configuration option, with an
explanation and the default setting.
- Added comments in the code where relevant to clarify the use and
default value of the 'database_name_attribute'.

---------

Signed-off-by: hadesy <6346047+hadesy@users.noreply.github.com>
Co-authored-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
2 people authored and XinRanZhAWS committed Mar 13, 2024
1 parent fbdd5d9 commit 91f1e8d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .chloggen/add-configurable-dbname-attribute.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: servicegraphprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Added a new configuration option `database_name_attribute` to allow users to specify a custom attribute name for identifying the database name in span attributes."

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

# (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: []
2 changes: 2 additions & 0 deletions connector/servicegraphconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ The following settings can be optionally configured:
- Default: `[db.name, net.sock.peer.addr, net.peer.name, rpc.service, net.sock.peer.name, net.peer.name, http.url, http.target]`
- `metrics_flush_interval`: the interval at which metrics are flushed to the exporter.
- Default: Metrics are flushed on every received batch of traces.
- `database_name_attribute`: the attribute name used to identify the database name from span attributes.
- Default: `db.name`

## Example configuration

Expand Down
4 changes: 4 additions & 0 deletions connector/servicegraphconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Config struct {
// MetricsFlushInterval is the interval at which metrics are flushed to the exporter.
// If set to 0, metrics are flushed on every received batch of traces.
MetricsFlushInterval time.Duration `mapstructure:"metrics_flush_interval"`

// DatabaseNameAttribute is the attribute name used to identify the database name from span attributes.
// The default value is db.name.
DatabaseNameAttribute string `mapstructure:"database_name_attribute"`
}

type StoreConfig struct {
Expand Down
5 changes: 3 additions & 2 deletions connector/servicegraphconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func TestLoadConfig(t *testing.T) {
TTL: time.Second,
MaxItems: 10,
},
CacheLoop: time.Minute,
StoreExpirationLoop: 2 * time.Second,
CacheLoop: time.Minute,
StoreExpirationLoop: 2 * time.Second,
DatabaseNameAttribute: "db.name",
},
cfg.Connectors[component.NewID(metadata.Type)],
)
Expand Down
8 changes: 7 additions & 1 deletion connector/servicegraphconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (
defaultPeerAttributes = []string{
semconv.AttributeDBName, semconv.AttributeNetSockPeerAddr, semconv.AttributeNetPeerName, semconv.AttributeRPCService, semconv.AttributeNetSockPeerName, semconv.AttributeNetPeerName, semconv.AttributeHTTPURL, semconv.AttributeHTTPTarget,
}

defaultDatabaseNameAttribute = semconv.AttributeDBName
)

type metricSeries struct {
Expand Down Expand Up @@ -110,6 +112,10 @@ func newConnector(set component.TelemetrySettings, config component.Config) *ser
pConfig.VirtualNodePeerAttributes = defaultPeerAttributes
}

if pConfig.DatabaseNameAttribute == "" {
pConfig.DatabaseNameAttribute = defaultDatabaseNameAttribute
}

meter := metadata.Meter(set)

droppedSpan, _ := meter.Int64Counter(
Expand Down Expand Up @@ -289,7 +295,7 @@ func (p *serviceGraphConnector) aggregateMetrics(ctx context.Context, td ptrace.

// A database request will only have one span, we don't wait for the server
// span but just copy details from the client span
if dbName, ok := findAttributeValue(semconv.AttributeDBName, rAttributes, span.Attributes()); ok {
if dbName, ok := findAttributeValue(p.config.DatabaseNameAttribute, rAttributes, span.Attributes()); ok {
e.ConnectionType = store.Database
e.ServerService = dbName
e.ServerLatencySec = spanDuration(span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ connectors:
store:
ttl: 1s
max_items: 10
database_name_attribute: db.name

service:
pipelines:
Expand Down

0 comments on commit 91f1e8d

Please sign in to comment.