Skip to content

Commit

Permalink
Merge pull request #2 from newrelic-experimental/sling-jobs
Browse files Browse the repository at this point in the history
Sling Event and Scripting
  • Loading branch information
dhilpipre authored Mar 12, 2024
2 parents 8c54592 + 40b5d9e commit e63534b
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
.git
.github
/.metadata/
.settings
.classpath
.project
build
bin
27 changes: 27 additions & 0 deletions apache-sling-event/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

// Build.gradle generated for instrumentation module apache-sling-jobs

apply plugin: 'java'

dependencies {
implementation 'org.apache.sling:org.apache.sling.event:4.2.0'

// New Relic Java Agent dependencies
implementation 'com.newrelic.agent.java:newrelic-agent:6.4.0'
implementation 'com.newrelic.agent.java:newrelic-api:6.4.0'
implementation fileTree(include: ['*.jar'], dir: '../libs')
implementation fileTree(include: ['*.jar'], dir: '../test-lib')
}

jar {
manifest {
attributes 'Implementation-Title': 'com.newrelic.instrumentation.labs.apache-sling-event'
attributes 'Implementation-Vendor': 'New Relic Labs'
attributes 'Implementation-Vendor-Id': 'com.newrelic.labs'
attributes 'Implementation-Version': 1.0
}
}

verifyInstrumentation {
passes 'org.apache.sling:org.apache.sling.event:[4.0.0,)'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.sling.event.impl.jobs.queues;

import org.apache.sling.event.impl.jobs.JobHandler;
import org.apache.sling.event.impl.jobs.JobImpl;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.TracedMethod;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave
public abstract class JobQueueImpl {

@Trace(dispatcher = true)
private void startJob(final JobHandler handler) {
JobImpl job = handler.getJob();
TracedMethod traced = NewRelic.getAgent().getTracedMethod();

traced.addCustomAttribute("QueueName",job.getQueueName());
traced.addCustomAttribute("Topic", job.getTopic());
Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.apache.sling.event.jobs.consumer;

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer.JobResult;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.NewField;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.WeaveAllConstructors;
import com.newrelic.api.agent.weaver.Weaver;

@Weave(type = MatchType.Interface, originalName = "org.apache.sling.event.jobs.consumer.JobConsumer")
public abstract class JobConsumer_instrumentation {

@Trace(dispatcher = true)
public JobResult process(Job job) {
return Weaver.callOriginal();
}

@Weave(type = MatchType.Interface, originalName = "org.apache.sling.event.jobs.consumer.JobConsumer$AsyncHandler")
public static class AsyncHandler {

@NewField
private Token token = null;

@WeaveAllConstructors
public AsyncHandler() {
if(token == null) {
Token t = NewRelic.getAgent().getTransaction().getToken();
if(t != null && t.isActive()) {
token = t;
} else if(t != null) {
t.expire();
t = null;
}
}
}

@Trace(async = true)
public void failed() {
if(token != null) {
token.linkAndExpire();
token = null;
}
Weaver.callOriginal();
}

@Trace(async = true)
public void ok() {
if(token != null) {
token.linkAndExpire();
token = null;
}
Weaver.callOriginal();
}

@Trace(async = true)
public void cancel() {
if(token != null) {
token.linkAndExpire();
token = null;
}
Weaver.callOriginal();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.apache.sling.event.jobs.consumer;

import org.apache.sling.event.jobs.Job;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.TracedMethod;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave(type = MatchType.Interface)
public abstract class JobExecutor {

@Trace
public JobExecutionResult process(Job job, JobExecutionContext context) {
TracedMethod traced = NewRelic.getAgent().getTracedMethod();
traced.setMetricName("Custom","Sling","JobExecutor",getClass().getSimpleName(),"process");
traced.addCustomAttribute("QueueName", job.getQueueName());
traced.addCustomAttribute("Topic", job.getTopic());
JobExecutionResult jobResult = Weaver.callOriginal();
if(jobResult != null) {
String jobResultStr = null;
if(jobResult.cancelled()) {
jobResultStr = "Cancelled";
} else if(jobResult.failed()) {
jobResultStr = "Failed";
} else if(jobResult.succeeded()) {
jobResultStr = "Succeeded";
}
traced.addCustomAttribute("JobResult", jobResultStr);
}
return jobResult;
}
}
32 changes: 32 additions & 0 deletions apache-sling-scripting/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// Build.gradle generated for instrumentation module apache-sling-scripting

apply plugin: 'java'

dependencies {
implementation 'org.apache.sling:org.apache.sling.scripting.core:2.2.0'
implementation 'javax.servlet:javax.servlet-api:3.1.0'
// https://mvnrepository.com/artifact/org.apache.sling/org.apache.sling.api
compileOnly 'org.apache.sling:org.apache.sling.api:2.22.0'

// New Relic Java Agent dependencies
implementation 'com.newrelic.agent.java:newrelic-agent:6.4.0'
implementation 'com.newrelic.agent.java:newrelic-api:6.4.0'
implementation fileTree(include: ['*.jar'], dir: '../libs')
implementation fileTree(include: ['*.jar'], dir: '../test-lib')
}

jar {
manifest {
attributes 'Implementation-Title': 'com.newrelic.instrumentation.labs.apache-sling-scripting'
attributes 'Implementation-Vendor': 'New Relic Labs'
attributes 'Implementation-Vendor-Id': 'com.newrelic.labs'
attributes 'Implementation-Version': 1.0
}
}

verifyInstrumentation {
passes('org.apache.sling:org.apache.sling.scripting.core:[2.2.0,)') {
compile 'org.apache.sling:org.apache.sling.api:2.22.0'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.apache.sling.scripting.core.impl;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.sling.api.scripting.SlingBindings;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave
abstract class DefaultSlingScript {

public abstract String getServletName();

@Trace
public void service(ServletRequest req, ServletResponse res) {
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Sling","DefaultSlingScript","service",getServletName());

Weaver.callOriginal();
}

@Trace
public Object call(SlingBindings props, String method, Object... args) {
NewRelic.getAgent().getTracedMethod().addCustomAttribute("Method", method);
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Sling","DefaultSlingScript","call");
return Weaver.callOriginal();
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ include 'apache-sling-distribution-api'
include 'apache-sling-distribution-core'
include 'apache-sling-model'
include 'apache-sling-provisioning-model'
include 'apache-sling-event'
include 'apache-sling-scripting'

0 comments on commit e63534b

Please sign in to comment.