Skip to content
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

Fix empty log lines parsing in parse_cassandra_log, add automated tes… #1424

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
5 changes: 5 additions & 0 deletions .github/workflows/test_and_build_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ jobs:
git status
exit 1
fi
- name: Run Vector tests
run: |
sudo apt-get install --yes curl bc
curl --proto '=https' --tlsv1.2 -sSfL https://sh.vector.dev | bash -s -- -y
make $VECTOR=~/.vector/bin/vector vector-test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
build_image:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG/CHANGELOG-1.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ When cutting a new release, update the `unreleased` heading to the tag being gen

## unreleased

* [DOCS] [#1469](https://github.com/riptano/mission-control/issues/1469) Add docs for Reaper's Control Plane deployment mode
* [CHANGE] Bump default Medusa version to 0.22.3
* [BUGFIX] [#1409](https://github.com/k8ssandra/k8ssandra-operator/issues/1409) Vector would crash in the Cassandra log parsing if empty lines were present. Add automated tests for Vector parsing rules.
* [DOCS] [#1469](https://github.com/riptano/mission-control/issues/1469) Add docs for Reaper's Control Plane deployment mode
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ else
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(GO_FLAGS) ./apis/... ./pkg/... ./test/yq/... ./controllers/... -covermode=atomic -coverprofile coverage.out
endif

.PHONY: vector-test
vector-test: ## Run vector tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] shouldn't this depend on vector-install?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want it to depend on the vector-install if some person (well, maybe just me?) has it installed through brew or other means. Also, since it was broken on some platforms like MacOS (the fix isn't merged yet), it would make running the tests difficult.

@echo Generating test files for Vector tests
$(eval TMP := $(shell mktemp -d))
VECTOR_TEST_FILES=true OUTPUT_PATH=$(TMP) go test -v ./pkg/telemetry -run=TestGenerateTomlTestFiles
@echo Running vector test files
OUTPUT_PATH=$(TMP) VECTOR=$(VECTOR) scripts/run-vector-tests.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[thought] Couldn't we just run the vector executable (and assert its return value) from within the Go test?
That way it would be a regular test, we wouldn't need a dedicated make target / script file / CI step to run it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that also, but I don't think we've done that too often in our tests. It's an external program, so not really running go test code.

But would we really want Vector installation to be a dependency on running Go tests? make test would require then vector to be always installed.

rm -rf $(TMP)

E2E_TEST_TIMEOUT ?= 3600s

PHONY: e2e-test
Expand Down Expand Up @@ -326,6 +335,7 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint # TODO Add linting to the GHA also
VECTOR ?= $(LOCALBIN)/vector

## Tool Versions
CERT_MANAGER_VERSION ?= v1.12.2
Expand All @@ -346,6 +356,10 @@ cert-manager-multi: ## Install cert-manager to the clusters
make cert-manager; \
done

.PHONY: vector-install
vector-install:
curl --proto '=https' --tlsv1.2 -sSfL https://sh.vector.dev | bash -s -- --prefix $(LOCALBIN) -y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to comment that I'm getting an error on this, but I see that you have opened an issue with the Vector project :)


# Install NGINX in the current Kind cluster using Helm and a values file that is suitable for
# running e2e tests locally with a cluster created with setup-kind-multicluster.sh. Helm must be
# pre-installed on the system.
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ timeout_ms = 10000
Inputs: []string{"systemlog"},
Config: `source = '''
del(.source_type)
.message = string!(.message)
.message = strip_whitespace(.message)
. |= parse_groks!(.message, patterns: [
"%{LOGLEVEL:loglevel}\\s+\\[(?<thread>((.+)))\\]\\s+%{TIMESTAMP_ISO8601:timestamp}\\s+%{JAVACLASS:class}:%{NUMBER:line}\\s+-\\s+(?<message>(.+\\n?)+)",
"%{LOGLEVEL:loglevel}\\s+\\[(?<thread>((.+)))\\]\\s+%{TIMESTAMP_ISO8601:timestamp_raw}\\s+%{JAVACLASS:class}:%{NUMBER:line}\\s+-\\s+(?<message>(.+\\n?)+)",
]
)
.timestamp = parse_timestamp!(.timestamp_raw, format: "%Y-%m-%d %T,%3f")
del(.timestamp_raw)
pod_name, err = get_env_var("POD_NAME")
if err == null {
.pod_name = pod_name
Expand Down
95 changes: 95 additions & 0 deletions pkg/telemetry/vector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package telemetry

import (
"fmt"
"os"
"strings"
"testing"

"github.com/go-logr/logr/testr"
Expand Down Expand Up @@ -366,3 +369,95 @@ func TestOverrideSourcePossible(t *testing.T) {

assert.Equal("stdin", sources[0].Type)
}

func TestGenerateTomlTestFiles(t *testing.T) {
if os.Getenv("VECTOR_TEST_FILES") == "" {
t.Skip("Set VECTOR_TEST_FILES to generate vector test files")
}
outputDir := os.Getenv("OUTPUT_PATH")
if outputDir == "" {
fmt.Printf("No OUTPUT_PATH env variable set")
t.FailNow()
}
assert := assert.New(t)
sources, transformers, sinks := BuildDefaultVectorComponents(vector.VectorConfig{})
assert.Equal(2, len(sources))
assert.Equal(2, len(transformers))
assert.Equal(1, len(sinks))

telemetrySpec := &telemetry.TelemetrySpec{
Vector: &telemetry.VectorSpec{
Enabled: ptr.To[bool](true),
Components: &telemetry.VectorComponentsSpec{
Sources: sources,
Sinks: sinks,
Transforms: transformers,
},
},
}

// Vector components are provided in the Telemetry spec, build the Vector sink config from them
vectorConfigToml := BuildCustomVectorToml(telemetrySpec)

b := strings.Builder{}
fmt.Fprint(&b, vectorConfigToml)

// Append tests

fmt.Fprint(&b, `
[[tests]]
name = "Test parsing normal Cassandra logs"

[[tests.inputs]]
insert_at = "parse_cassandra_log"
value = "WARN [ScheduledTasks:1] 2024-10-01 12:31:17,694 LeaksDetectorImpl.java:306 - LEAK: RandomAccessReader/RandomAccessReader was not released before it was garbage-collected. This resource is a debugging aid, no negative consequences follow from this leak. However, please report this nonetheless even if auto-cleaning succeeded. Auto cleaning result: not attempted, no cleaner."

[[tests.outputs]]
extract_from = "parse_cassandra_log"

[[tests.outputs.conditions]]
type = "vrl"
source = '''
assert_eq!(.loglevel, "WARN")
assert_eq!(.thread, "ScheduledTasks:1")
assert!(is_timestamp(.timestamp))
assert!(is_string(.message))
assert!(is_string(.line))
assert!(exists(.class))
'''
`)

assert.NoError(os.WriteFile(fmt.Sprintf("%s/vector-simple.toml", outputDir), []byte(b.String()), 0644))

b.Reset()
fmt.Fprint(&b, vectorConfigToml)

fmt.Fprint(&b, `
[[tests]]
name = "Test parsing normal Cassandra logs"

[[tests.inputs]]
insert_at = "parse_cassandra_log"
value = '''
WARN [ScheduledTasks:1] 2024-10-01 12:31:17,694 LeaksDetectorImpl.java:306 - LEAK: RandomAccessReader/RandomAccessReader was not released before it was garbage-collected. This resource is a debugging aid, no negative consequences follow from this leak. However, please report this nonetheless even if auto-cleaning succeeded. Auto cleaning result: not attempted, no cleaner."


'''

[[tests.outputs]]
extract_from = "parse_cassandra_log"

[[tests.outputs.conditions]]
type = "vrl"
source = '''
assert_eq!(.loglevel, "WARN")
assert_eq!(.thread, "ScheduledTasks:1")
assert!(is_timestamp(.timestamp))
assert!(is_string(.message))
assert!(is_string(.line))
assert!(exists(.class))
'''
`)

assert.NoError(os.WriteFile(fmt.Sprintf("%s/vector-emptyline.toml", outputDir), []byte(b.String()), 0644))
}
7 changes: 7 additions & 0 deletions scripts/run-vector-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
for i in $OUTPUT_PATH/*; do
$VECTOR test $i
if [ "$?" -ne 0 ]; then
exit 1
fi
done
Loading