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

add logprocessor to agent #1713

Merged
merged 6 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent.internal.processors;

import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.MatchType;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorAttribute;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorIncludeExclude;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.trace.data.SpanData;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorAttribute;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorIncludeExclude;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.MatchType;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.trace.data.SpanData;
import org.checkerframework.checker.nullness.qual.Nullable;

public abstract class AgentProcessor {
private final @Nullable IncludeExclude include;
private final @Nullable IncludeExclude exclude;
Expand All @@ -40,7 +61,6 @@ protected static AttributeProcessor.IncludeExclude getNormalizedIncludeExclude(P
public static abstract class IncludeExclude {
// Function to compare span with user provided span names or span patterns
public abstract boolean isMatch(SpanData span, boolean isLog);

}

// ok to have this class cover both spanNames and logNames
Expand Down Expand Up @@ -68,30 +88,26 @@ public static StrictIncludeExclude create(ProcessorIncludeExclude includeExclude
if (logNames == null) {
logNames = new ArrayList<>();
}

return new StrictIncludeExclude(attributes, spanNames, logNames);
}

// Function to compare span with user provided span names and log names
public boolean isMatch(SpanData span, boolean isLog) {

if(spanNames.isEmpty() && logNames.isEmpty()) {
if (spanNames.isEmpty() && logNames.isEmpty()) {
// check attributes for both spans and logs
return this.checkAttributes(span);
}
if(isLog) {
if(logNames.isEmpty()) return false;
if(!logNames.isEmpty() && !logNames.contains(span.getName())) return false;
if (isLog) {
if (logNames.isEmpty()) return false;
if (!logNames.isEmpty() && !logNames.contains(span.getName())) return false;
} else {
if(spanNames.isEmpty()) return false;
if(!spanNames.isEmpty() && !spanNames.contains(span.getName())) return false;
if (spanNames.isEmpty()) return false;
if (!spanNames.isEmpty() && !spanNames.contains(span.getName())) return false;
}

return this.checkAttributes(span);
}



// Function to compare span with user provided attributes list
private boolean checkAttributes(SpanData span) {
for (ProcessorAttribute attribute : attributes) {
Expand Down Expand Up @@ -150,7 +166,6 @@ public static RegexpIncludeExclude create(ProcessorIncludeExclude includeExclude
logPatterns.add(Pattern.compile(regex));
}
}

return new RegexpIncludeExclude(spanPatterns, logPatterns, attributeKeyValuePatterns);
}

Expand All @@ -173,19 +188,18 @@ private static boolean isPatternFound(SpanData span, List<Pattern> patterns) {
// Function to compare span with user provided span patterns
public boolean isMatch(SpanData span, boolean isLog) {

if(spanPatterns.isEmpty() && logPatterns.isEmpty()) {
if (spanPatterns.isEmpty() && logPatterns.isEmpty()) {
// check attributes for both spans and logs
return checkAttributes(span);
}

if(isLog) {
if(logPatterns.isEmpty()) return false;
if(!logPatterns.isEmpty() && !isPatternFound(span, logPatterns)) return false;
if (isLog) {
if (logPatterns.isEmpty()) return false;
if (!logPatterns.isEmpty() && !isPatternFound(span, logPatterns)) return false;
} else {
if(spanPatterns.isEmpty()) return false;
if(!spanPatterns.isEmpty() && !isPatternFound(span, spanPatterns)) return false;
if (spanPatterns.isEmpty()) return false;
if (!spanPatterns.isEmpty() && !isPatternFound(span, spanPatterns)) return false;
}

return checkAttributes(span);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package com.microsoft.applicationinsights.agent.internal.processors;
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

import java.util.List;
import java.util.regex.Matcher;
package com.microsoft.applicationinsights.agent.internal.processors;

import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorAction;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorConfig;
Expand All @@ -12,6 +30,9 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;
import java.util.regex.Matcher;

// structure which only allows valid data
// normalization has to occur before construction
public class AttributeProcessor extends AgentProcessor {
Expand All @@ -33,6 +54,19 @@ public static AttributeProcessor create(ProcessorConfig config) {
return new AttributeProcessor(config.actions, normalizedInclude, normalizedExclude);
}

// this won't be needed once we update to 0.13.0
// see https://github.com/open-telemetry/opentelemetry-java/pull/2284
public static String getAttribute(Attributes attributes, AttributeKey<String> key) {
Object existingValueObj = attributes.get(key);
// checking the return type won't be needed once we update to 0.13.0
// see https://github.com/open-telemetry/opentelemetry-java/pull/2284
if (existingValueObj instanceof String) {
return (String) existingValueObj;
} else {
return null;
kryalama marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Function to process actions
public SpanData processActions(SpanData span) {
SpanData updatedSpan = span;
Expand Down Expand Up @@ -149,19 +183,6 @@ private SpanData processExtractAction(SpanData span, ProcessorAction actionObj)
return new MySpanData(span, builder.build());
}

// this won't be needed once we update to 0.13.0
// see https://github.com/open-telemetry/opentelemetry-java/pull/2284
public static String getAttribute(Attributes attributes, AttributeKey<String> key) {
Object existingValueObj = attributes.get(key);
// checking the return type won't be needed once we update to 0.13.0
// see https://github.com/open-telemetry/opentelemetry-java/pull/2284
if (existingValueObj instanceof String) {
return (String) existingValueObj;
} else {
return null;
}
}

@SuppressWarnings("unchecked")
private void putIntoBuilder(AttributesBuilder builder, AttributeKey<?> key, Object value) {
switch (key.getType()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package com.microsoft.applicationinsights.agent.internal.processors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent.internal.processors;

import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorConfig;
import com.microsoft.applicationinsights.agent.internal.processors.AgentProcessor.IncludeExclude;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorConfig;
import com.microsoft.applicationinsights.customExceptions.FriendlyException;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ExporterWithAttributeProcessor implements SpanExporter {

private final SpanExporter delegate;
Expand All @@ -27,11 +47,11 @@ public ExporterWithAttributeProcessor(ProcessorConfig config, SpanExporter deleg
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
// we need to filter attributes before passing on to delegate
List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);
List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);
}

private SpanData process(SpanData span) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent.internal.processors;

import com.microsoft.applicationinsights.agent.internal.processors.AgentProcessor.IncludeExclude;
Expand Down Expand Up @@ -26,20 +47,18 @@ public ExporterWithLogProcessor(ProcessorConfig config, SpanExporter delegate) t
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
// we need to filter attributes before passing on to delegate

List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);

List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);
}

private SpanData process(SpanData span) {
IncludeExclude include = logProcessor.getInclude();
if(!ProcessorUtil.isSpanOfTypeLog(span)) {
if (!ProcessorUtil.isSpanOfTypeLog(span)) {
return span;
}
IncludeExclude include = logProcessor.getInclude();
if (include != null && !include.isMatch(span, true)) {
//If Not included we can skip further processing
return span;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package com.microsoft.applicationinsights.agent.internal.processors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent.internal.processors;

import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorConfig;
import com.microsoft.applicationinsights.agent.internal.processors.AgentProcessor.IncludeExclude;
import com.microsoft.applicationinsights.agent.internal.wasbootstrap.configuration.Configuration.ProcessorConfig;
import com.microsoft.applicationinsights.customExceptions.FriendlyException;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ExporterWithSpanProcessor implements SpanExporter {

private final SpanExporter delegate;
Expand All @@ -27,20 +47,18 @@ public ExporterWithSpanProcessor(ProcessorConfig config, SpanExporter delegate)
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
// we need to filter attributes before passing on to delegate

List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);

List<SpanData> copy = new ArrayList<>();
for (SpanData span : spans) {
copy.add(process(span));
}
return delegate.export(copy);
}

private SpanData process(SpanData span) {
IncludeExclude include = spanProcessor.getInclude();
if(ProcessorUtil.isSpanOfTypeLog(span)) {
if (ProcessorUtil.isSpanOfTypeLog(span)) {
return span;
}
IncludeExclude include = spanProcessor.getInclude();
if (include != null && !include.isMatch(span, false)) {
//If Not included we can skip further processing
return span;
Expand Down
Loading