Skip to content

Commit

Permalink
feat: pass the interval though env var to integrations (newrelic#511)
Browse files Browse the repository at this point in the history
* feat: pass the interval though env var to integrations

* test: check default and 0 intervals

* test: add test to cmd channnel
  • Loading branch information
gsanchezgavier authored Jun 4, 2021
1 parent 4bc5560 commit 09ad913
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package runintegration
import (
"context"
"testing"
"time"

"github.com/newrelic/infrastructure-agent/internal/agent/cmdchannel"
"github.com/newrelic/infrastructure-agent/internal/integrations/v4/integration"
Expand Down Expand Up @@ -52,6 +53,7 @@ func TestHandle_queuesIntegrationToBeRun(t *testing.T) {

d := <-defQueue
assert.Equal(t, "nri-process-discovery", d.Name)
assert.Equal(t, time.Duration(0), d.Interval)
// Definition won't allow assert further
}

Expand Down
8 changes: 7 additions & 1 deletion internal/integrations/v4/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
defaultIntegrationInterval = config.FREQ_PLUGIN_EXTERNAL_PLUGINS * time.Second
defaultTimeout = 120 * time.Second
minimumTimeout = 100 * time.Millisecond
intervalEnvVarName = "NRI_CONFIG_INTERVAL"
)

var minimumIntegrationIntervalOverride = ""
Expand Down Expand Up @@ -61,6 +62,11 @@ func newDefinitionWithoutLookup(ce config2.ConfigEntry, passthroughEnv []string,
if err := ce.Sanitize(); err != nil {
return Definition{}, err
}

interval := getInterval(ce.Interval)
// Reading this env the integration can know configured interval.
ce.Env[intervalEnvVarName] = fmt.Sprintf("%v", interval)

d := Definition{
ExecutorConfig: executor.Config{
User: ce.User,
Expand All @@ -70,7 +76,7 @@ func newDefinitionWithoutLookup(ce config2.ConfigEntry, passthroughEnv []string,
},
Labels: ce.Labels,
Name: ce.InstanceName,
Interval: getInterval(ce.Interval),
Interval: interval,
WhenConditions: conditions(ce.When),
ConfigTemplate: configTemplate,
newTempFile: newTempFile,
Expand Down
28 changes: 28 additions & 0 deletions internal/integrations/v4/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package integration

import (
"context"
"fmt"
"io/ioutil"
"runtime"
"testing"
Expand Down Expand Up @@ -163,6 +164,33 @@ func TestTimeout_Default(t *testing.T) {
// THEN the integration has a default timeout
assert.Equal(t, defaultTimeout, i.Timeout)
}
func TestInterval_EnvironmentVariableCustom(t *testing.T) {
// GIVEN a configuration with custom interval
// WHEN an integration is loaded from it
i, err := NewDefinition(config2.ConfigEntry{InstanceName: "foo", Interval: "55s", Exec: config2.ShlexOpt{"bar"}}, ErrLookup, nil, nil)
require.NoError(t, err)

// THEN the integration has an environment variable with the interval value
assert.Equal(t, "55s", i.ExecutorConfig.Environment[intervalEnvVarName])
}
func TestInterval_EnvironmentVariableCero(t *testing.T) {
// GIVEN a configuration with custom interval
// WHEN an integration is loaded from it
i, err := NewDefinition(config2.ConfigEntry{InstanceName: "foo", Interval: "0", Exec: config2.ShlexOpt{"bar"}}, ErrLookup, nil, nil)
require.NoError(t, err)

// THEN the integration has an environment variable with the interval value
assert.Equal(t, "0s", i.ExecutorConfig.Environment[intervalEnvVarName])
}
func TestInterval_EnvironmentVariableDefault(t *testing.T) {
// GIVEN a configuration with custom interval
// WHEN an integration is loaded from it
i, err := NewDefinition(config2.ConfigEntry{InstanceName: "foo", Exec: config2.ShlexOpt{"bar"}}, ErrLookup, nil, nil)
require.NoError(t, err)

// THEN the integration has an environment variable with the interval value
assert.Equal(t, fmt.Sprintf("%v", defaultIntegrationInterval), i.ExecutorConfig.Environment[intervalEnvVarName])
}

func TestTimeout_TooLow(t *testing.T) {
// GIVEN a configured timeout where the user forgot to write a suffix
Expand Down

0 comments on commit 09ad913

Please sign in to comment.