Configuring New Relic custom metrics with YAML - java

According to the New Relic Documentation:
Starting in version 2.10.0, you can monitor specific methods in your application without modifying code by using a custom instrumentation XML file.
It also says:
Prior to 2.10.0, YAML files could be used for custom instrumentation. These YAML files are still supported.
I can't find documentation for the YAML format anywhere. I'm assuming it's pretty similar to the XML structure, but it can't be a 1-to-1 match.
"Where can I find documentation" seems like a terrible Stack Overflow question, so here is specifically what I want to know. Given the following Java class:
public class Test {
public void foo() {
bar();
}
private void bar() {
}
}
What New Relic YAML configuration would I use to track both foo and bar where foo is a transaction entry point and bar is not (assuming I inferred the meaning of that attribute correctly)?
Thanks a lot!
Patrick

I talked to the folks over at New Relic. The documentation regarding the yml configuration has been removed because it has been deprecated. The old yml configuration support is there for backward compatibility; however, new features have been added to the xml configuration that are not supported by the yml configs . . . for example, method matching without parameter specifications.
The supported approach to custom extensions is to use the XML configuration. A few notes from my experience in case it helps someone else with similar problems.
The custom xml examples documentation at the time of writing has a sample that's invalid if you try to validate it with the command mentioned in the custom monitoring by xml document.
Here's a sample script that worked for me:
<?xml version="1.0" encoding="UTF-8"?>
<extension
xmlns="https://newrelic.com/docs/java/xsd/v1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="newrelic-extension extension.xsd "
name="HelloWorldExtension" version="1.0"
>
<instrumentation>
<pointcut transactionStartPoint="true">
<className>com.autopilotllc.HelloWorld</className>
<method>
<name>sayHello</name>
</method>
</pointcut>
</instrumentation>
</extension>

Related

TomEE custom property provider

I am running my application in TomEE 8. I want to read some property from my DB and pass it to application.
I saw one example from https://rmannibucau.wordpress.com/2014/08/06/tomee-and-more-advanced-resource-configuration/ . may be it is old. it is not working . while deploying application it expect the Resource Type. But example doesn't have Type in resource tag.
<Resource id="..." properties-provider="com.foo.MyPropertiesReader" />
What is the latest/correct way to load my custom properties to my application from my DB ?
I believe that Romain was trying to be concise in his examples. In his examples, it can be surmised that he was using javax.sql.DataSource or DataSource. Despite that, though, this will work for any type of resource, even custom resources. There are also already implementations that you can check out:
HerokuDatabasePropertiesProvider.java
OpenshiftMySQLPropertiesProvider.java
OpenshiftPostgreSQLPropertiesProvider.java
In the page you mentioned, Romain also notes that your class can either implement org.apache.openejb.api.resource.PropertiesResourceProvider, or supply a Properties provides(); method.
Here is a small example:
org.superbiz.provider.MyPropertiesReader.java
package org.superbiz.provider;
import org.apache.openejb.api.resource.PropertiesResourceProvider;
import org.apache.openejb.testng.PropertiesBuilder;
import java.util.Properties;
public class MyPropertiesReader implements PropertiesResourceProvider {
public Properties provides() {
return new PropertiesBuilder()
.p("JdbcDriver", "org.hsqldb.jdbcDriver")
.p("JdbcUrl", "jdbc:hsqldb:mem:moviedb")
.build();
}
}
src/main/webapp/WEB-INF/resources.xml
<resources>
<Resource id="movieDatabase"
type="DataSource"
properties-provider="org.superbiz.provider.MyPropertiesReader"/>
</resources>
These are the key snippets that I hope will help clear your doubts. Implementation of the datasource is left for you to code. :)

How can I parse yaml comments using java?

