Skip to content

Commit

Permalink
feat(operator): remove maven structured logging
Browse files Browse the repository at this point in the history
We just need to capture the log trace, no need to make it more complicated than that.
  • Loading branch information
squakez committed Jan 23, 2025
1 parent 284c089 commit ecea586
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 303 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ build/_offline
build/camel-k-runtime-*-maven-offline.tar.gz
build/_test
build/_maven_output
build/_maven_overlay
build/_kamelets
build/_maven_overlay/
build/maven/target/
build/maven
build/m2
Expand Down
12 changes: 2 additions & 10 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ ARG MAVEN_HOME="/usr/share/maven"
ARG MAVEN_DIST_URL="https://archive.apache.org/dist/maven/maven-3/${MAVEN_DEFAULT_VERSION}/binaries/apache-maven-${MAVEN_DEFAULT_VERSION}-bin.zip"
ARG MVNW_DIR="/usr/share/maven/mvnw/"
ARG MVN_REPO="/etc/maven/m2"
ARG MAVEN_OPTS=""

USER 0

# Maven configuration
RUN mkdir -p ${MAVEN_HOME}
RUN mkdir -p ${MVN_REPO}
COPY build/_maven_overlay/ /usr/share/maven/lib/
ADD build/_maven_overlay/logback.xml /usr/share/maven/conf/

# Prepare the maven wrapper required in the build Pod strategy
COPY build/mvnw/mvnw.tar ${MVNW_DIR}
Expand All @@ -39,13 +36,8 @@ RUN tar -xC ${MVNW_DIR} -f ${MVNW_DIR}mvnw.tar \
&& sed -i "s;distributionUrl=.*;distributionUrl=${MAVEN_DIST_URL};" ${MVNW_DIR}.mvn/wrapper/maven-wrapper.properties
# Used by mvnw to download maven dist into it
ENV MAVEN_USER_HOME="${MAVEN_HOME}"
# Install a default mvnw distribution at build time and prepare the config for formatting log
RUN ${MVNW_DIR}/mvnw --version | grep "Maven home:" | sed 's/Maven home: //' >> ${MVNW_DIR}default \
&& cp -r /usr/share/maven/lib/. $(cat ${MVNW_DIR}default)/lib \
&& rm $(cat ${MVNW_DIR}default)/lib/maven-slf4j-provider* \
&& rm $(cat ${MVNW_DIR}default)/lib/slf4j-api-1.*

ENV MAVEN_OPTS="${MAVEN_OPTS} -Dlogback.configurationFile=${MAVEN_HOME}/conf/logback.xml"
# Install a default mvnw distribution at build time
RUN ${MVNW_DIR}/mvnw --version

ADD build/_maven_output ${MVN_REPO}
# Fix https://github.com/moby/moby/issues/37965
Expand Down
1 change: 0 additions & 1 deletion e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ func Logs(t *testing.T, ctx context.Context, ns, podName string, options corev1.
}

