-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[connector/slowsql] init dir structure for slow sql connector #42680
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
Merged
songy23
merged 47 commits into
open-telemetry:main
from
JaredTan95:slowsqlconector_init
Oct 21, 2025
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
4888670
init dir structure for slow sql connector
JaredTan95 ad610ec
Merge branch 'main' into slowsqlconector_init
JaredTan95 27148ef
remove batch from various examples
JaredTan95 c62df6d
Merge branch 'main' into slowsqlconector_init
JaredTan95 72e66f7
update code owners
JaredTan95 c67da39
Merge branch 'slowsqlconector_init' of https://github.com/JaredTan95/…
JaredTan95 7a10fe7
update code owners
JaredTan95 eb0fe16
update mod version
JaredTan95 bf62594
fix make checkapi
JaredTan95 59dc5cc
make crosslink
JaredTan95 8ce42d4
fix tidylist
JaredTan95 87f2ae8
Merge branch 'main' into slowsqlconector_init
JaredTan95 ddf0c3e
remove
JaredTan95 9300917
remove
JaredTan95 a64d509
Merge branch 'slowsqlconector_init' of https://github.com/JaredTan95/…
JaredTan95 1924947
Update connector/slowsqlconnector/metadata.yaml
JaredTan95 e661158
Merge branch 'main' into slowsqlconector_init
JaredTan95 72c1a59
update mod version
JaredTan95 fed7d06
update gen
JaredTan95 78a237d
fix gocritic
JaredTan95 e34fea4
fix gocritic
JaredTan95 db0934f
fix format
JaredTan95 5cb6797
fix format
JaredTan95 d06f12a
fix format
JaredTan95 df3cd2a
update verions.yaml
JaredTan95 b433b6f
add config_test.go
JaredTan95 6110356
Merge branch 'main' into slowsqlconector_init
JaredTan95 9d6d184
make crosslink
JaredTan95 42b7910
make gotidy
JaredTan95 81dd4ac
Merge branch 'main' into slowsqlconector_init
JaredTan95 22d1431
Update connector/slowsqlconnector/metadata.yaml
JaredTan95 d0f9e80
Merge branch 'main' into slowsqlconector_init
JaredTan95 b0ab4f7
update code owners
JaredTan95 f0ea2ae
make generate
JaredTan95 6c314a5
polish duplicated code
JaredTan95 9601601
Merge branch 'main' into slowsqlconector_init
JaredTan95 6255cd6
fix UT
JaredTan95 0621f60
fix UT
JaredTan95 af49dbd
Merge branch 'main' into slowsqlconector_init
JaredTan95 d217098
Merge branch 'main' into slowsqlconector_init
JaredTan95 bb7f5ca
Merge branch 'main' into slowsqlconector_init
JaredTan95 c0db124
fix db system
JaredTan95 7100b9b
./.github/workflows/scripts/check-collector-module-version.sh
JaredTan95 4da0207
go mod tidy
JaredTan95 d1697dc
Merge branch 'main' into slowsqlconector_init
JaredTan95 57e69a8
Update connector/slowsqlconnector/factory.go
atoulme 705a55a
Merge branch 'main' into slowsqlconector_init
JaredTan95 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| include ../../Makefile.Common |
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,125 @@ | ||
| # SlowSQL Connector | ||
|
|
||
| <!-- status autogenerated section --> | ||
| | Status | | | ||
| | ------------- |-----------| | ||
| | Distributions | [] | | ||
| | Issues | [](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aconnector%2Fslowsql) [](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aconnector%2Fslowsql) | | ||
| | Code coverage | [](https://app.codecov.io/gh/open-telemetry/opentelemetry-collector-contrib/tree/main/?components%5B0%5D=connector_slowsql&displayType=list) | | ||
| | [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@JaredTan95](https://www.github.com/JaredTan95), [@Frapschen](https://www.github.com/Frapschen), [@atoulme](https://www.github.com/atoulme) | | ||
|
|
||
| [development]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#development | ||
|
|
||
| ## Supported Pipeline Types | ||
|
|
||
| | [Exporter Pipeline Type] | [Receiver Pipeline Type] | [Stability Level] | | ||
| | ------------------------ | ------------------------ | ----------------- | | ||
| | traces | logs | [development] | | ||
|
|
||
| [Exporter Pipeline Type]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#exporter-pipeline-type | ||
| [Receiver Pipeline Type]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#receiver-pipeline-type | ||
| [Stability Level]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#stability-levels | ||
| <!-- end autogenerated section --> | ||
|
|
||
| ## Overview | ||
|
|
||
| Generate logs from recorded [slow database statement](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md/) associated with spans. | ||
|
|
||
| Each **log** will have _at least_ the following dimensions: | ||
| - Service name | ||
| - Span kind | ||
| - Span name | ||
| - Status code | ||
| - Trace ID | ||
| - Span ID | ||
| - Database System | ||
| - Database Statement | ||
| - Database Statement Duration | ||
|
|
||
| Each log will additionally have the following attributes: | ||
| - Span attributes. If you want to filter out some attributes (like only copying HTTP attributes starting with `http.`) use the [transform processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/transformprocessor/). | ||
|
|
||
| ## Configurations | ||
|
|
||
| If you are not already familiar with connectors, you may find it helpful to first visit the [Connectors README]. | ||
|
|
||
| The following settings can be optionally configured: | ||
| - `dimensions`: the list of dimensions to add to *logs* with the default dimensions defined above. | ||
| Each additional dimension is defined with a `name` which is looked up in the span's collection of attributes or | ||
| resource attributes (AKA process tags) such as `ip`, `host.name` or `region`. | ||
| - `db_system:` the list value of span attribute `db.system`, Filter specific db systems, define those database's statements need to be collected. ref: https://opentelemetry.io/docs/specs/semconv/attributes-registry/db/ | ||
| - Default: `[h2, mongodb, mssql, mysql, oracle, postgresql, mariadb]` | ||
| - `threshold`: define a threshold and collect when the `db.statement`, namely span duration, larger than this value. | ||
| - Default: `500ms` | ||
|
|
||
| ## Examples | ||
|
|
||
| The following is a simple example usage of the `slow sql` connector. | ||
|
|
||
| ```yaml | ||
| receivers: | ||
| nop: | ||
|
|
||
| exporters: | ||
| nop: | ||
|
|
||
| connectors: | ||
| slowsql: | ||
| threshold: 600ms | ||
| dimensions: | ||
| - name: k8s.namespace.name | ||
| - name: k8s.pod.name | ||
|
|
||
| service: | ||
| pipelines: | ||
| traces: | ||
| receivers: [nop] | ||
| exporters: [slowsql] | ||
| logs: | ||
| receivers: [slowsql] | ||
| exporters: [nop] | ||
| ``` | ||
|
|
||
| The following is a more complex example usage of the `slowsql` connector using Elasticsearch as exporters. | ||
|
|
||
| ```yaml | ||
| receivers: | ||
| otlp: | ||
| protocols: | ||
| grpc: | ||
| http: | ||
|
|
||
| exporters: | ||
| elasticsearch/slow_sql: | ||
| tls: | ||
| insecure: true | ||
| mapping: | ||
| mode: raw | ||
| endpoints: | ||
| - http://localhost:9200 | ||
| user: elastic | ||
| password: elastic | ||
|
|
||
| connectors: | ||
| slowsql: | ||
| threshold: 600ms | ||
| dimensions: | ||
| - name: k8s.namespace.name | ||
| - name: k8s.pod.name | ||
|
|
||
| service: | ||
| pipelines: | ||
| traces: | ||
| receivers: [otl[]] | ||
| exporters: [slowsql] | ||
| logs: | ||
| receivers: [slowsql] | ||
| exporters: [elasticsearch/slow_sql] | ||
| ``` | ||
|
|
||
| The full list of settings exposed for this connector is documented in [slowsqlconnector/config.go](../../connector/slowsqlconnector/config.go). | ||
| ### More Examples | ||
|
|
||
| For more example configuration covering various other use cases, please visit the [testdata directory](../../connector/slowsqlconnector/testdata). | ||
|
|
||
| [Connectors README]:https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md |
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,64 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package slowsqlconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/slowsqlconnector" | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/collector/confmap/xconfmap" | ||
| ) | ||
|
|
||
| // Dimension defines the dimension name and optional default value if the Dimension is missing from a span attribute. | ||
| type Dimension struct { | ||
| Name string `mapstructure:"name"` | ||
| Default *string `mapstructure:"default"` | ||
| // prevent unkeyed literal initialization | ||
| _ struct{} | ||
| } | ||
|
|
||
| // Config defines the configuration options for exceptionsconnector | ||
| type Config struct { | ||
| // Threshold of slow sql. default 500ms. | ||
| Threshold time.Duration `mapstructure:"threshold"` | ||
| // Filter specific db systems, default "h2", "mongodb", "mssql", "mysql", "oracle", "progress", "postgresql", "mariadb", ref: https://opentelemetry.io/docs/specs/semconv/attributes-registry/db/ | ||
| DBSystem []string `mapstructure:"db_system"` | ||
| // 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: | ||
| // https://github.com/open-telemetry/opentelemetry-collector/blob/main/model/semconv/opentelemetry.go. | ||
| Dimensions []Dimension `mapstructure:"dimensions"` | ||
| } | ||
|
|
||
| var _ xconfmap.Validator = (*Config)(nil) | ||
|
|
||
| // Validate checks if the connector configuration is valid | ||
| func (c Config) Validate() error { | ||
| err := validateDimensions(c.Dimensions) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // 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, spanNameKey, statusCodeKey} { | ||
| labelNames[key] = struct{}{} | ||
| } | ||
|
|
||
| for _, key := range dimensions { | ||
| if _, ok := labelNames[key.Name]; ok { | ||
| return fmt.Errorf("duplicate dimension name %q", key.Name) | ||
| } | ||
| labelNames[key.Name] = struct{}{} | ||
| } | ||
|
|
||
| return nil | ||
| } |
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,60 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package slowsqlconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/slowsqlconnector" | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/confmap/confmaptest" | ||
| "go.opentelemetry.io/collector/confmap/xconfmap" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/connector/slowsqlconnector/internal/metadata" | ||
| ) | ||
|
|
||
| func TestLoadConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) | ||
| require.NoError(t, err) | ||
|
|
||
| tests := []struct { | ||
| id component.ID | ||
| expected component.Config | ||
| }{ | ||
| { | ||
| id: component.NewIDWithName(metadata.Type, "default"), | ||
| expected: createDefaultConfig(), | ||
| }, | ||
| { | ||
| id: component.NewIDWithName(metadata.Type, "full"), | ||
| expected: &Config{ | ||
| Threshold: time.Millisecond * 600, | ||
| DBSystem: []string{"h2", "mysql"}, | ||
| Dimensions: []Dimension{ | ||
| {Name: "k8s.namespace.name"}, | ||
| {Name: "k8s.pod.name"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.id.String(), func(t *testing.T) { | ||
| factory := NewFactory() | ||
| cfg := factory.CreateDefaultConfig() | ||
|
|
||
| sub, err := cm.Sub(tt.id.String()) | ||
| require.NoError(t, err) | ||
| err = sub.Unmarshal(cfg) | ||
| assert.NoError(t, err) | ||
| assert.NoError(t, xconfmap.Validate(cfg)) | ||
| assert.Equal(t, tt.expected, cfg) | ||
| }) | ||
| } | ||
| } |
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,45 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package slowsqlconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/slowsqlconnector" | ||
|
|
||
| import ( | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| conventions "go.opentelemetry.io/otel/semconv/v1.27.0" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/internal/pdatautil" | ||
| ) | ||
|
|
||
| const ( | ||
| serviceNameKey = string(conventions.ServiceNameKey) | ||
| dbSystemKey = string(conventions.DBSystemKey) | ||
| statementExecDuration = conventions.DBClientOperationDurationName | ||
| spanKindKey = "span.kind" // OpenTelemetry non-standard constant. | ||
| spanNameKey = "span.name" // OpenTelemetry non-standard constant. | ||
| statusCodeKey = "status.code" // OpenTelemetry non-standard constant. | ||
| dbStatementKey = "db.statement" // OpenTelemetry non-standard constant. | ||
| ) | ||
|
|
||
| func newDimensions(cfgDims []Dimension) []pdatautil.Dimension { | ||
| if len(cfgDims) == 0 { | ||
| return nil | ||
| } | ||
| dims := make([]pdatautil.Dimension, len(cfgDims)) | ||
| for i := range cfgDims { | ||
| dims[i].Name = cfgDims[i].Name | ||
| if cfgDims[i].Default != nil { | ||
| val := pcommon.NewValueStr(*cfgDims[i].Default) | ||
| dims[i].Value = &val | ||
| } | ||
| } | ||
| return dims | ||
| } | ||
|
|
||
| func findAttributeValue(key string, attributes ...pcommon.Map) (string, bool) { | ||
| for _, attr := range attributes { | ||
| if v, ok := attr.Get(key); ok { | ||
| return v.AsString(), true | ||
| } | ||
| } | ||
| return "", false | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.