github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/clients/k6/log-generation.md (about)

     1  ---
     2  title: Log generation
     3  weight: 10
     4  ---
     5  # Log generation
     6  
     7  ## Using `pushParameterized`
     8  
     9  Push logs to Loki with `pushParameterized`.
    10  This method generates batches of streams in a random fashion.
    11  This method requires three arguments:
    12  
    13  | name | description |
    14  | ---- | ----------- |
    15  | `streams` | number of streams per batch |
    16  | `minSize` | minimum batch size in bytes |
    17  | `maxSize` | maximum batch size in bytes |
    18  
    19  **Javascript example code fragment:**
    20  
    21  ```javascript
    22  import loki from 'k6/x/loki';
    23  
    24  const KB = 1024;
    25  const MB = KB * KB;
    26  
    27  const conf = loki.Config("http://localhost:3100");
    28  const client = loki.Client(conf);
    29  
    30  export default () => {
    31     client.pushParameterized(2, 500 * KB, 1 * MB);
    32  };
    33  ```
    34  
    35  ### Argument `streams`
    36  
    37  The first argument of the method is the desired amount of streams per batch.
    38  Instead of using a fixed amount of streams in each call, you can randomize the
    39  value to simulate a more realistic scenario.
    40  
    41  **Javascript example code fragment:**
    42  
    43  ```javascript
    44  function randomInt(min, max) {
    45    return Math.floor(Math.random() * (max - min + 1) + min);
    46  };
    47  
    48  export default () => {
    49     let streams = randomInt(2, 8);
    50     client.pushParameterized(streams, 500 * KB, 1 * MB);
    51  }
    52  ```
    53  
    54  ### Arguments `minSize` and `maxSize`
    55  
    56  The second and third argument of the method take the lower and upper bound of
    57  the batch size. The resulting batch size is a random value between the two
    58  arguments. This mimics the behaviour of a log client, such as Promtail or
    59  the Grafana Agent, where logs are buffered and pushed once a certain batch size
    60  is reached or after a certain size when no logs have been received.
    61  
    62  The batch size is not equal to the payload size, as the batch size only counts
    63  bytes of the raw logs. The payload may be compressed when Protobuf encoding
    64  is used.
    65  
    66  ## Log format
    67  
    68  `xk6-loki` can emit log lines in seven distinct formats. The label `format` of
    69  a stream defines the format of its log lines.
    70  
    71  * Apache common (`apache_common`)
    72  * Apache combined (`apache_combined`)
    73  * Apache error (`apache_error`)
    74  * [BSD syslog](https://datatracker.ietf.org/doc/html/rfc3164) (`rfc3164`)
    75  * [Syslog](https://datatracker.ietf.org/doc/html/rfc5424) (`rfc5424`)
    76  * JSON (`json`)
    77  * [logfmt](https://pkg.go.dev/github.com/kr/logfmt) (`logfmt`)
    78  
    79  Under the hood, the extension uses a fork the library
    80  [flog](https://github.com/mingrammer/flog) for generating log lines.
    81  
    82  ## Labels
    83  
    84  `xk6-loki` uses the following label names for generating streams:
    85  
    86  | name      | type     | cardinality     |
    87  | --------- | -------- | --------------- |
    88  | instance  | fixed    | 1 per k6 worker |
    89  | format    | fixed    | 7               |
    90  | os        | fixed    | 3               |
    91  | namespace | variable | >100            |
    92  | app       | variable | >100            |
    93  | pod       | variable | >100            |
    94  | language  | variable | >100            |
    95  | word      | variable | >100            |
    96  
    97  By default, variable labels are not used.
    98  However, you can specify the
    99  cardinality (quantity of distinct label values) using the `cardinality` argument
   100  in the `Config` constructor.
   101  
   102  **Javascript example code fragment:**
   103  ```javascript
   104  import loki from 'k6/x/loki';
   105  
   106  const cardinality = {
   107     "app": 1,
   108     "namespace": 2,
   109     "language": 2,
   110     "pod": 5,
   111  };
   112  const conf = loki.Config("http://localhost:3100", 5000, 1.0, cardinality);
   113  const client = loki.Client(conf);
   114  ```
   115  
   116  The total quantity of distinct streams is defined by the cartesian product of
   117  all label values. Keep in mind that high cardinality negatively impacts the performance of
   118  the Loki instance.
   119  
   120  ## Payload encoding
   121  
   122  Loki accepts two kinds of push payload encodings: JSON and Protobuf.
   123  While JSON is easier for humans to read,
   124  Protobuf is optimized for performance
   125  and should be preferred when possible.
   126  
   127  To define the ratio of Protobuf to JSON requests, the client
   128  configuration accepts values of 0.0 to 1.0.
   129  0.0 means 100% JSON encoding, and 1.0 means 100% Protobuf encoding.
   130  
   131  The default value is 0.9.
   132  
   133  **Javascript example code fragment:**
   134  ```javascript
   135  import loki from 'k6/x/loki';
   136  
   137  const ratio = 0.8; // 80% Protobuf, 20% JSON
   138  const conf = loki.Config("http://localhost:3100", 5000, ratio);
   139  const client = loki.Client(conf);
   140  ```