func StructuredLogs(t *testing.T, ctx context.Context, ns, podName string, options *corev1.PodLogOptions, ignoreParseErrors bool) ([]util.LogEntry, error) {

stream, err := TestClient(t).CoreV1().Pods(ns).GetLogs(podName, options).Stream(ctx)
if err != nil {
msg := "Error while reading container logs"
Expand Down
Binary file removed java/.mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 0 additions & 18 deletions java/.mvn/wrapper/maven-wrapper.properties

This file was deleted.

116 changes: 0 additions & 116 deletions java/maven-logging/pom.xml

This file was deleted.

67 changes: 0 additions & 67 deletions java/maven-logging/src/main/resources/logback.xml

This file was deleted.

75 changes: 31 additions & 44 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,68 @@ limitations under the License.
package maven

import (
"encoding/json"
"strings"
"regexp"

"github.com/apache/camel-k/v2/pkg/util/log"
)

type mavenLog struct {
Level string `json:"level"`
TS string `json:"ts"`
Logger string `json:"logger"`
Msg string `json:"msg"`
Class string `json:"class"`
CallerMethodName string `json:"caller_method_name"`
CallerFileName string `json:"caller_file_name"`
CallerLineNumber int `json:"caller_line_number"`
Thread string `json:"thread"`
Level string `json:"level"`
Msg string `json:"msg"`
}

const (
TRACE = "TRACE"
DEBUG = "DEBUG"
INFO = "INFO"
WARN = "WARN"
ERROR = "ERROR"
FATAL = "FATAL"
TRACE = "TRACE"
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
FATAL = "FATAL"
)

var mavenLogger = log.WithName("maven.build")
var mavenLoggingFormat = regexp.MustCompile(`^\[(TRACE|DEBUG|INFO|WARNING|ERROR|FATAL)\] (.*)$`)

// LogHandler is in charge to log the text passed and, if the trace is an error, to return the message to the caller.
func LogHandler(s string) string {
l, parseError := parseLog(s)
if parseError == nil {
normalizeLog(l)
} else {
// Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur.
// For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin,
// etc). The build may still have succeeded, though.
nonNormalizedLog(s)
}
l := parseLog(s)
normalizeLog(l)

// Return the error message according to maven log
if strings.HasPrefix(s, "[ERROR]") {
return s
if l.Level == ERROR {
return l.Msg
}

return ""
}

func parseLog(line string) (mavenLog, error) {
func parseLog(line string) mavenLog {
var l mavenLog

err := json.Unmarshal([]byte(line), &l)
if err != nil {
return l, err
matches := mavenLoggingFormat.FindAllStringSubmatch(line, -1)
if len(matches) == 0 || len(matches[0]) != 3 {
// If this is happening, then, we have a problem with parsing the maven output
// however we are printing the output in its plain format
l = mavenLog{
Level: INFO,
Msg: line,
}
} else {
l = mavenLog{
Level: matches[0][1],
Msg: matches[0][2],
}
}

return l, nil
return l
}

func normalizeLog(mavenLog mavenLog) {
switch mavenLog.Level {
case DEBUG, TRACE:
mavenLogger.Debug(mavenLog.Msg)
case INFO, WARN:
case INFO, WARNING:
mavenLogger.Info(mavenLog.Msg)
case ERROR, FATAL:
mavenLogger.Error(nil, mavenLog.Msg)
}
}

func nonNormalizedLog(rawLog string) {
// Distinguish an error message from the rest
if strings.HasPrefix(rawLog, "[ERROR]") {
mavenLogger.Error(nil, rawLog)
} else {
mavenLogger.Info(rawLog)
}
}
21 changes: 20 additions & 1 deletion pkg/util/maven/maven_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/apache/camel-k/v2/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -37,5 +38,23 @@ func TestRunAndLogErrorMvn(t *testing.T) {
err := util.RunAndLog(context.Background(), cmd, LogHandler, LogHandler)

require.Error(t, err)
require.ErrorContains(t, err, "[ERROR] The goal you specified requires a project to execute but there is no POM in this directory")
require.ErrorContains(t, err, "The goal you specified requires a project to execute but there is no POM in this directory")
}

func TestParseLog(t *testing.T) {
mavenLogLine := parseLog("[INFO] this is an info log trace")
assert.Equal(t, INFO, mavenLogLine.Level)
assert.Equal(t, "this is an info log trace", mavenLogLine.Msg)
}

func TestParseErrorLog(t *testing.T) {
mavenLogLine := parseLog("[ERROR] this is an error log trace")
assert.Equal(t, ERROR, mavenLogLine.Level)
assert.Equal(t, "this is an error log trace", mavenLogLine.Msg)
}

func TestParseLogCannotTrace(t *testing.T) {
mavenLogLine := parseLog("[FAILING] this is a failing log trace")
assert.Equal(t, INFO, mavenLogLine.Level)
assert.Equal(t, "[FAILING] this is a failing log trace", mavenLogLine.Msg)
}
Loading

0 comments on commit ecea586

Please sign in to comment.