-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Common sql comment extractor oracle #48404
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
Open
tharun0064
wants to merge
8
commits into
open-telemetry:main
Choose a base branch
from
newrelic-forks:common-extractor-package-oracle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
801928c
Add SQL comment extraction for APM correlation
tharun0064 d122a45
update package name
tharun0064 01f2c91
Add changelog entry for SQL comment extraction feature
tharun0064 d81e443
Merge branch 'main' into common-extractor-package-oracle
tharun0064 a594d0c
update test files with new attribute
tharun0064 03f9a56
update change log file
tharun0064 13f81e9
update docs
tharun0064 2d12f2a
update the config schema
tharun0064 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| change_type: enhancement | ||
| component: receiver/oracledb | ||
| note: Add SQL comment extraction support for APM correlation. Users can now configure `allowed_comment_keys` to extract key-value pairs from SQL query comments and include them as telemetry attributes for correlation with APM traces. | ||
| issues: [48338] | ||
| change_logs: [user] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package sqlcomments // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/sqlcomments" | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| // leadingBlockCommentRegex matches one or more leading /* */ block comments | ||
| leadingBlockCommentRegex = regexp.MustCompile(`^\s*(/\*.*?\*/\s*)+`) | ||
| // commentContentRegex extracts content between /* and */ delimiters | ||
| commentContentRegex = regexp.MustCompile(`/\*(.*?)\*/`) | ||
| ) | ||
|
|
||
| // ExtractAndFilterComments extracts leading /* */ block comments from SQL, | ||
| // parses them as key=value pairs, and returns only allowed keys. | ||
| // Returns comma-separated filtered pairs, or empty string if no allowed keys found. | ||
| // Format: "key1=value1,key2=value2" | ||
| // | ||
| // This function is designed to be secure by default: | ||
| // - If allowedKeys is empty or nil, returns empty string (no extraction) | ||
| // - Only keys explicitly listed in allowedKeys are included in the result | ||
| // - Duplicate keys use first occurrence only | ||
| // - Malformed pairs (without =) are silently skipped | ||
| // | ||
| // Example: | ||
| // | ||
| // sqlText := "/* key1=value1,key2=value2 */ SELECT * FROM users" | ||
| // allowedKeys := []string{"key1"} | ||
| // result := ExtractAndFilterComments(sqlText, allowedKeys) | ||
| // // result == "key1=value1" | ||
| func ExtractAndFilterComments(sqlText string, allowedKeys []string) string { | ||
| // Early exit: if no allowed keys, return empty immediately (secure by default) | ||
| if len(allowedKeys) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| // Extract leading block comments using regex | ||
| matches := leadingBlockCommentRegex.FindString(sqlText) | ||
| if matches == "" { | ||
| return "" | ||
| } | ||
|
|
||
| // Strip /* and */ delimiters from all comments | ||
| // Match each individual comment block | ||
| commentMatches := commentContentRegex.FindAllStringSubmatch(matches, -1) | ||
| if len(commentMatches) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| // Concatenate all comment contents | ||
| var allComments strings.Builder | ||
| for i, match := range commentMatches { | ||
| if len(match) > 1 { | ||
| if i > 0 { | ||
| allComments.WriteString(",") | ||
| } | ||
| allComments.WriteString(strings.TrimSpace(match[1])) | ||
| } | ||
| } | ||
|
|
||
| commentContent := allComments.String() | ||
| if commentContent == "" { | ||
| return "" | ||
| } | ||
|
|
||
| // Parse key=value pairs and filter by allowed keys | ||
| pairs := strings.Split(commentContent, ",") | ||
| var filteredPairs []string | ||
| seenKeys := make(map[string]bool) | ||
|
|
||
| // Create a set of allowed keys for O(1) lookup | ||
| allowedSet := make(map[string]bool) | ||
| for _, key := range allowedKeys { | ||
| allowedSet[key] = true | ||
| } | ||
|
|
||
| for _, pair := range pairs { | ||
| pair = strings.TrimSpace(pair) | ||
| if pair == "" { | ||
| continue | ||
| } | ||
|
|
||
| // Split by first = only | ||
| parts := strings.SplitN(pair, "=", 2) | ||
| if len(parts) != 2 { | ||
| // Malformed pair, skip it | ||
| continue | ||
| } | ||
|
|
||
| key := strings.TrimSpace(parts[0]) | ||
| value := strings.TrimSpace(parts[1]) | ||
|
|
||
| // Check if key is allowed and not already seen (use first occurrence) | ||
| if allowedSet[key] && !seenKeys[key] { | ||
| seenKeys[key] = true | ||
| filteredPairs = append(filteredPairs, key+"="+value) | ||
| } | ||
| } | ||
|
|
||
| // Join filtered pairs with comma (no space) | ||
| return strings.Join(filteredPairs, ",") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package sqlcomments | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestExtractAndFilterComments(t *testing.T) { | ||
| t.Run("single allowed key", func(t *testing.T) { | ||
| sqlText := "/* nr_service_guid=abc-123 */ SELECT * FROM t" | ||
| allowedKeys := []string{"nr_service_guid"} | ||
| want := "nr_service_guid=abc-123" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("multiple allowed keys", func(t *testing.T) { | ||
| sqlText := "/* nr_service_guid=abc,app_id=xyz */ SELECT * FROM t" | ||
| allowedKeys := []string{"nr_service_guid", "app_id"} | ||
| want := "nr_service_guid=abc,app_id=xyz" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("no matches", func(t *testing.T) { | ||
| sqlText := "/* other=val */ SELECT * FROM t" | ||
| allowedKeys := []string{"nr_service_guid"} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("empty allowlist", func(t *testing.T) { | ||
| sqlText := "/* nr_service_guid=abc */ SELECT * FROM t" | ||
| allowedKeys := []string{} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("nil allowlist", func(t *testing.T) { | ||
| sqlText := "/* nr_service_guid=abc */ SELECT * FROM t" | ||
| var allowedKeys []string | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("multiple comments", func(t *testing.T) { | ||
| sqlText := "/* a=1 */ /* b=2 */ SELECT * FROM t" | ||
| allowedKeys := []string{"a", "b"} | ||
| want := "a=1,b=2" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("not leading comment", func(t *testing.T) { | ||
| sqlText := "SELECT * FROM t /* a=1 */" | ||
| allowedKeys := []string{"a"} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("whitespace before comment", func(t *testing.T) { | ||
| sqlText := " /* nr_service_guid=abc */ SELECT * FROM t" | ||
| allowedKeys := []string{"nr_service_guid"} | ||
| want := "nr_service_guid=abc" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("keys with spaces trimmed", func(t *testing.T) { | ||
| sqlText := "/* key1 = value1 , key2 = value2 */ SELECT * FROM t" | ||
| allowedKeys := []string{"key1", "key2"} | ||
| want := "key1=value1,key2=value2" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("partial match filters correctly", func(t *testing.T) { | ||
| sqlText := "/* allowed=yes,notallowed=no,also_allowed=maybe */ SELECT * FROM t" | ||
| allowedKeys := []string{"allowed", "also_allowed"} | ||
| want := "allowed=yes,also_allowed=maybe" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("malformed pairs skipped", func(t *testing.T) { | ||
| sqlText := "/* valid=1,invalid,another=2 */ SELECT * FROM t" | ||
| allowedKeys := []string{"valid", "invalid", "another"} | ||
| want := "valid=1,another=2" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("empty comment", func(t *testing.T) { | ||
| sqlText := "/**/ SELECT * FROM t" | ||
| allowedKeys := []string{"any"} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("no comments", func(t *testing.T) { | ||
| sqlText := "SELECT * FROM t" | ||
| allowedKeys := []string{"any"} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("unclosed comment", func(t *testing.T) { | ||
| sqlText := "/* unclosed SELECT * FROM t" | ||
| allowedKeys := []string{"any"} | ||
| want := "" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("values with special characters", func(t *testing.T) { | ||
| sqlText := `/* guid=abc-123-def,path=/api/v1/users */ SELECT * FROM t` | ||
| allowedKeys := []string{"guid", "path"} | ||
| want := "guid=abc-123-def,path=/api/v1/users" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("duplicate keys use first", func(t *testing.T) { | ||
| sqlText := "/* key=first,key=second */ SELECT * FROM t" | ||
| allowedKeys := []string{"key"} | ||
| want := "key=first" | ||
|
|
||
| got := ExtractAndFilterComments(sqlText, allowedKeys) | ||
|
|
||
| if got != want { | ||
| t.Errorf("ExtractAndFilterComments() = %q, want %q", got, want) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, i want to understand about the suggested change more please?
The sql comments is designed to float in comma seperated key value pairs in query.comments attr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It goes hand in hand with the other comment.