I want to use a yml configuration file in my project. I am using jackson-dataformat-yaml for parsing yml files. But I need to parse yml comments as well. I used the similar approach in python using ruamel yaml. How can I do the same in java?
Upd.
What for? Well, I wanted to make it possible to override my configuration options by using command line arguments. So, to generate description message for each option, I wanted to use my comments. Like this:
In my config.yml
# Define a source directory
src: '/foo/bar'
# Define a destination directory
dst: '/foo/baz'
So when you run your program with the --help flag, you'll see the following output:
Your program can be ran with the following options:
--src Define a source directory
--dst Define a destination directory
The main benefit in such a model is that you don't ever need to repeat the same statement twice, because they can be retrieved from the configuration file.
Basically, you have three layers of data:
Your configuration schema. This defines the values that are to be defined in the configuration file.
The configuration file itself, which describes the usual configuration on the current machine.
One-time switches, which override the usual configuration.
The descriptions of what each value does belong to the schema, not to the configuration file itself. Think about it: If someone edits the configuration file on their machine and changes the comments, your help output would suddenly show different descriptions.
My suggestion would be to add the descriptions to the schema. The schema is the Java class you load your YAML into. I am not sure why you are using Jackson, since it uses SnakeYaml as parser and SnakeYaml is perfectly able to deserialize into Java classes, but has more configuration options since it does not generalize over JSON and YAML like Jackson does.
Here's a general idea how to do it with SnakeYaml (beware, untested):
// ConfigParam.java
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface ConfigParam { String description(); }
// Configuration.java
public class Configuration {
#ConfigParam("Define a source directory")
String src;
#ConfigParam("Define a destination directory")
String dst;
}
// loading code
Yaml yaml = new Yaml(new Constructor(Configuration.class));
Configuration config = yaml.loadAs(input, Configuration.class);
// help generation code
System.out.println("Your program can be ran with the following options:")
for (Field field: Configuration.class.getFields()) {
ConfigParam ann = field.getAnnotation(ConfigParam.class);
if (ann != null) {
System.out.println(String.format("--%s %s", field.getName(), ann.description());
}
}
For mapping actual parameters to the configuration, you can also loop over class fields and map the parameters to the field names after having loaded the configuration (to replace the standard values with the given ones).

Java: xml to singleton (using maven & jaxb)

I want to do such thing completely automatically by maven.
I have a .xml file with some data. There is some references by ids from one object in this xml to another, and so on. I don't have .xsd of this .xml.
I need exactly two things:
1) Compile this xml to Java classes.
2) Creating one Singleton class named by this .xml name and containing all .xml data relative the inner .xml structure.
For example, my .xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<MyBigSinletonWithAllData>
<Cars>
<Car id="1" speed = "5"/>
<Car id="2" speed = "3"/>
</Cars>
</MyBigSinletonWithAllData>
And I want to get somehting like this, as automatic output:
class MyBigSinletonWithAllData {
List<Cars> cars;
}
class Car {
double speed;
}
class MyBigSinletonWithAllDataSingleton {
MyBigSinletonWithAllData INSTANCE = new MyBigSinletonWithAllData();
/* And here we read an INSTANCE from our .xml file */
}
So, my question is how to do this fully automatically by Maven?
I don't want to code manually the "MyBigSinletonWithAllDataSingleton", I just want to get it already generated for me, so I need only to write code line like this:
MyBigSinletonWithAllDataSingleton.INSTANCE
to get full access to all data that written in relative .xml file.
For now I use "maven-jaxb2-plugin" to generate Java classes from .xsd; But I also need a tool that do .xsd from .xml, and tool that create automatically read/write operations to singleton.
In Java SOAP based webservices there are different implementation like apache axis and apche cxf. In that implementations we have tools which we will run or simply we will pass the XML file to that tool so corresponding binding classes will be generated.
Please look into their code so that it will helps you.
or may be some different tools also will be there(google it).
And yes you can convert xml to xsd file.
Go through the link
Conver XML to XSD

Spring Soap web service client using a WSDL and codehaus jaxb2; need XmlRootElement?

