Skip to content

Commit 2db0f41

Browse files
committed
Refactoring: Unifying method names.
* start() and build() has been unified to open() function names. * Label values may be created from any primitive type.
1 parent 92c1c9f commit 2db0f41

11 files changed

Lines changed: 153 additions & 57 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ import pl.mjaron.tinyloki.*;
3232

3333
public class Sample {
3434
public static void main(String[] args) {
35-
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withBasicAuth("user", "pass").start();
36-
ILogStream helloStream = loki.stream().info().l("topic", "hello").build();
37-
helloStream.log("Hello world!");
35+
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withBasicAuth("user", "pass").open();
36+
ILogStream logStream = loki.stream().info().l("topic", "shortExample").open();
37+
logStream.log("Hello world!");
38+
logStream.log("Hello world!", Labels.of("structured_metadata", "My log metadata value."));
3839
loki.closeSync();
3940
}
4041
}

src/main/java/pl/mjaron/tinyloki/ILogMonitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ static void printError(final String what) {
8585
void onSync(final boolean isSuccess);
8686

8787
/**
88-
* Called when start operation ends.
88+
* Called when {@link IExecutor} has started.
8989
*
9090
* @since 1.0.0
9191
*/
9292
void onStart();
9393

9494
/**
95-
* Called when stop operation ends.
95+
* Called when {@link IExecutor} has stopped.
9696
*
9797
* @param isSuccess The stop operation result.
9898
* @since 1.0.0

src/main/java/pl/mjaron/tinyloki/Labels.java

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,99 @@ public static Labels of(final Map<String, String> labels) {
419419
/**
420420
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
421421
*
422-
* @param name Label name. Valid label identifier starts with letter and contains only letters, digits or '_'.
423-
* @param value Label value. Valid label identifier starts with letter and contains only letters, digits or '_'.
422+
* @param name Label name.
423+
* @param value Label value.
424424
* @return New instance of {@link Labels}.
425-
* @see #l(String, String)
426425
* @since 1.0.1
427426
*/
428427
public static Labels of(final String name, final String value) {
429428
return new Labels().l(name, value);
430429
}
431430

431+
/**
432+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
433+
*
434+
* @param name Label name.
435+
* @param value Label value.
436+
* @return New instance of {@link Labels}.
437+
* @since 1.1.0
438+
*/
439+
public static Labels of(final String name, final int value) {
440+
return new Labels().l(name, value);
441+
}
442+
443+
/**
444+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
445+
*
446+
* @param name Label name.
447+
* @param value Label value.
448+
* @return New instance of {@link Labels}.
449+
* @since 1.1.0
450+
*/
451+
public static Labels of(final String name, final long value) {
452+
return new Labels().l(name, value);
453+
}
454+
455+
/**
456+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
457+
*
458+
* @param name Label name.
459+
* @param value Label value.
460+
* @return New instance of {@link Labels}.
461+
* @since 1.1.0
462+
*/
463+
public static Labels of(final String name, final float value) {
464+
return new Labels().l(name, value);
465+
}
466+
467+
/**
468+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
469+
*
470+
* @param name Label name.
471+
* @param value Label value.
472+
* @return New instance of {@link Labels}.
473+
* @since 1.1.0
474+
*/
475+
public static Labels of(final String name, final double value) {
476+
return new Labels().l(name, value);
477+
}
478+
479+
/**
480+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
481+
*
482+
* @param name Label name.
483+
* @param value Label value.
484+
* @return New instance of {@link Labels}.
485+
* @since 1.1.0
486+
*/
487+
public static Labels of(final String name, final char value) {
488+
return new Labels().l(name, value);
489+
}
490+
491+
/**
492+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
493+
*
494+
* @param name Label name.
495+
* @param value Label value.
496+
* @return New instance of {@link Labels}.
497+
* @since 1.1.0
498+
*/
499+
public static Labels of(final String name, final byte value) {
500+
return new Labels().l(name, value);
501+
}
502+
503+
/**
504+
* Creates labels containing single name-value pair. Call {@link #l(String, String)} method to add more labels.
505+
*
506+
* @param name Label name.
507+
* @param value Label value.
508+
* @return New instance of {@link Labels}.
509+
* @since 1.1.0
510+
*/
511+
public static Labels of(final String name, final short value) {
512+
return new Labels().l(name, value);
513+
}
514+
432515
/**
433516
* Creates a new empty labels instance. Call {@link #l(String, String)} method to add any labels.
434517
*

src/main/java/pl/mjaron/tinyloki/Settings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public IExecutor getExecutor() {
501501
*
502502
* @return New {@link TinyLoki} instance.
503503
*/
504-
public TinyLoki start() {
505-
return TinyLoki.createAndStart(this);
504+
public TinyLoki open() {
505+
return TinyLoki.open(this);
506506
}
507507
}

src/main/java/pl/mjaron/tinyloki/StreamBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
/**
66
* Helper class for shorter initialization code.
77
* <p>
8-
* call {@link #build()} to initialize {@link ILogStream} with parameters passed to this builder.
8+
* call {@link #open()} to initialize {@link ILogStream} with parameters passed to this builder.
99
* <p>
1010
* Example:
1111
* <pre>{@code
12-
* ILogStream myStream = logController.stream().info().l("my_custom_label", "value").build();
12+
* ILogStream myStream = logController.stream().info().l("my_custom_label", "value").open();
1313
* }</pre>
1414
*/
1515
public class StreamBuilder {
@@ -26,7 +26,7 @@ public StreamBuilder(final TinyLoki tinyLoki) {
2626
*
2727
* @return New {@link ILogStream} instance.
2828
*/
29-
public ILogStream build() {
29+
public ILogStream open() {
3030
return tinyLoki.createStream(labels);
3131
}
3232

src/main/java/pl/mjaron/tinyloki/TinyLoki.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* <pre>{@code
1818
* TinyLoki tinyLoki = TinyLoki.withUrl("http://localhost:3100")
1919
* .withBasicAuth("user", "pass")
20-
* .start();
20+
* .open();
2121
*
22-
* ILogStream windowStream = tinyLoki.stream().info().l("device", "window").build();
23-
* ILogStream doorStream = tinyLoki.stream().info().l("device", "door").build();
22+
* ILogStream windowStream = tinyLoki.stream().info().l("device", "window").open();
23+
* ILogStream doorStream = tinyLoki.stream().info().l("device", "door").open();
2424
*
2525
* windowStream.log("The window is open.");
2626
* doorStream.log("The door is open.");
@@ -85,7 +85,7 @@ public class TinyLoki implements java.io.Closeable {
8585
* .withConnectTimeout(connectTimeout)
8686
* .withLogCollector(logCollector)
8787
* .withLogMonitor(logMonitor)
88-
* .start(); // LogController created here.
88+
* .open(); // LogController created here.
8989
* </pre>
9090
* <p>
9191
* Optionally call {@link #withExactUrl(String)} instead to skip URL value normalization
@@ -124,7 +124,7 @@ public static Settings withExactUrl(final String url) {
124124
* @see #withUrl(String)
125125
* @since 0.3.0
126126
*/
127-
public static TinyLoki createAndStart(final Settings settings) {
127+
public static TinyLoki open(final Settings settings) {
128128
return new TinyLoki(settings.getLogCollector(), settings.getLogEncoder(), settings.getLogSenderSettings(), settings.getLogSender(), settings.getLabelSettings(), settings.getStructuredMetadataLabelSettings(), settings.getExecutor(), settings.getBuffering(), settings.getLogMonitor()).start();
129129
}
130130

@@ -135,13 +135,13 @@ public static TinyLoki createAndStart(final Settings settings) {
135135
* @param user Basic authentication user. If null, BA header will not be sent.
136136
* @param pass Basic authentication password. If null, BA header will not be sent.
137137
* @return New {@link TinyLoki LogController} object.
138-
* @deprecated Use {@link TinyLoki#withUrl(String)} to initialize settings and finally call {@link Settings#start()}, e.g:
138+
* @deprecated Use {@link TinyLoki#withUrl(String)} to initialize settings and finally call {@link Settings#open()}, e.g:
139139
* <pre>
140-
* TinyLoki.withUrl(url).withBasicAuth(user, pass).start();
140+
* TinyLoki.withUrl(url).withBasicAuth(user, pass).open();
141141
* </pre>
142142
*/
143-
public static TinyLoki createAndStart(final String url, final String user, final String pass) {
144-
return TinyLoki.withUrl(url).withBasicAuth(user, pass).start();
143+
public static TinyLoki open(final String url, final String user, final String pass) {
144+
return TinyLoki.withUrl(url).withBasicAuth(user, pass).open();
145145
}
146146

147147
private final ILogCollector logCollector;
@@ -258,7 +258,7 @@ public ILogStream createStream(final Labels labels) {
258258
* Provides a {@link StreamBuilder} to initialize the stream. E.g:
259259
*
260260
* <pre>{@code
261-
* ILogStream myStream = logController.stream().info().l("my_custom_label", "value").build();
261+
* ILogStream myStream = logController.stream().info().l("my_custom_label", "value").open();
262262
* }</pre>
263263
*
264264
* @return New instance of {@link StreamBuilder}.

src/test/java/pl/mjaron/tinyloki/IntegrationTest.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public static boolean isIntegrationEnabled() {
2424

2525
@Test
2626
void shortExample() throws InterruptedException {
27-
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withBasicAuth("user", "pass").start();
28-
ILogStream helloStream = loki.stream().info().l("topic", "hello").build();
29-
helloStream.log("Hello world!");
27+
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withBasicAuth("user", "pass").open();
28+
ILogStream logStream = loki.stream().info().l("topic", "shortExample").open();
29+
logStream.log("Hello world!");
30+
logStream.log("Hello world!", Labels.of("structured_metadata", "My log metadata value."));
3031
loki.closeSync();
3132
}
3233

3334
@Test
34-
void verboseTest() throws InterruptedException {
35+
void verboseExample() throws InterruptedException {
3536

3637
// Initialize the log controller instance with URL.
3738
// The endpoint loki/api/v1/push will be added by default if missing.
@@ -42,7 +43,8 @@ void verboseTest() throws InterruptedException {
4243
try (TinyLoki loki = TinyLoki.withUrl("http://localhost:3100/loki/api/v1/push")
4344

4445
// Print all diagnostic information coming from the TinyLoki library. For diagnostic purposes only.
45-
.withVerboseLogMonitor()
46+
// The messages are printed only if there is no log encoder (comment out .withGzipLogEncoder())
47+
.withVerboseLogMonitor(true)
4648

4749
// Set the custom log processing interval time.
4850
// So the executor will try to send the next logs 10 seconds after the previous logs sending operation.
@@ -52,26 +54,37 @@ void verboseTest() throws InterruptedException {
5254
.withConnectTimeout(10 * 1000)
5355

5456
// Encode the logs to limit the size of data sent.
55-
.withGzipLogEncoder()
57+
// .withGzipLogEncoder()
5658

5759
// The BasicBuffering is set by default, but here the (not encoded) message size limit may be customized.
5860
.withBasicBuffering(3 * 1024 * 1024, 10)
5961

6062
// Initialize the library with above settings.
6163
// The ThreadExecutor will create a new thread and start waiting for the logs to be sent.
62-
.start()) {
64+
.open()) {
6365

6466
// Some logs here...
65-
ILogStream whiteStream = loki.stream().l("color", "white").build();
67+
68+
// Let's define some labels common for few streams.
69+
Labels topic = Labels.of("topic", "verboseExample");
70+
71+
ILogStream topicStream = loki.stream().l(topic).open();
72+
topicStream.log("Hello world.");
73+
74+
ILogStream whiteStream = loki.stream().l(topic).l("topic", "verboseExample").l("color", "white").open();
6675
whiteStream.log("Hello white world.");
6776

6877
// Blocking method, tries to send the logs ASAP and wait for sending completion.
6978
// This method returns false when timeout occurs, but true when sending has completed with success or failure.
7079
boolean allHttpSendingOperationsFinished = loki.sync();
7180
System.out.println("Are all logs processed: " + allHttpSendingOperationsFinished);
7281

73-
ILogStream redStream = loki.stream().l("color", "red").build();
74-
redStream.log("Hello white world.");
82+
ILogStream redStream = loki.stream().l(topic).l("color", "red").open();
83+
84+
// Let's attach the Grafana Loki structured metadata.
85+
// In current implementation, the duplicated logs with same log line and timestamp (structured metadata doesn't matter) - is sent but may be dropped by Grafana Loki.
86+
redStream.log("Hello red world 0", Labels.of("structured_metadata_label", 0).l("other_structured_metadata_label", 'a'));
87+
redStream.log("Hello red world 1", Labels.of("structured_metadata_label", 9).l("other_structured_metadata_label", 'z'));
7588

7689
// Blocking method, tries to synchronize the logs than interrupt and join the execution thread.
7790
// Set the custom timeout time for this operation.
@@ -83,27 +96,26 @@ void verboseTest() throws InterruptedException {
8396

8497
@Test
8598
void structuredMetadataTest() throws InterruptedException {
86-
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withVerboseLogMonitor(true).withBasicAuth("user", "pass").start();
87-
ILogStream helloStream = loki.stream().info().l("topic", "structured_metadata").l("Number", 3).build();
99+
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withVerboseLogMonitor(true).withBasicAuth("user", "pass").open();
100+
ILogStream helloStream = loki.stream().info().l("topic", "structuredMetadataTest").l("Number", 3).open();
88101
helloStream.log("Hello world!", Labels.of("struct", "custom label"));
89102
loki.closeSync();
90103
}
91104

92105
@Test
93106
void logLevelTest() throws InterruptedException {
94-
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withVerboseLogMonitor(true).withBasicAuth("user", "pass").start();
95-
loki.stream().critical().l("topic", "loglevel").build().log("Log level: critical");
96-
loki.stream().fatal().l("topic", "loglevel").build().log("Log level: fatal");
97-
loki.stream().warning().l("topic", "loglevel").build().log("Log level: warning");
98-
loki.stream().info().l("topic", "loglevel").build().log("Log level: info");
99-
loki.stream().debug().l("topic", "loglevel").build().log("Log level: debug");
100-
loki.stream().verbose().l("topic", "loglevel").build().log("Log level: verbose");
101-
loki.stream().trace().l("topic", "loglevel").build().log("Log level: trace");
102-
loki.stream().unknown().l("topic", "loglevel").build().log("Log level: unknown");
107+
TinyLoki loki = TinyLoki.withUrl("http://localhost:3100").withVerboseLogMonitor(true).withBasicAuth("user", "pass").open();
108+
loki.stream().critical().l("topic", "logLevelTest").open().log("Log level: critical");
109+
loki.stream().fatal().l("topic", "logLevelTest").open().log("Log level: fatal");
110+
loki.stream().warning().l("topic", "logLevelTest").open().log("Log level: warning");
111+
loki.stream().info().l("topic", "logLevelTest").open().log("Log level: info");
112+
loki.stream().debug().l("topic", "logLevelTest").open().log("Log level: debug");
113+
loki.stream().verbose().l("topic", "logLevelTest").open().log("Log level: verbose");
114+
loki.stream().trace().l("topic", "logLevelTest").open().log("Log level: trace");
115+
loki.stream().unknown().l("topic", "logLevelTest").open().log("Log level: unknown");
103116
loki.closeSync();
104117
}
105118

106-
107119
static class MassiveThread extends Thread {
108120
static final public String LABEL_STREAM_IDX = "test_stream_index";
109121

@@ -142,10 +154,10 @@ void sampleMassiveTest() throws InterruptedException {
142154
final int testRunId = random.nextInt(1000);
143155
System.out.println("Running test: " + testRunId);
144156

145-
try (TinyLoki loki = TinyLoki.withUrl("http://localhost:3100/loki/api/v1/push").withVerboseLogMonitor(false).withExecutor(new ThreadExecutor(100000)).start()) {
157+
try (TinyLoki loki = TinyLoki.withUrl("http://localhost:3100/loki/api/v1/push").withVerboseLogMonitor(false).withExecutor(new ThreadExecutor(100000)).open()) {
146158

147159
for (int i = 0; i < 100; ++i) {
148-
streams.add(loki.stream().l("test_name", "sampleMassiveTest").l("test_run_id", "test_" + testRunId).l(MassiveThread.LABEL_STREAM_IDX, "index_" + i).build());
160+
streams.add(loki.stream().l("test_name", "sampleMassiveTest").l("test_run_id", "test_" + testRunId).l(MassiveThread.LABEL_STREAM_IDX, "index_" + i).open());
149161
}
150162

151163
for (int i = 0; i < 100; ++i) {

src/test/java/pl/mjaron/tinyloki/LogControllerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ class LogControllerTest {
1414
@Test
1515
void getLogMonitor() {
1616
final ILogMonitor logMonitor = new SilentLogMonitor();
17-
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogMonitor(logMonitor).start();
17+
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogMonitor(logMonitor).open();
1818
assertSame(logMonitor, controller.getLogMonitor());
1919

2020
}
2121

2222
@Test
2323
void getLogCollector() {
2424
final ILogCollector collector = new JsonLogCollector();
25-
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogCollector(collector).start();
25+
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogCollector(collector).open();
2626
assertSame(collector, controller.getLogCollector());
2727
}
2828

2929
@Test
3030
void getExecutor() {
3131
final IExecutor executor = new ThreadExecutor();
32-
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withExecutor(executor).start();
32+
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withExecutor(executor).open();
3333
assertSame(executor, controller.getExecutor());
3434
}
3535

3636
@Test
3737
void createStreamFromMap() throws InterruptedException {
3838
MemoryLogSender logSender = new MemoryLogSender();
39-
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogSender(logSender).start();
39+
final TinyLoki controller = TinyLoki.withUrl("http://example.com").withLogSender(logSender).open();
4040
Map<String, String> map = new HashMap<>();
4141
map.put("aaa", "bbb");
4242
ILogStream stream = controller.createStream(map);

src/test/java/pl/mjaron/tinyloki/SyncExecutorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class SyncExecutorTest {
1010
@Test
1111
void basic() throws InterruptedException {
1212

13-
TinyLoki controller = TinyLoki.withUrl("http://example.com").withExecutor(new SyncExecutor()).withLogSender(new DummyLogSender(100)).withVerboseLogMonitor().start();
14-
ILogStream stream = controller.stream().l("color", "white").build();
13+
TinyLoki controller = TinyLoki.withUrl("http://example.com").withExecutor(new SyncExecutor()).withLogSender(new DummyLogSender(100)).withVerboseLogMonitor().open();
14+
ILogStream stream = controller.stream().l("color", "white").open();
1515
stream.log("My first log.");
1616
assertTrue(controller.closeSync());
1717
}

0 commit comments

Comments
 (0)