Sidehistorik
...
Read Access Logging in Confluence for good reasons to log via the Event system.
Tip |
---|
To make this work, the jar file from https://github.com/datadog/java-dogstatsd-client#java-dogstatsd-client musk be placed under confluence/WEB-INF/lib and Confluence must be restarted |
The script is executed by an Event Handler in Confluence:
...
Kodeblok | ||
---|---|---|
| ||
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal import com.atlassian.confluence.user.*; import com.atlassian.confluence.pages.Page import com.atlassian.confluence.pages.PageManager import com.atlassian.confluence.spaces.Space import com.atlassian.confluence.spaces.SpaceManager import com.atlassian.sal.api.component.ComponentLocator import com.atlassian.confluence.event.events.content.page.* import groovy.transform.Field import javacom.timgroup.netstatsd.InetAddressStatsDClient; import javacom.timgroup.netstatsd.DatagramPacketNonBlockingStatsDClient; System.out.println("Start post2splunk.groovy")StatsDClient statsd = new NonBlockingStatsDClient("confluence.stats.views","localhost",8125,""); def spaceManager = ComponentLocator.getComponent(SpaceManager) def pageManager = ComponentLocator.getComponent(PageManager) String userName="Anonymous" def currentUser = AuthenticatedUserThreadLocal.get() if (currentUser) { userName=(String)currentUser.name } System.out.println("Step 1 post2splunk.groovy") def event = event as PageEvent // keys to create unique nodes for counters // https://docs.atlassian.com/confluence/5.9.7/com/atlassian/confluence/pages/Page.html System.out.println("Step 2 post2splunk.groovy") @Field final def host = "127.0.0.1" @Field final def port = 8125 def spaceKey = event.page.spaceKey def pageId = event.page.id as String def nodeId = "confluence.stats.views" // build the unique metric keys def pageViewMetricKey = "${nodeId}. statsd.incrementCounter("page.${pageId}" def spaceViewMetricKey = "${nodeId}.); statsd.incrementCounter("space.${spaceKey}" def userViewMetricKey = "${nodeId}.); statsd.incrementCounter("user.${userName}.${pageId}" System.out.println("Step 3 post2splunk.groovy") // increase by one the counters for the following metric keys increaseByOne(pageViewMetricKey, userViewMetricKey, spaceViewMetricKey) def increaseByOne(String... keys) { def dataToSend = "" def value = 1 //increase counter by one System.out.println("Step 4 post2splunk.groovy") //syntax for counter according to https://github.com/etsy/statsd/blob/master/docs/metric_types.md for (key in keys) { dataToSend += "${key}:${value}|c\n" } System.out.println("Step 5 post2splunk.groovy") def data = dataToSend.getBytes() //def address = InetAddress.getByName(host as String) def address = InetAddress.getLocalHost(); def packet = new DatagramPacket(data, data.length, address, port as int) def socket = new DatagramSocket() try { System.out.println("Step 6 post2splunk.groovy") socket.send(packet) } finally { System.out.println("Step 7 post2splunk.groovy") socket.close() } } System.out.println("End post2splunk.groovy") |