I am working within an existing Spring web application; I need to write code to call to a SOAP web service that provides a WSDL. I also use Netbeans, though am willing to jump out to a command line whenever that's easier.
I have configured the Maven pom for the project to include the codehaus jaxb2-maven-plugin, and written a small test program to send a request. When that call is executed, I get an error saying that it cannot process one of the generated classes because it has no XmlRootElement annotation.
On searching further for that error, I find LOTS of requests for information on it, but none that apply. Most of them use a different JaxB library, and all of them give me examples of how to configure their plugin, not the one I have.
I suppose I can change plugins (I already have once), but what I would REALLY like is to find some decent documentation on THIS one. I need something that can be done on Java 7, not 8, that does not involve Spring 4 (or 5, or 6), preferably just an explanation of the various options that can be supplied to the maven plugin and/or the command line to generate classes so they can be marshalled and unmarshalled by Spring's default classes for the purposes.
--- Edit
Here's what I have at the moment; since it's testing code, I just have the marshaller declared and set up in the code instead of the configuration:
public class XClient extends WebServiceGatewaySupport {
public GetXResponse getXResponse(GetX XRequest) {
// do in configuration for production...
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.X.pics.service");
setMarshaller(marshaller);
setUnmarshaller(marshaller);
String uri = "https://site.X.com/services/X";
WebServiceTemplate template = getWebServiceTemplate();
Object response = template.marshalSendAndReceive(uri,
XRequest
);
GetXResponse getXResponse = (GetXResponse) response;
return getXResponse;
}
}
When I run my program, it gives the following (just first lines):
org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.xo.pics.service.GetPricing" as an element because it is missing an #XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:322)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
I put in the line about preferring Spring libraries since a lot of answers on SO seem to take the form of "Change over to this tool/library", not because I didn't think it could be done in Spring. I'll be happy to configure and use a marshaller within Spring, as soon as I figure out (or am told) how.
I finally got this working; it does seem like it ought to be easier.
The POM file entry for the JAXB Maven plugin now looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceType>wsdl</sourceType>
<sources>
<source>C:/projects/gw/src/main/resources/wsdl/xo2.wsdl</source>
</sources>
<extension>true</extension>
<xjbSources>
<xjbSource>bindings.xjb</xjbSource>
</xjbSources>
</configuration>
</plugin>
One of the difficulties I faced was a lack of documentation on the options here; I eventually looked inside the jar file in the plugin code and found the jaxb2-maven-plugin.pom there, and looked through the unformatted documentation for the various options. This is how I discovered the "sourceType", "sources", and "xjbSources" tags. It was also looking in that part of my .m2 directory that helped me realize the different versions available, and the docs available on the web do warn you of the major differences between 1.x and 2.x.
Anyway, I had found the following bindings file, though wasn't sure until later how to specify a bindings file in this version of the plugin:
<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings>
<jxb:globalBindings>
<xjc:simple/>
</jxb:globalBindings>
</jxb:bindings>
</jxb:bindings>
The specification of in the bindings is what causes the #XmlRootElement in the generated sources; you need this if your WSDL has complex types that have a defined name.
Then my client boiled down to this:
public class XOClient extends WebServiceGatewaySupport {
public GetPricingResponse getPricingResponse(GetPricing pricingRequest) {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(GetPricing.class, GetPricingResponse.class);
setMarshaller(marshaller);
setUnmarshaller(marshaller);
String uri = "https://blah.blah.blah.com/services/pricing";
Object o = getWebServiceTemplate().marshalSendAndReceive(uri, pricingRequest);
GetPricingResponse response = (GetPricingResponse)o;
return response;
}
}
In the final Spring application, I would be more likely to configure the marshaller URI and bound classes instead of writing code to set them, but to use in my little testing program this was easier.
So this allows me to build up my pricingRequest object and get back a GetPricingResponse object, and use any of the other API methods similarly.
Hope this is a help to someone else.

Use a class field in the 'plugin.xml' for an eclipse plugin

I am currently working on a little Eclipse plugin and I have to deal with the classic 'plugin.xml' stuff, such as the creation of a nature:
<extension
id="aplugin.natures.MyNature.NATURE_ID.id"
name="Sample Project Nature"
point="org.eclipse.core.resources.natures">
Now in this particular example, I must, somewhere in my plugin code, give this 'id' as a String to some Eclipse function. So I need to create a specific class like:
package aplugin.natures;
public class MyNature implements IProjectNature {
public static final String NATURE_ID = "aplugin.natures.MyNature.NATURE_ID.id"; //$NON-NLS-1$
}
And here comes my problem, I got some copy and paste of my 'id'. I must admit that I am not very proud of it.
So my question is, does someone know a way to use the 'NATURE_ID' field in the 'MyNature' class directly in the 'plugin.xml' file ?.
In the end I wish to write something like:
<extension id="${aplugin.natures.MyNature.NATURE_ID}" ... >
That is not possible by design.
The idea is that the Eclipse core can load the plugin.xml files for all resolved plug-ins/bundles without loading/activating the plug-ins. Resolving your construct above would normally require Eclipse to resolve all references for the class - MyNature in this case - which can easily lead to the activation of many other dependent plug-ins. Not good.
So - by design - all data in plugin.xml must be self-contained. (With the possible exception of localized strings).

Categories

Resources