diff options
Diffstat (limited to 'reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis')
4 files changed, 534 insertions, 0 deletions
diff --git a/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphAnalyzer.java b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphAnalyzer.java new file mode 100644 index 0000000..208b3e1 --- /dev/null +++ b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphAnalyzer.java @@ -0,0 +1,114 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.logging + * ================================================================================ + * Copyright © 2018 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.ref.slf4j.analysis; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; + +/** + * Crude analyzer for log messages, to build a simple + * representation of the call graph. + */ +public class CallGraphAnalyzer { + + /** Messages of interest. */ + private List<LogEntry> mEntries = new ArrayList<>(); + + /** + * Capture entry if it's interesting. + * @param entry candidate. + * @return this. + */ + public CallGraphAnalyzer add(final LogEntry entry) { + + if (entry.getLogger().contains("ONAPLogAdapterTest")) { + return this; + } + + if (StringUtils.isNotBlank(entry.getMarkers())) { + this.mEntries.add(entry); + } + + return this; + } + + /** + * Get all captured entries, for diagnostics only. + * @return entries. + */ + public List<LogEntry> getEntries() { + return this.mEntries; + } + + /** + * Find the entry point into the call graph through the various components. + * @return entry point or (failure) null. + */ + public LogEntry findEntryPoint() { + for (final LogEntry e : this.mEntries) { + if (e.getLogger().endsWith("ComponentAlpha")) { + if ("ENTRY".equals(e.getMarkers())) { + if (StringUtils.isBlank(e.getPartnerName())) { + return e; + } + } + } + } + return null; + } + + /** + * Find entries for where a component invokes others. + * @param parent parent ENTRY (not actually the entry where it's doing the invoking). + * @return components invoked by this one. + */ + public List<LogEntry> findInvokes(final LogEntry parent) { + final List<LogEntry> invokes = new ArrayList<>(); + for (final LogEntry e : this.mEntries) { + if (StringUtils.equals(parent.getInvocationID(), e.getInvocationID())) { + final String invokingID = e.getInvokingID(); + if (StringUtils.isNotBlank(invokingID)) { + invokes.add(e); + } + } + } + return invokes; + } + + /** + * Find a specific invocation. + * @param invoke invocation record. + * @return invocation ENTRY, or (failure) null if not found. + */ + public LogEntry findInvocation(final LogEntry invoke) { + for (final LogEntry e : this.mEntries) { + if ("ENTRY".equals(e.getMarkers())) { + if (StringUtils.equals(invoke.getInvokingID(), e.getInvocationID())) { + return e; + } + } + } + return null; + } +} diff --git a/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphReportWriter.java b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphReportWriter.java new file mode 100644 index 0000000..ee67b35 --- /dev/null +++ b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/CallGraphReportWriter.java @@ -0,0 +1,111 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.logging + * ================================================================================ + * Copyright © 2018 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.ref.slf4j.analysis; + +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.testng.Assert; + +/** + * A simple, recursive text-only report writer for the call graph. + */ +public class CallGraphReportWriter { + + /** The analyzer which does the work. */ + final CallGraphAnalyzer mAnalyzer; + + /** Short report, for validation. */ + final StringBuilder mShortReport = new StringBuilder(); + + /** Longer report, for human eyes. */ + final StringBuilder mLongReport = new StringBuilder(); + + /** + * Construct writer. + * @param analyzer initialized analyzer. + */ + public CallGraphReportWriter(final CallGraphAnalyzer analyzer) { + + this.mAnalyzer = analyzer; + + Assert.assertTrue(analyzer.getEntries().size() > 0); + final LogEntry e0 = analyzer.findEntryPoint(); + Assert.assertNotNull(e0); + + this.mLongReport.append(e0.toShortString()).append("\n"); + this.mShortReport.append(StringUtils.substringAfter(e0.getLogger(), ".Component")).append("\n"); + + this.report(e0, 1); + + } + + /** + * Recursively analyze. + * @param invoker entry point. + * @param depth recursive depth, for handbrake. + */ + private void report(final LogEntry invoker, final int depth) { + + if (depth > 100) { + throw new AssertionError("Recursion ad infinitum"); + } + + final List<LogEntry> invokes0 = this.mAnalyzer.findInvokes(invoker); + for (final LogEntry invoke0 : invokes0) { + + final LogEntry invoked0 = this.mAnalyzer.findInvocation(invoke0); + + Assert.assertNotNull(invoked0); + + final String indent = StringUtils.repeat(' ', depth * 4); + this.mLongReport.append(indent).append(invoked0.toShortString()).append('\n'); + this.mShortReport.append(indent).append(StringUtils.substringAfter(invoked0.getLogger(), ".Component")).append('\n'); + + report(invoked0, depth + 1); + } + } + + /** + * Get report. + * @return short report, for validation. + */ + public String getShortReport() { + return this.mShortReport.toString(); + } + + /** + * Get report. + * @return long report, for printing out. + */ + public String getLongReport() { + return this.mLongReport.toString(); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return this.getLongReport(); + } +} diff --git a/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntry.java b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntry.java new file mode 100644 index 0000000..b9bd48f --- /dev/null +++ b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntry.java @@ -0,0 +1,238 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.logging + * ================================================================================ + * Copyright © 2018 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.ref.slf4j.analysis; + +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.bind.DatatypeConverter; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.slf4j.event.Level; + +/** + * Test class for reading a logentry during analysis. + */ +public class LogEntry { + + /** Property. */ + private final Date mTimestamp; + + /** Property. */ + private final String mThread; + + /** Property. */ + private final Level mLevel; + + /** Property. */ + private final String mLogger; + + /** Property. */ + private final String mMessage; + + /** Property. */ + private final String mException; + + /** Property. */ + private final Map<String, String> mMDCs; + + /** Property. */ + private final String mMarkers; + + /** + * Construct from log line. + * @param line to be parsed. + */ + public LogEntry(final String line) { + + final String [] tokens = line.split("\t", -1); + if (tokens.length < 8) { + throw new IllegalArgumentException("Unsupported line (expected 8+ tokens, got " + + tokens.length + "): " + line); + } + + int index = 0; + + this.mTimestamp = DatatypeConverter.parseDateTime(tokens[index++]).getTime(); + this.mThread = tokens[index++]; + this.mLevel = Level.valueOf(tokens[index++].trim()); + this.mLogger = tokens[index++]; + + this.mMDCs = parseMDCs(tokens[index++]); + this.mMessage = tokens[index++]; + this.mException = tokens[index++]; + this.mMarkers = tokens[index++]; + } + + /** + * Parse serialized MDCs. + * @param mdc serialized DMC map. + * @return parsed. + */ + static Map<String, String> parseMDCs(final String mdc) { + + final Map<String, String> mdcs = new HashMap<>(); + for (final String token : mdc.split(",")) { + final String[] mdcTokens = token.split("="); + if (mdcTokens.length == 2) { + mdcs.put(StringUtils.trim(mdcTokens[0]), StringUtils.trim(mdcTokens[1])); + } + } + return Collections.unmodifiableMap(mdcs); + } + + /** + * Getter. + * @return property. + */ + public Date getTimestamp() { + return this.mTimestamp; + } + + /** + * Getter. + * @return property. + */ + public String getThread() { + return this.mThread; + } + + /** + * Getter. + * @return property. + */ + public Level getLevel() { + return this.mLevel; + } + + /** + * Getter. + * @return property. + */ + public String getLogger() { + return this.mLogger; + } + + /** + * Getter. + * @return property. + */ + public String getMessage() { + return this.mMessage; + } + + /** + * Getter. + * @return property. + */ + public String getException() { + return this.mException; + } + + /** + * Getter. + * @return property. + */ + public Map<String, String> getMDCs() { + return this.mMDCs; + } + + /** + * Getter. + * @return property. + */ + public String getMarkers() { + return this.mMarkers; + } + + /** + * Getter. + * @return property. + */ + public String getRequestID() { + return this.getMDCs().get("RequestID"); + } + + /** + * Getter. + * @return property. + */ + public String getInvocationID() { + return this.getMDCs().get("InvocationID"); + } + + /** + * Getter. + * @return property. + */ + public String getPartnerName() { + return this.getMDCs().get("PartnerName"); + } + + /** + * Getter. + * @return property. + */ + public String getInvokingID() { + if (StringUtils.defaultString(this.getMarkers()).startsWith("INVOKE")) { + return this.getMessage(); + } + return null; + } + + /** + * Getter. + * @return property. + */ + public String toShortString() { + final StringBuilder buf = new StringBuilder(); + buf.append("LogEntry(markers=").append(StringUtils.defaultString(this.getMarkers())); + buf.append(", logger=").append(this.getLogger().substring(1 + this.getLogger().lastIndexOf("."))); + if (StringUtils.isNotBlank(this.getRequestID())) { + buf.append(", requestID=[...]").append(StringUtils.right(this.getRequestID(), 8)); + } + if (StringUtils.isNotBlank(this.getInvocationID())) { + buf.append(", invocationID=[...]").append(StringUtils.right(this.getInvocationID(), 8)); + } + if (StringUtils.isNotBlank(this.getInvokingID())) { + buf.append(", invokingID=[...]").append(StringUtils.right(this.getInvokingID(), 8)); + } + + final Calendar c = Calendar.getInstance(); + c.setTime(this.getTimestamp()); + + buf.append(", timestamp=").append(DatatypeConverter.printDateTime(c)); + return buf.append(")").toString(); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntryTest.java b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntryTest.java new file mode 100644 index 0000000..63ead27 --- /dev/null +++ b/reference/logging-slf4j-demo/src/test/java/org/onap/logging/ref/slf4j/analysis/LogEntryTest.java @@ -0,0 +1,71 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.logging + * ================================================================================ + * Copyright © 2018 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.logging.ref.slf4j.analysis; + +import java.util.Map; + +import org.slf4j.event.Level; +import org.testng.annotations.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; + +public class LogEntryTest { + + @Test + public void testLogEntry() { + + final String eg = "2018-05-07T16:45:53.056Z\tpool-1-thread-1\tINFO" + + "\torg.onap.logging.ref.slf4j.component.gamma.ComponentGamma\tInstanceUUID=fa8dd337-6991-4535-a069-ca552466d972," + + " RequestID=46161759-1b92-40a4-a408-800e0d62dd9e, ServiceName=service.alpha, EntryTimestamp=2018-05-08T02:45:53.056," + + " InvocationID=aac8fec9-498c-42a2-936b-38f5c0f5ca82, PartnerName=service.beta, ClientIPAddress=127.0.0.1," + + " ServerFQDN=localhost\t\t\tENTRY\t\n"; + + final LogEntry parsed = new LogEntry(eg); + assertThat(parsed.getTimestamp(), notNullValue()); + assertThat(parsed.getThread(), is("pool-1-thread-1")); + assertThat(parsed.getLevel(), is(Level.INFO)); + assertThat(parsed.getLogger(), is("org.onap.logging.ref.slf4j.component.gamma.ComponentGamma")); + assertThat(parsed.getMDCs().get("ServiceName"), is("service.alpha")); + assertThat(parsed.getMDCs().get("PartnerName"), is("service.beta")); + assertThat(parsed.getMessage(), is("")); + assertThat(parsed.getMarkers(), is("ENTRY")); + assertThat(parsed.getException(), is("")); + + } + + @Test + public void testParseMDCsEmpty() { + final Map<String, String> map = LogEntry.parseMDCs(""); + assertThat(map.size(), is(0)); + } + + @Test + public void testParseMDCs() { + final Map<String, String> map = LogEntry.parseMDCs("A=B, C=D , D = F "); + assertThat(map.get("A"), is("B")); + assertThat(map.get("C"), is("D")); + assertThat(map.get("D"), is("F")); + assertThat(map.size(), is(3)); + } +} |