Skip to content

Commit 20ebb42

Browse files
authored
feat: Init logger, add initial MemDB plugin (#70)
Beginning of #5. Started implementing a MemDB plugin, but other things got in the way: 1. Dropped the plugin builder as it makes it harder to extend it (I found https://projectlombok.org/features/experimental/SuperBuilder but didn't dig deeper) 2. Looks like we shouldn't throw in the serve command so the caller can use the return value and do `System.exit(exitCode)` 3. Removed the docs command as now the CLI does it 4. Initialized the logger via code using `log4j2` (couldn't find an easier way to support JSON logging)
1 parent 9abfbaf commit 20ebb42

File tree

13 files changed

+131
-96
lines changed

13 files changed

+131
-96
lines changed

Diff for: lib/bin/main/io/cloudquery/glob/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Ryan Uber
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: lib/bin/main/io/cloudquery/glob/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Glob Matching Library
2+
3+
This glob-matching library was copied from [ryanuber/go-glob](https://github.com/ryanuber/go-glob) and therefore falls under its [license](LICENSE).

Diff for: lib/build.gradle

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id 'java-library'
3-
id "io.freefair.lombok" version "8.1.0"
3+
id "io.freefair.lombok" version "8.2.2"
44
id "maven-publish"
55
}
66

@@ -36,6 +36,9 @@ dependencies {
3636
implementation "io.cloudquery:plugin-pb-java:0.0.5"
3737
implementation "org.apache.arrow:arrow-vector:12.0.1"
3838

39+
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
40+
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
41+
3942
testImplementation(platform('org.junit:junit-bom:5.10.0'))
4043
testImplementation('org.junit.jupiter:junit-jupiter:5.10.0')
4144
testImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0')

Diff for: lib/src/main/java/io/cloudquery/Main.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.cloudquery;
2+
3+
import io.cloudquery.memdb.MemDB;
4+
import io.cloudquery.server.PluginServe;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
PluginServe serve = PluginServe.builder().plugin(new MemDB()).args(args).build();
9+
int exitCode = serve.Serve();
10+
System.exit(exitCode);
11+
}
12+
}

Diff for: lib/src/main/java/io/cloudquery/memdb/MemDB.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.cloudquery.memdb;
2+
3+
import io.cloudquery.plugin.Plugin;
4+
5+
public class MemDB extends Plugin {
6+
public MemDB() {
7+
super("memdb", "0.0.1");
8+
}
9+
}

Diff for: lib/src/main/java/io/cloudquery/plugin/Plugin.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package io.cloudquery.plugin;
22

3-
import lombok.Builder;
3+
import lombok.AllArgsConstructor;
44
import lombok.Getter;
55
import lombok.NonNull;
66

