Skip to content
Merged
Show file tree
Hide file tree
Changes from 45 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 Sep 15, 2025
ad610ec
Merge branch 'main' into slowsqlconector_init
JaredTan95 Sep 15, 2025
27148ef
remove batch from various examples
JaredTan95 Sep 16, 2025
c62df6d
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 1, 2025
72e66f7
update code owners
JaredTan95 Oct 1, 2025
c67da39
Merge branch 'slowsqlconector_init' of https://github.com/JaredTan95/…
JaredTan95 Oct 1, 2025
7a10fe7
update code owners
JaredTan95 Oct 1, 2025
eb0fe16
update mod version
JaredTan95 Oct 1, 2025
bf62594
fix make checkapi
JaredTan95 Oct 1, 2025
59dc5cc
make crosslink
JaredTan95 Oct 1, 2025
8ce42d4
fix tidylist
JaredTan95 Oct 1, 2025
87f2ae8
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 1, 2025
ddf0c3e
remove
JaredTan95 Oct 1, 2025
9300917
remove
JaredTan95 Oct 8, 2025
a64d509
Merge branch 'slowsqlconector_init' of https://github.com/JaredTan95/…
JaredTan95 Oct 8, 2025
1924947
Update connector/slowsqlconnector/metadata.yaml
JaredTan95 Oct 9, 2025
e661158
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 9, 2025
72c1a59
update mod version
JaredTan95 Oct 9, 2025
fed7d06
update gen
JaredTan95 Oct 9, 2025
78a237d
fix gocritic
JaredTan95 Oct 9, 2025
e34fea4
fix gocritic
JaredTan95 Oct 9, 2025
db0934f
fix format
JaredTan95 Oct 9, 2025
5cb6797
fix format
JaredTan95 Oct 9, 2025
d06f12a
fix format
JaredTan95 Oct 9, 2025
df3cd2a
update verions.yaml
JaredTan95 Oct 9, 2025
b433b6f
add config_test.go
JaredTan95 Oct 9, 2025
6110356
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 9, 2025
9d6d184
make crosslink
JaredTan95 Oct 9, 2025
42b7910
make gotidy
JaredTan95 Oct 9, 2025
81dd4ac
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 9, 2025
22d1431
Update connector/slowsqlconnector/metadata.yaml
JaredTan95 Oct 10, 2025
d0f9e80
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 10, 2025
b0ab4f7
update code owners
JaredTan95 Oct 10, 2025
f0ea2ae
make generate
JaredTan95 Oct 10, 2025
6c314a5
polish duplicated code
JaredTan95 Oct 10, 2025
9601601
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 10, 2025
6255cd6
fix UT
JaredTan95 Oct 10, 2025
0621f60
fix UT
JaredTan95 Oct 10, 2025
af49dbd
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 13, 2025
d217098
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 14, 2025
bb7f5ca
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 16, 2025
c0db124
fix db system
JaredTan95 Oct 16, 2025
7100b9b
./.github/workflows/scripts/check-collector-module-version.sh
JaredTan95 Oct 16, 2025
4da0207
go mod tidy
JaredTan95 Oct 16, 2025
d1697dc
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 17, 2025
57e69a8
Update connector/slowsqlconnector/factory.go
atoulme Oct 19, 2025
705a55a
Merge branch 'main' into slowsqlconector_init
JaredTan95 Oct 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ component_management:
name: connector_signaltometrics
paths:
- connector/signaltometricsconnector/**
- component_id: connector_slowsql
name: connector_slowsql
paths:
- connector/slowsqlconnector/**
- component_id: connector_spanmetrics
name: connector_spanmetrics
paths:
Expand Down
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ connector/roundrobinconnector/ @open-telemetry
connector/routingconnector/ @open-telemetry/collector-contrib-approvers @mwear @TylerHelmuth @evan-bradley @edmocosta
connector/servicegraphconnector/ @open-telemetry/collector-contrib-approvers @mapno @JaredTan95
connector/signaltometricsconnector/ @open-telemetry/collector-contrib-approvers @ChrsMark @lahsivjar
connector/slowsqlconnector/ @open-telemetry/collector-contrib-approvers @JaredTan95 @Frapschen @atoulme
connector/spanmetricsconnector/ @open-telemetry/collector-contrib-approvers @portertech @Frapschen @iblancasa
connector/sumconnector/ @open-telemetry/collector-contrib-approvers @greatestusername @shalper2 @crobert-1
exporter/alertmanagerexporter/ @open-telemetry/collector-contrib-approvers @sokoide @mcube8
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/beta_stability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ body:
- connector/routing
- connector/servicegraph
- connector/signaltometrics
- connector/slowsql
- connector/spanmetrics
- connector/sum
- exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ body:
- connector/routing
- connector/servicegraph
- connector/signaltometrics
- connector/slowsql
- connector/spanmetrics
- connector/sum
- exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ body:
- connector/routing
- connector/servicegraph
- connector/signaltometrics
- connector/slowsql
- connector/spanmetrics
- connector/sum
- exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ body:
- connector/routing
- connector/servicegraph
- connector/signaltometrics
- connector/slowsql
- connector/spanmetrics
- connector/sum
- exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ body:
- connector/routing
- connector/servicegraph
- connector/signaltometrics
- connector/slowsql
- connector/spanmetrics
- connector/sum
- exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions .github/component_labels.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ connector/roundrobinconnector connector/roundrobin
connector/routingconnector connector/routing
connector/servicegraphconnector connector/servicegraph
connector/signaltometricsconnector connector/signaltometrics
connector/slowsqlconnector connector/slowsql
connector/spanmetricsconnector connector/spanmetrics
connector/sumconnector connector/sum
exporter/alertmanagerexporter exporter/alertmanager
Expand Down
1 change: 1 addition & 0 deletions connector/slowsqlconnector/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
125 changes: 125 additions & 0 deletions connector/slowsqlconnector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# SlowSQL Connector

<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Distributions | [] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aconnector%2Fslowsql%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aconnector%2Fslowsql) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aconnector%2Fslowsql%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aconnector%2Fslowsql) |
| Code coverage | [![codecov](https://codecov.io/github/open-telemetry/opentelemetry-collector-contrib/graph/main/badge.svg?component=connector_slowsql)](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
64 changes: 64 additions & 0 deletions connector/slowsqlconnector/config.go
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
}
60 changes: 60 additions & 0 deletions connector/slowsqlconnector/config_test.go
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)
})
}
}
45 changes: 45 additions & 0 deletions connector/slowsqlconnector/connector.go
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
}
Loading