Generate post with json body containing dynamic values

I’ve been looking at using k6 to generate otlp requests for logs metrics and traces against our opentelemetry-collector / lgtm stack.

Is it possible to have a json object and generate certain strings per request? For example the below is the actual request we use in gatling. Any #{foo} is being replaced with a dynamic value per request. Our hope is to migrate to using k6 for these requests.

The code in gatling to populate the variables used in the json below:

  val customFeeder: Iterator[Map[String, Any]] = Iterator.continually(Map(
    "app_id" -> new Random().between(0, 300),
    "time" -> System.currentTimeMillis()
  ))
{
  "resourceLogs": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": {
              "stringValue": "loadtest_service_#{app_id}"
            }
          },
          {
            "key": "app",
            "value": {
              "stringValue": "loadtest.service"
            }
          }
        ]
      },
      "scopeLogs": [
        {
          "scope": {
            "name": "my.library",
            "version": "1.0.0",
            "attributes": [
              {
                "key": "loadtest.attribute",
                "value": {
                  "stringValue": "test attribute#{app_id}"
                }
              },
              {
                "key": "app",
                "value": {
                  "stringValue": "loadtest.service"
                }
              }
            ]
          },
          "logRecords": [
            {
              "timeUnixNano": "#{time}000000",
              "observedTimeUnixNano": "#{time}000000",
              "severityNumber": 10,
              "severityText": "Information",
              "traceId": "5b8efff798038103d269b633813fc60C",
              "spanId": "EEE19B7EC3C1B174",
              "body": {
                "stringValue": "Example log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log recordExample log record"
              },
              "attributes": [
                {
                  "key": "string.attribute",
                  "value": {
                    "stringValue": "some string"
                  }
                },
                {
                  "key": "boolean.attribute",
                  "value": {
                    "boolValue": true
                  }
                },
                {
                  "key": "int.attribute",
                  "value": {
                    "intValue": "10"
                  }
                },
                {
                  "key": "double.attribute",
                  "value": {
                    "doubleValue": 637.704
                  }
                },
                {
                  "key": "array.attribute",
                  "value": {
                    "arrayValue": {
                      "values": [
                        {
                          "stringValue": "many"
                        },
                        {
                          "stringValue": "values"
                        }
                      ]
                    }
                  }
                },
                {
                  "key": "map.attribute",
                  "value": {
                    "kvlistValue": {
                      "values": [
                        {
                          "key": "some.map.key",
                          "value": {
                            "stringValue": "some value"
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

I’ve looked at the k6 extensions which submit data to either loki mimir or tempo. Those extensions submit their data via the native protocol as opposed to otlp ( in this case json/http, ideally we would encode this in protobuf or whatever ). When I tried to use the trace generator I found what appears to be a bug where it just crashes after a little bit.

I’ve looked over the documentation and I’m not seeing anything obvious about how to accomplish this. Any help would be greatly appreciated!

Hi @bilschnice
This page from the docs shows multiple examples of an HTTP post with a stringified JSON payload. There also examples of parameterizing a larger JSON data set as a SharedArray object, but since your JSON is a single object, it seems most logical to define it directly within the script. To achieve the dynamic inline variables I believe you will need to use ${var} and template literals. Here are a couple of examples, look for the backticks.

For example:

...
"stringValue": `loadtest_service_${app_id}`
...
"observedTimeUnixNano": `${time}000000`,
...