pushing metric to otel collector

In this example, we will be pushing or in better term exporting metric to our otel-collector. Generally there are a couple of approach for otel collector to obtain metrics data

1. push - this is where application push metric data to it 

2. pull - this is where it scrap for metrics 

3. others


In this example, we will be setting up for scenario 1 - where app will push metrics. Let's setup our otel collector first


Here is our config.

# collector-config.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318

exporters:
  debug:
    verbosity: detailed

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [debug]
    metrics:
      receivers: [otlp]
      exporters: [debug]
    logs:
      receivers: [otlp]
      exporters: [debug]

And running our docker command here:- 



docker run -p 4317:4317 -p 4318:4318 --rm -v $PWD/collector-config.yaml:/etc/otelcol/
config.yaml otel/opentelemetry-collector



Next we will create our payload 


$json = @'
{
  "resourceMetrics": [
    {
      "resource": {},
      "scopeMetrics": [
        {
          "scope": {},
          "metrics": [
            {
              "name": "cpu_usage",
              "gauge": {
                "dataPoints": [
                  {
                    "asDouble": 42.5,
                    "attributes": [
                      {
                        "key": "host",
                        "value": {
                          "stringValue": "server1"
                        }
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      ]
    }
  ]
}
'@

Finally run the following command :-


Invoke-WebRequest -Uri "http://localhost:4318/v1/metrics" `
  -Method POST `
  -Headers @{ "Content-Type" = "application/json" } `
  -Body $json

And we will get the following outputs







Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20