Git Product home page Git Product logo

librato-java's Introduction

DEPRECATED: If you are using AppOptics please refer to appoptics-api-java.

librato-java

Java language bindings for the Librato Metrics API.

Looking for a previous version?

You can find documentation for versions < 2.x.x here.

Maven Dependency

<dependency>
    <groupId>com.librato.metrics</groupId>
    <artifactId>librato-java</artifactId>
    <version>2.1.0</version>
</dependency>

Setup

You must first initialize the client:

LibratoClient client = LibratoClient.builder(email, apiToken)
    // these are optional
    .setConnectTimeout(new Duration(5, SECONDS))
    .setReadTimeout(new Duration(5, SECONDS))
    .setAgentIdentifier("my app name")
    // and finally build
    .build();

Once your client has been built, you can submit measures to the Librato API:

PostMeasuresResult result = client.postMeasures(new Measures()
    .add(new GaugeMeasure(name, value))
    .add(new GaugeMeasure(name, value).setSource("uid:43"))
    .add(new GaugeMeasure(name, sum, count, min, max))
    .add(new GaugeMeasure(name, sum, count, min, max, sumSquares))
    .add(new CounterMeasure(name, value))
    .add(new TaggedMeasure(name, value, tag, tag))
    .add(new TaggedMeasure(name, sum, count, min, max, tag, tag ,tag)));

for (PostResult postResult : result.results) {
    if (result.isError()) {
        log.error(result.toString);
    }
}

Note that if you wish to submit tagged measures you need to first contact [email protected] to get early access to this new feature.

librato-java's People

Contributors

collinvandyck avatar joemadeus avatar josephruscio avatar lucky avatar mheffner avatar mihasya avatar mjr5749 avatar petercable avatar philmcmahon avatar sergeyvladimirovich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

librato-java's Issues

Post Measures - Support for TLS 1.2

Since the drop of TLS 1.0 support I am unable to use the postMeasures endpoint without a Socket Exception. This is librato-java 2.1.2

java.lang.RuntimeException: java.net.SocketException: Connection reset
at com.librato.metrics.client.DefaultPoster.post(DefaultPoster.java:63)
at com.librato.metrics.client.LibratoClient$4.call(LibratoClient.java:94)
at com.librato.metrics.client.LibratoClient$4.call(LibratoClient.java:87)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

Are there plans to support this or is there another way to post measures which doesn't have this error?

Queue failed submissions to try again

If I have network connectivity issues for a few minutes it would be great if the library tried to submit the missed submissions again when the connectivity is restored.

Dashes being stripped out of metric names

(This bug has already been confirmed by @jderrett via support, but adding the report here...)

@garnold

Another thing I've bumped into is metric naming. According to the API
docs
, metric names can contain the following characters: A-Za-z0-9.:-_. I recently changed a metric name from using _ to using -, however when viewed in control panel metrics list the dash is stripped out.

@jderrett

Sorry, but it does look like there is a bug in the Java library that prevents dashes from being used. The regex for specifying allowed characters has a subtle issue: the dash is seen as a “range” rather than a literal character (but underscore is ok). Here’s the code if you’re interested: https://github.com/librato/librato-java/blob/master/src/main/java/com/librato/metrics/Sanitizer.java#L25

Instantiation of a Sanitizer

Hi,

I am trying to use this library to send metrics to librato. The sample code in the documentation seems to be missing the instantiation of the sanitizer:

LibratoBatch batch = new LibratoBatch(batchSize, sanitizer, timeout, timeoutUnit, agent, poster)

Looking at the code, it seems that the required sanitizer (Sanitizer.LAST_PASS) is always executed, so I suppose, that Sanitizer.NO_OP is expected here?

LibratoBatch batch = new LibratoBatch(batchSize, SANITIZER.NO_OP, timeout, timeoutUnit, agent, poster)

That's a bit confusing, although this is a special library for librato, I have to write much more code than expected. Using a third party implementation (like https://github.com/GeoNet/app-server-metrics) which is using just an HTTP client, would lead to easier code:

sender = new LibratoMetricsSender(libratoUser, libratoApiKey); sender.send("server", ...);

Is there a shorter way, like this, to send metrics to librato?
Thanks.

Switch to Jackson under FasterXML namespace

Curious if there is a particular reason for using org.codehaus.jackson instead of com.fasterxml.jackson? As far as I know the codehaus groupid was deprecated a long time ago in favor of FasterXML

Design discussion in a multithreaded use of LibratoBatch

