Skip to content

Commit

Permalink
[oracle] Fix empty lock payloads (#27015)
Browse files Browse the repository at this point in the history
  • Loading branch information
nenadnoveljic authored Jun 25, 2024
1 parent 8906c9f commit fd2ef75
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/collector/corechecks/oracle/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/benbjohnson/clock"
)

type vInstance struct {
Expand Down Expand Up @@ -150,6 +151,7 @@ func (c *Check) init() error {

c.fqtEmitted = getFqtEmittedCache()
c.planEmitted = getPlanEmittedCache(c)
c.clock = clock.New()
c.initialized = true

return nil
Expand Down
9 changes: 7 additions & 2 deletions pkg/collector/corechecks/oracle/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -132,11 +131,17 @@ func (c *Check) locks() error {
}
oracleRows = append(oracleRows, p)
}

if len(oracleRows) == 0 {
return nil
}

hname, _ := hostname.Get(context.TODO())
ts := float64(c.clock.Now().UnixMilli())
m := metricsPayload{
Host: c.dbHostname,
Kind: "lock_metrics",
Timestamp: float64(time.Now().UnixMilli()),
Timestamp: ts,
MinCollectionInterval: float64(c.config.MinCollectionInterval),
Tags: c.tags,
AgentVersion: c.agentVersion,
Expand Down
86 changes: 86 additions & 0 deletions pkg/collector/corechecks/oracle/locks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:build oracle_test

package oracle

import (
"context"
"database/sql"
"encoding/json"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/benbjohnson/clock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestLocks(t *testing.T) {
db, dbMock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

c, sender := newDbDoesNotExistCheck(t, "", "")
c.dbVersion = "19.2"
c.db = sqlx.NewDb(db, "sqlmock")
c.Run()

sender.SetupAcceptAll()
sender.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return()

hname, _ := hostname.Get(context.TODO())
m := metricsPayload{
Host: c.dbHostname,
Kind: "lock_metrics",
Timestamp: float64(time.Now().UnixMilli()),
MinCollectionInterval: float64(c.config.MinCollectionInterval),
Tags: c.tags,
AgentVersion: c.agentVersion,
AgentHostname: hname,
OracleVersion: c.dbVersion,
}

dbMock.ExpectQuery("SELECT.*transaction.*").WillReturnError(sql.ErrNoRows)
err = c.locks()
assert.NoError(t, err, "failed to execute locks query")
emptyPayload := lockMetricsPayload{
OracleRows: []oracleLockRow{},
}
emptyPayload.metricsPayload = m
payloadBytes, err := json.Marshal(emptyPayload)
require.NoError(t, err, "failed to marshal lock metrics payload")
sender.AssertNotCalled(t, "EventPlatformEvent", payloadBytes, "dbm-metrics")

rows := sqlmock.NewRows([]string{"SECONDS", "PROGRAM"}).
AddRow(17, "test-program")
dbMock.ExpectQuery("SELECT.*transaction.*").WillReturnRows(rows)
mockClock := clock.NewMock()
mockClock.Set(time.Now())
c.clock = mockClock
ts := float64(c.clock.Now().UnixMilli())
err = c.locks()
assert.NoError(t, err, "failed to execute locks query")
payload := lockMetricsPayload{
OracleRows: []oracleLockRow{
{
SecondsInTransaction: 17,
Program: "test-program",
},
},
}
m.Timestamp = ts
payload.metricsPayload = m
payloadBytes, err = json.Marshal(payload)
require.NoError(t, err, "failed to marshal lock metrics payload")
sender.AssertEventPlatformEvent(t, payloadBytes, "dbm-metrics")
}
2 changes: 2 additions & 0 deletions pkg/collector/corechecks/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
"github.com/DataDog/datadog-agent/pkg/version"
"github.com/benbjohnson/clock"

//nolint:revive // TODO(DBM) Fix revive linter
_ "github.com/godror/godror"
Expand Down Expand Up @@ -113,6 +114,7 @@ type Check struct {
databaseRole string
openMode string
legacyIntegrationCompatibilityMode bool
clock clock.Clock
}

type vDatabase struct {
Expand Down

0 comments on commit fd2ef75

Please sign in to comment.