@@ -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 ) {
0 commit comments