Hi there,

in the application I wrote I discovered a memory leak which was caused by too much thread objects called "librato-http-poster". The Problem in this case was that I used my easy way to create the LibratoBatch instance with the Builder (see #31), but I did not call .close() for the DefaultHttpPoster.

It is not possible to use a single DefaultHttpPoster for several instances of LibratoBatch, because the DefaultHttpPoster is not thread safe, so I have to create a new one (and thus, a new LibratoBatch) for each few metrics I want to send to librato. That's ok, but this leads to some design issues in code:

  • When creating a LibratoBatch I don't want to know anything about the underlying implementation. But before creating a LibratoBatch now, I have to create a new DefaultHttpPoster first, and keep it in memory to be able to close the thread pool after posting.
  • When posting the actual metrics with LibratoBatch, I have to create a new LibratoBatch instance and thus, know about the credentials. This is not the location in code where I want to care about configuration.

Not sure, if I explained this issues clearly enough.

For the moment I work around this closing the DefaultHttpPoster, as I am using Groovy this works (although not recommended):

libratoBatch.httpPoster.close()

There are several possibilities to fix this issues:

  1. Make DefaultHttpPoster completely threadsafe, so that it can be reused (as singleton bean)
  2. Create a close() method (or similar) in LibratoBatch which calls close() of it's HttpPoster
  3. Delegate the instantiation and closing of the executor pool to the post() method of the DefaultHttpPoster (in my eyes this would be the best solution, why should other code care about the internals of the thread implementation of this class?)

Proxy support for LibratoClient

Hi,
I am trying to send metrics to librato from an application running behind a firewall. I have to use a http proxy to talk to librato. It seems the LibratoClient does not support proxies at the moment. Is it planned to add that feature in the future?

Thanks!

Can't add a period to a measurement

There's no way to add a period to a metric or batch. The librato period is very useful for keeping costs down on Librato's system, so it'd be great to have this as part of the package.

Need far more tests

Ideally, an in-process test http server that can be posted to to verify the json being produced.

Failed to load class "org.slf4j.impl.StaticLoggerBinder"

Hi,

Upgrading to the latest Java bindings and I see the following -

04-04 14:37:44.311 12316-12316/com.myapp W/System.err: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
04-04 14:37:44.311 12316-12316/com.myapp W/System.err: SLF4J: Defaulting to no-operation (NOP) logger implementation
04-04 14:37:44.311 12316-12316/com.myapp W/System.err: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
04-04 14:37:52.507 12316-12316/com.myapp D/AndroidRuntime: Shutting down VM

Using Android Studio 2.3 (latest) and all the latest tools.

build.gradle here -

apply plugin: 'java'
apply plugin: 'maven'
group = 'com.librato.metrics'
version = '2.0.6'
description = """Librato Metrics API Java Library"""
sourceCompatibility = 1.6
targetCompatibility = 1.6
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
configurations.all {
}
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version:'1.9.7'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.6.6'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version:'1.6.6'
testCompile group: 'junit', name: 'junit-dep', version:'4.10'
testCompile group: 'org.mockito', name: 'mockito-core', version:'1.9.5'
testCompile group: 'org.assertj', name: 'assertj-core', version:'1.7.1'
}

Logcat -

AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 29335
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/bind/DatatypeConverter;
at com.librato.metrics.client.Authorization.base64Encode(Authorization.java:29)
at com.librato.metrics.client.Authorization.buildAuthHeader(Authorization.java:25)
at com.librato.metrics.client.LibratoClient.(LibratoClient.java:45)
at com.librato.metrics.client.LibratoClientBuilder.build(LibratoClientBuilder.java:44)
at ...
android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1036)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4744)
at android.app.ActivityThread.access$1600(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.bind.DatatypeConverter" on path: DexPathList[[zip file "/data/app/com.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.app-2/lib/arm64, /data/app/com.app-2/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.librato.metrics.client.Authorization.base64Encode(Authorization.java:29) 
at com.librato.metrics.client.Authorization.buildAuthHeader(Authorization.java:25) 
at com.librato.metrics.client.LibratoClient.(LibratoClient.java:45) 
at com.librato.metrics.client.LibratoClientBuilder.build(LibratoClientBuilder.java:44) 
at
...
android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1036) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4744) 
at android.app.ActivityThread.access$1600(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5459) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Suppressed: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 20 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Looks like "Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.bind.DatatypeConverter" is not available on Android

http://stackoverflow.com/questions/5461127/using-jaxb-with-google-android

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.