Update java plugin docs for beta (#10534)

This commit is contained in:
Karen Metts 2019-03-13 14:19:59 -04:00 committed by GitHub
parent 6f7d97abce
commit a1f6da67b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 566 additions and 79 deletions

View file

@ -15,7 +15,7 @@
[[java-output-plugin]]
=== How to write a Java output plugin
experimental[]
beta[]
// Pulls in shared section: Setting Up Environment
include::include/javapluginsetup.asciidoc[]
@ -23,41 +23,39 @@ include::include/javapluginsetup.asciidoc[]
[float]
=== Code the plugin
The example output plugin prints events in JSON format to the console. Let's
look at the main class in that example output:
The example output plugin prints events to the console using the event's
`toString` method. Let's look at the main class in the example output:
[source,java]
-----
@LogstashPlugin(name="java_output_example")
@LogstashPlugin(name = "java_output_example")
public class JavaOutputExample implements Output {
public static final PluginConfigSpec<String> PREFIX_CONFIG =
Configuration.stringSetting("prefix", "");
PluginConfigSpec.stringSetting("prefix", "");
private final String id;
private String prefix;
private PrintStream printer;
private final CountDownLatch done = new CountDownLatch(1);
private volatile boolean stopped = false;
public JavaOutputExample(final Configuration configuration, final Context context) {
this(configuration, context, System.out);
public JavaOutputExample(final String id, final Configuration configuration, final Context context) {
this(id, configuration, context, System.out);
}
JavaOutputExample(final Configuration config, final Context context, OutputStream targetStream) {
JavaOutputExample(final String id, final Configuration config, final Context context, OutputStream targetStream) {
this.id = id;
prefix = config.get(PREFIX_CONFIG);
printer = new PrintStream(targetStream);
}
@Override
public void output(final Collection<Event> events) {
try {
Iterator<Event> z = events.iterator();
while (z.hasNext() && !stopped) {
String s = prefix + z.next().toJson();
printer.println(s);
}
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
Iterator<Event> z = events.iterator();
while (z.hasNext() && !stopped) {
String s = prefix + z.next();
printer.println(s);
}
}
@ -76,6 +74,11 @@ public class JavaOutputExample implements Output {
public Collection<PluginConfigSpec<?>> configSchema() {
return Collections.singletonList(PREFIX_CONFIG);
}
@Override
public String getId() {
return id;
}
}
-----
@ -97,7 +100,7 @@ Notes about the class declaration:
in the Logstash pipeline definition. For example, this output would be referenced in the output section of the
Logstash pipeline definition as `output { java_output_example => { .... } }`
** The value of the `name` property must match the name of the class excluding casing and underscores.
* The class must implement the `co.elastic.logstash.api.v0.Output` interface.
* The class must implement the `co.elastic.logstash.api.Output` interface.
[float]
==== Plugin settings
@ -107,7 +110,7 @@ The snippet below contains both the setting definition and the method referencin
[source,java]
-----
public static final PluginConfigSpec<String> PREFIX_CONFIG =
Configuration.stringSetting("prefix", "");
PluginConfigSpec.stringSetting("prefix", "");
@Override
public Collection<PluginConfigSpec<?>> configSchema() {
@ -131,26 +134,28 @@ unsupported settings are present.
[source,java]
-----
private final String id;
private String prefix;
private PrintStream printer;
public JavaOutputExample(final Configuration configuration, final Context context) {
public JavaOutputExample(final String id, final Configuration configuration, final Context context) {
this(configuration, context, System.out);
}
JavaOutputExample(final Configuration config, final Context context, OutputStream targetStream) {
JavaOutputExample(final String id, final Configuration config, final Context context, OutputStream targetStream) {
this.id = id;
prefix = config.get(PREFIX_CONFIG);
printer = new PrintStream(targetStream);
}
-----
All Java output plugins must have a constructor taking both a `Configuration`
and `Context` argument. This is the constructor that will be used to instantiate
them at runtime. The retrieval and validation of all plugin settings should
occur in this constructor. In this example, the values of the `prefix` setting
is retrieved and stored in a local variable for later use in the `output`
method. In this example, a second, pacakge private constructor is defined that
is useful for unit testing with a `Stream` other than `System.out`.
All Java output plugins must have a constructor taking a `String` id and a
`Configuration` and `Context` argument. This is the constructor that will be
used to instantiate them at runtime. The retrieval and validation of all plugin
settings should occur in this constructor. In this example, the values of the
`prefix` setting is retrieved and stored in a local variable for later use in
the `output` method. In this example, a second, pacakge private constructor is
defined that is useful for unit testing with a `Stream` other than `System.out`.
Any additional initialization may occur in the constructor as well. If there are
any unrecoverable errors encountered in the configuration or initialization of
@ -164,14 +169,10 @@ be logged and will prevent Logstash from starting.
-----
@Override
public void output(final Collection<Event> events) {
try {
Iterator<Event> z = events.iterator();
while (z.hasNext() && !stopped) {
String s = prefix + z.next().toJson();
printer.println(s);
}
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
Iterator<Event> z = events.iterator();
while (z.hasNext() && !stopped) {
String s = prefix + z.next();
printer.println(s);
}
}
-----
@ -210,6 +211,20 @@ should **not** signal the output to stop as the `stop` method does. The
awaitStop mechanism may be implemented in any way that honors the API contract
though a `CountDownLatch` works well for many use cases.
[float]
==== getId method
[source,java]
-----
@Override
public String getId() {
return id;
}
-----
For output plugins, the `getId` method should always return the id that was provided to the plugin through its
constructor at instantiation time.
[float]
==== Unit tests