K6 prometheus exporter for cortex

Hello Team,

I am using k6 prometheus write extension to push metrics to cortex, As our cortex is using auth & need to pass X-Scope-OrgID . I can see the code supports it.

Howerver I need help to supply this information as a environment variable like K6_PROMETHEUS_REMOTE_URL .

How I can achieve same?

Hi @niravshah2705 , welcome to the community forum :tada:

I am not certain this is what you are asking, but if you just want access to env variables that is done through the __ENV object, so in your case __ENV.K6_PROMETHEUS_REMOTE_URL.

Is this what you wanted or did I misunderstand?

Thank you @mstoykov for your reply. I can setup exposed K6_PROMETHEUS_REMOTE_URL environment variable. & works well for me.

However I have to modify code & expose TenantName as environment variable, so that I dont have to change k6 scripts if I run locally or in our integrated environment.
Is there any default way to expose config via environment variables in plugins , I would like to do so too.

Regards,
Nirav Shah

However I have to modify code & expose TenantName as environment variable, so that I dont have to change k6 scripts if I run locally or in our integrated environment.

You can do exactly the same - set env variable TENANT_NAME and use it as __ENV.TENANT_NAME internally.

If you are using k6 cloud you will need to use the -e option as in that case we don’t want to send all your env variables to the cloud.

Is there any default way to expose config via environment variables in plugins , I would like to do so too.

I don’t know what you mean, but I guess the thing above? Although there are no such thing as plugins in k6. Do you mean extensions?

Hey @mstoykov , It does not work for me. Can you try if it is working as mentioned by you?

Yes, I mean extenstions. As I am not able to use mentioned environment variables. I think it has different pattern for naming conventions.

Hi @niravshah2705, can you please give some code sample that does not work for you?

All I am saying will 100% work and either you found some very complex bug or are doing a very small mistake somewhere. With a small code sample - in this case only the parts where you make the client, and how you run it - are enough, we will likely be able to debug it.

Hello @mstoykov

Please find steps I follow as below:

nirav@Niravs-MacBook-Pro ~/g/bin> export K6_PROMETHEUS_REMOTE_URL="https://cortex.dev.mycompany.com/api/v1/push"
nirav@Niravs-MacBook-Pro ~/g/bin> export TENANT_NAME="test"
nirav@Niravs-MacBook-Pro ~/g/bin> export K6_TENANT_NAME="test"
nirav@Niravs-MacBook-Pro ~/g/bin> ./k6_old run ~/testcase1.js -o output-prometheus-remote

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

INFO[0002] Prometheus: configuring remote-write with prometheus mapping 
  execution: local
     script: /Users/nirav/testcase1.js
     output: Output k6 metrics to prometheus remote-write endpoint

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations shared among 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)


running (00m01.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m01.0s/10m0s  1/1 shared iters

     █ Fetch a list of public crocodiles

       ✓ response status is 200.
       ✓ https://test-api.k6.io/public/crocodiles has valid json response
       ✓ number of crocs is greater than 4

   ✓ checks.........................: 100.00% ✓ 3        ✗ 0  
     data_received..................: 6.5 kB  6.4 kB/s
     data_sent......................: 638 B   625 B/s
     group_duration.................: avg=1s       min=1s       med=1s       max=1s       p(90)=1s       p(95)=1s      
     http_req_blocked...............: avg=281.44ms min=20µs     med=281.44ms max=562.86ms p(90)=506.57ms p(95)=534.71ms
     http_req_connecting............: avg=101.62ms min=0s       med=101.62ms max=203.25ms p(90)=182.92ms p(95)=193.08ms
     http_req_duration..............: avg=217.51ms min=211.14ms med=217.51ms max=223.89ms p(90)=222.62ms p(95)=223.25ms
       { expected_response:true }...: avg=217.51ms min=211.14ms med=217.51ms max=223.89ms p(90)=222.62ms p(95)=223.25ms
     http_req_failed................: 0.00%   ✓ 0        ✗ 2  
     http_req_receiving.............: avg=1.78ms   min=1.16ms   med=1.78ms   max=2.39ms   p(90)=2.27ms   p(95)=2.33ms  
     http_req_sending...............: avg=665µs    min=105µs    med=665µs    max=1.22ms   p(90)=1.11ms   p(95)=1.16ms  
     http_req_tls_handshaking.......: avg=104.7ms  min=0s       med=104.7ms  max=209.4ms  p(90)=188.46ms p(95)=198.93ms
     http_req_waiting...............: avg=215.07ms min=207.51ms med=215.07ms max=222.62ms p(90)=221.11ms p(95)=221.86ms
     http_reqs......................: 2       1.958484/s
     iteration_duration.............: avg=1s       min=1s       med=1s       max=1s       p(90)=1s       p(95)=1s      
     iterations.....................: 1       0.979242/s
     vus............................: 1       min=1      max=1
     vus_max........................: 1       min=1      max=1

ERRO[0004] Failed to store timeseries.                   error="server returned HTTP status 401 Unauthorized: no org id"
ERRO[0004] Failed to store timeseries.                   error="server returned HTTP status 401 Unauthorized: no org id"
ERRO[0004] Failed to store timeseries.                   error="server returned HTTP status 401 Unauthorized: no org id"

Note here: as cortex we have enabled auth with below, it expects client_name environment variable:
config:
auth_enabled: true

We are getting 401 as the variables defined are not passed to the command.

I don’t see a code sample @niravshah2705 so there is nothing for me to check. Please provide some code sample that reproduces the issue for you - you can remove the names or tokens as needed.

@mstoykov Thanks for help. Now I figured out pattern of parameter passing for the extensions.

./k6 run ~/testcase1.js -o ‘output-prometheus-remote=url=https://cortex.dev.mycompany.com/api/v1/push,headers.X-Scope-OrgID=k6
or

nirav@Niravs-MacBook-Pro ~/g/bin> export K6_PROMETHEUS_REMOTE_URL=“https://cortex.dev.mycompany.com/api/v1/push
nirav@Niravs-MacBook-Pro ~/g/bin> ./k6 run ~/testcase1.js -o ‘output-prometheus-remote=headers.X-Scope-OrgID=k6’ -v

or
export K6_PROMETHEUS_REMOTE_URL=“https://cortex.dev.mycompany.com/api/v1/push
env ‘K6_PROMETHEUS_HEADERS_X-Scope-OrgID=k6’ ./k6 run ~/testcase1.js -o output-prometheus-remote

All above worked for me. I was looking for wrong extension code, hence expecting wrong parameter !!