7-
@Builder(builderMethodName = "innerBuilder")
87
@Getter
9-
public class Plugin {
10-
public static PluginBuilder builder(String name, String version) {
11-
return innerBuilder().name(name).verion(version);
12-
}
13-
8+
@AllArgsConstructor
9+
public abstract class Plugin {
1410
@NonNull
15-
private final String name;
11+
protected final String name;
1612
@NonNull
17-
private final String verion;
13+
protected final String version;
1814
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.cloudquery.scheduler;
2+
3+
public class Scheduler {
4+
public Scheduler() {
5+
}
6+
}

Diff for: lib/src/main/java/io/cloudquery/server/DocCommand.java

-7
This file was deleted.

Diff for: lib/src/main/java/io/cloudquery/server/PluginServe.java

+3-17
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,14 @@
66
import lombok.NonNull;
77
import picocli.CommandLine;
88

9-
import java.util.ArrayList;
10-
import java.util.List;
11-
129
@Builder(access = AccessLevel.PUBLIC)
1310
public class PluginServe {
1411
@NonNull
1512
private final Plugin plugin;
1613
@Builder.Default
17-
private List<String> args = new ArrayList<>();
18-
private boolean destinationV0V1Server;
19-
private String sentryDSN;
20-
private boolean testListener;
21-
//TODO: Allow a test listener to be passed in
22-
// testListenerConn *bufconn.Listener
14+
private String[] args = new String[] {};
2315

24-
public void Serve() throws ServerException {
25-
int exitStatus = new CommandLine(new RootCommand()).
26-
addSubcommand("serve", new ServeCommand(plugin)).
27-
addSubcommand("doc", new DocCommand()).
28-
execute(args.toArray(new String[]{}));
29-
if (exitStatus != 0) {
30-
throw new ServerException("error processing command line exit status = "+exitStatus);
31-
}
16+
public int Serve() {
17+
return new CommandLine(new RootCommand()).addSubcommand(new ServeCommand(plugin)).execute(args);
3218
}
3319
}

Diff for: lib/src/main/java/io/cloudquery/server/RootCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
@CommandLine.Command
66
public class RootCommand {
7-
}
7+
}

Diff for: lib/src/main/java/io/cloudquery/server/ServeCommand.java

+52-28
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,28 @@
99
import io.grpc.Server;
1010
import io.grpc.protobuf.services.ProtoReflectionService;
1111
import lombok.ToString;
12-
12+
import picocli.CommandLine.Command;
13+
import java.io.IOException;
1314
import java.util.List;
1415
import java.util.concurrent.Callable;
1516
import java.util.concurrent.Executors;
16-
import java.util.logging.Level;
17-
import java.util.logging.Logger;
1817

19-
import static picocli.CommandLine.Command;
18+
import org.apache.logging.log4j.Logger;
19+
import org.apache.logging.log4j.Level;
20+
import org.apache.logging.log4j.core.layout.JsonLayout;
21+
import org.apache.logging.log4j.core.layout.PatternLayout;
22+
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.appender.ConsoleAppender;
24+
import org.apache.logging.log4j.core.config.Configuration;
25+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
26+
import org.apache.logging.log4j.core.config.LoggerConfig;
27+
2028
import static picocli.CommandLine.Option;
2129

22-
@Command
30+
@Command(name = "serve", description = "start plugin gRPC server")
2331
@ToString
2432
public class ServeCommand implements Callable<Integer> {
25-
private static final Logger logger = Logger.getLogger(ServeCommand.class.getName());
33+
private static Logger logger;
2634
public static final List<Integer> DISCOVERY_VERSIONS = List.of(3);
2735

2836
@Option(names = "--address", converter = AddressConverter.class, description = "address to serve on. can be tcp: localhost:7777 or unix socket: `/tmp/plugin.rpc.sock` (default \"${DEFAULT-VALUE}\")")
@@ -52,31 +60,47 @@ public ServeCommand(Plugin plugin) {
5260
this.plugin = plugin;
5361
}
5462

55-
@Override
56-
public Integer call() throws Exception {
57-
// Initialize a logger
58-
59-
// Configure open telemetry
63+
private LoggerContext initLogger() {
64+
ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(
65+
this.logFormat == "text" ? PatternLayout.createDefaultLayout() : JsonLayout.createDefaultLayout());
6066

61-
// Configure test listener
67+
Configuration configuration = ConfigurationFactory.newConfigurationBuilder().build();
68+
configuration.addAppender(appender);
69+
LoggerConfig loggerConfig = new LoggerConfig("io.cloudquery", Level.getLevel(logLevel), false);
70+
loggerConfig.addAppender(appender, null, null);
71+
configuration.addLogger("io.cloudquery", loggerConfig);
72+
LoggerContext context = new LoggerContext(ServeCommand.class.getName() + "Context");
73+
context.start(configuration);
6274

63-
// Configure gRPC server
64-
Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create()).
65-
addService(new DiscoverServer(DISCOVERY_VERSIONS)).
66-
addService(new PluginServer(plugin)).
67-
addService(ProtoReflectionService.newInstance()).
68-
executor(Executors.newFixedThreadPool(10)).
69-
build();
70-
71-
// Configure sentry
72-
73-
// Log we are listening on address and port
75+
logger = context.getLogger(ServeCommand.class.getName());
76+
return context;
77+
}
7478

75-
// Run gRPC server and block
76-
server.start();
77-
logger.log(Level.INFO, "Started server on {0}", address);
78-
server.awaitTermination();
79-
return 0;
79+
@Override
80+
public Integer call() {
81+
LoggerContext context = this.initLogger();
82+
83+
try {
84+
// Configure open telemetry
85+
// Configure test listener
86+
// Configure gRPC server
87+
Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create())
88+
.addService(new DiscoverServer(DISCOVERY_VERSIONS)).addService(new PluginServer(plugin))
89+
.addService(ProtoReflectionService.newInstance()).executor(Executors.newFixedThreadPool(10))
90+
.build();
91+
// Configure sentry
92+
// Log we are listening on address and port
93+
// Run gRPC server and block
94+
server.start();
95+
logger.info("Started server on {}:{}", address.host(), address.port());
96+
server.awaitTermination();
97+
return 0;
98+
} catch (IOException | InterruptedException e) {
99+
logger.error("Failed to start server", e);
100+
return 1;
101+
} finally {
102+
context.close();
103+
}
80104
}
81105

82106
}

Diff for: lib/src/main/java/io/cloudquery/server/ServerException.java

-7
This file was deleted.
+15-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package io.cloudquery.server;
22

3+
import io.cloudquery.memdb.MemDB;
34
import io.cloudquery.plugin.Plugin;
45
import io.cloudquery.server.PluginServe.PluginServeBuilder;
56
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.api.Disabled;
78
import org.junit.jupiter.api.Test;
89

9-
import java.util.List;
10-
1110
@Disabled(value = "blocking tests - only used manually to test the gRPC runs correctly")
1211
public class PluginServeTest {
1312
public static final String URL = "https://sentry.url";
@@ -16,41 +15,31 @@ public class PluginServeTest {
1615

1716
@BeforeEach
1817
public void setUp() {
19-
plugin = Plugin.builder("test-plugin", "0.1.0").build();
18+
plugin = new MemDB();
2019
}
2120

2221
@Test
23-
public void simpleCallToServe() throws ServerException {
24-
PluginServe pluginServe = new PluginServeBuilder().
25-
plugin(plugin).
26-
sentryDSN(URL).
27-
args(List.of("serve")).
28-
build();
22+
public void simpleCallToServe() {
23+
PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(new String[] { "serve" }).build();
2924
pluginServe.Serve();
3025
}
3126

3227
@Test
33-
public void simpleCallToServeHelp() throws ServerException {
34-
PluginServe pluginServe = new PluginServeBuilder().
35-
plugin(plugin).
36-
sentryDSN(URL).
37-
args(List.of("serve", "--help")).
38-
build();
28+
public void simpleCallToServeHelp() {
29+
PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(new String[] { "serve", "--help" })
30+
.build();
3931
pluginServe.Serve();
4032
}
4133

4234
@Test
43-
public void simpleOverrideCommandLineArguments() throws ServerException {
44-
PluginServe pluginServe = new PluginServeBuilder().
45-
plugin(plugin).
46-
sentryDSN(URL).
47-
args(List.of(
48-
"serve",
49-
"--address", "foo.bar.com:7777",
50-
"--disable-sentry",
51-
"--otel-endpoint", "some-endpoint"
52-
)).
53-
build();
35+
public void simpleOverrideCommandLineArguments() {
36+
String[] args = new String[] {
37+
"serve",
38+
"--address", "foo.bar.com:7777",
39+
"--disable-sentry",
40+
"--otel-endpoint", "some-endpoint"
41+
};
42+
PluginServe pluginServe = new PluginServeBuilder().plugin(plugin).args(args).build();
5443
pluginServe.Serve();
5544
}
5645
}

0 commit comments

Comments
 (0)