Skip to content

Commit

Permalink
[Oracle Module] Change tablespace metricset collection period (#31259)
Browse files Browse the repository at this point in the history
* Add a check to verify the collection period and unit tests

* Resolve lint issues

* Update period in config and run make update

* Auto-generate oracle.asciidoc

* Resolve lint issues

* Update the configuration to separate the period for metricsets

* Add CHANGELOG.next.asciidoc entry

* Move the changelog entry to added
  • Loading branch information
yug-rajani authored Apr 15, 2022
1 parent e1a7f6d commit 3b2719c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Populate new container ECS fields in Docker module. {pull}30399[30399]
- Populate new container ECS fields in Kubernetes module. {pull}30181[30181]
- Populate ecs container fields in Containerd module. {pull}31025[31025]
- Enhance Oracle Module: Change tablespace metricset collection period {issue}30948[30948] {pull}31259[#31259]

*Packetbeat*

Expand Down
9 changes: 8 additions & 1 deletion metricbeat/docs/modules/oracle.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,16 @@ in <<configuration-metricbeat>>. Here is an example configuration:
----
metricbeat.modules:
- module: oracle
metricsets: ["tablespace", "performance"]
period: 10m
metricsets:
- tablespace
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]
- module: oracle
period: 10s
metricsets:
- performance
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]
# username: ""
Expand Down
9 changes: 8 additions & 1 deletion x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,16 @@ metricbeat.modules:

#-------------------------------- Oracle Module --------------------------------
- module: oracle
metricsets: ["tablespace", "performance"]
period: 10m
metricsets:
- tablespace
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]
- module: oracle
period: 10s
metricsets:
- performance
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]

# username: ""
Expand Down
9 changes: 8 additions & 1 deletion x-pack/metricbeat/module/oracle/_meta/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
- module: oracle
metricsets: ["tablespace", "performance"]
period: 10m
metricsets:
- tablespace
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]
- module: oracle
period: 10s
metricsets:
- performance
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]

# username: ""
Expand Down
21 changes: 19 additions & 2 deletions x-pack/metricbeat/module/oracle/tablespace/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package tablespace

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand All @@ -28,7 +28,7 @@ func TestEventMapping(t *testing.T) {
events, err := m.extractAndTransform(context.Background())
assert.NoError(t, err)

fmt.Printf("Total %d events\n", len(events))
t.Logf("Total %d events\n", len(events))

t.Run("Happy Path", func(t *testing.T) {
for _, event := range events {
Expand Down Expand Up @@ -81,3 +81,20 @@ func TestEventMapping(t *testing.T) {
})
})
}

func TestPeriod(t *testing.T) {
t.Run("Check lower period", func(t *testing.T) {
var printWarning = CheckCollectionPeriod(time.Second * 59)
assert.True(t, printWarning, "Warning expected.")
})

t.Run("Check period", func(t *testing.T) {
var printWarning = CheckCollectionPeriod(time.Minute * 10)
assert.False(t, printWarning, "Warning not expected.")
})

t.Run("Check higher period", func(t *testing.T) {
var printWarning = CheckCollectionPeriod(time.Minute * 11)
assert.False(t, printWarning, "Warning not expected.")
})
}
22 changes: 16 additions & 6 deletions x-pack/metricbeat/module/oracle/tablespace/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package tablespace

import (
"context"

"github.com/pkg/errors"
"fmt"
"time"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle"
Expand Down Expand Up @@ -37,7 +37,12 @@ type MetricSet struct {
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config := oracle.ConnectionDetails{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, errors.Wrap(err, "error parsing config file")
return nil, fmt.Errorf("error parsing config file: %w", err)
}

// Warn the user if the collection period value is less than 1 minute.
if CheckCollectionPeriod(base.Module().Config().Period) {
base.Logger().Warn("The current value of period is significantly low and might waste cycles and resources. Please set the period value to at least 1 minute or more.")
}

return &MetricSet{
Expand All @@ -46,26 +51,31 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}, nil
}

// CheckCollectionPeriod method returns true if the period is less than 1 minute.
func CheckCollectionPeriod(period time.Duration) bool {
return period < time.Minute
}

// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) (err error) {
db, err := oracle.NewConnection(&m.connectionDetails)
if err != nil {
return errors.Wrap(err, "error creating connection to Oracle")
return fmt.Errorf("error creating connection to Oracle: %w", err)
}
defer db.Close()

m.extractor = &tablespaceExtractor{db: db}

events, err := m.extractAndTransform(ctx)
if err != nil {
return errors.Wrap(err, "error getting or interpreting data from Oracle")
return fmt.Errorf("error getting or interpreting data from Oracle: %w", err)
}

m.Load(ctx, events, reporter)

return
return err
}

//Load is the L of an ETL. In this case, takes the events and sends them to Elasticseach
Expand Down
9 changes: 8 additions & 1 deletion x-pack/metricbeat/modules.d/oracle.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
# Docs: https://www.elastic.co/guide/en/beats/metricbeat/main/metricbeat-module-oracle.html

- module: oracle
metricsets: ["tablespace", "performance"]
period: 10m
metricsets:
- tablespace
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]
- module: oracle
period: 10s
metricsets:
- performance
enabled: true
hosts: ["user/pass@0.0.0.0:1521/ORCLPDB1.localdomain"]

# username: ""
Expand Down

0 comments on commit 3b2719c

Please sign in to comment.