From 35ec13b83b9ca747198a341e78a7a6eca2cb3c45 Mon Sep 17 00:00:00 2001 From: hadesy <6346047+hadesy@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:56:12 +0800 Subject: [PATCH] [processor/servicegraphprocessor] Add Configurable Database Name Attribute (#30726) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **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 --- .../add-configurable-dbname-attribute.yaml | 27 +++++++++++++++++++ connector/servicegraphconnector/README.md | 2 ++ connector/servicegraphconnector/config.go | 4 +++ .../servicegraphconnector/config_test.go | 5 ++-- connector/servicegraphconnector/connector.go | 8 +++++- .../service-graph-connector-config.yaml | 1 + 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100755 .chloggen/add-configurable-dbname-attribute.yaml diff --git a/.chloggen/add-configurable-dbname-attribute.yaml b/.chloggen/add-configurable-dbname-attribute.yaml new file mode 100755 index 000000000000..9f06b65d2e36 --- /dev/null +++ b/.chloggen/add-configurable-dbname-attribute.yaml @@ -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: [] diff --git a/connector/servicegraphconnector/README.md b/connector/servicegraphconnector/README.md index 79108a7cff8e..191158d63d48 100644 --- a/connector/servicegraphconnector/README.md +++ b/connector/servicegraphconnector/README.md @@ -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 diff --git a/connector/servicegraphconnector/config.go b/connector/servicegraphconnector/config.go index 316ac0eb2cba..5f27ea7444df 100644 --- a/connector/servicegraphconnector/config.go +++ b/connector/servicegraphconnector/config.go @@ -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 { diff --git a/connector/servicegraphconnector/config_test.go b/connector/servicegraphconnector/config_test.go index 62936dae18d2..8357c09535f8 100644 --- a/connector/servicegraphconnector/config_test.go +++ b/connector/servicegraphconnector/config_test.go @@ -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)], ) diff --git a/connector/servicegraphconnector/connector.go b/connector/servicegraphconnector/connector.go index 7b48ae1b5cc9..a8b2818eb565 100644 --- a/connector/servicegraphconnector/connector.go +++ b/connector/servicegraphconnector/connector.go @@ -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 { @@ -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( @@ -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) diff --git a/connector/servicegraphconnector/testdata/service-graph-connector-config.yaml b/connector/servicegraphconnector/testdata/service-graph-connector-config.yaml index 6ba8ca594b45..fa44050cef3b 100644 --- a/connector/servicegraphconnector/testdata/service-graph-connector-config.yaml +++ b/connector/servicegraphconnector/testdata/service-graph-connector-config.yaml @@ -13,6 +13,7 @@ connectors: store: ttl: 1s max_items: 10 + database_name_attribute: db.name service: pipelines: