|
| 1 | +# Code Engine custom metrics examples |
| 2 | + |
| 3 | +The following samples demonstrate how to emit custom metrics in Code Engine jobs and apps. |
| 4 | + |
| 5 | +Use the following command to build and deploy all examples to the Code Engine project of your choice. |
| 6 | +``` |
| 7 | +ibmcloud ce project select --name <your-project-name> |
| 8 | +
|
| 9 | +./run all |
| 10 | +``` |
| 11 | + |
| 12 | +## Metrics |
| 13 | + |
| 14 | +The application provided in this example expose Prometheus metrics at `/metrics` (port 2112). All metric names are prefixed with a configurable value set via the `METRICS_NAME_PREFIX` environment variable (default: `mymetrics_`). |
| 15 | + |
| 16 | + |
| 17 | +Once custom metrics scraping is enabled (see asset [metrics-collector](../metrics-collector/README.md)), the following command can be used to import the "My custom Code Engine Metrics" dashboard into IBM Cloud Monitoring: |
| 18 | + |
| 19 | +```bash |
| 20 | +# Load the custom metric dashboard configuration |
| 21 | +CE_CUSTOM_METRICS_DASHBOARD=$(curl -sL https://raw.githubusercontent.com/IBM/CodeEngine/main/metrics-examples/my-custom-code-engine-metrics-dashboard.json) |
| 22 | + |
| 23 | +# Import the dashboard |
| 24 | +curl -X POST https://$REGION.monitoring.cloud.ibm.com/api/v3/dashboards \ |
| 25 | + -H "Authorization: $(ibmcloud iam oauth-tokens --output JSON|jq -r '.iam_token')" \ |
| 26 | + -H "IBMInstanceID: $MONITORING_INSTANCE_GUID" \ |
| 27 | + -H "Content-Type: application/json" \ |
| 28 | + -d "{\"dashboard\": $CE_CUSTOM_METRICS_DASHBOARD}" |
| 29 | +``` |
| 30 | + |
| 31 | +To customize the prefix, set the environment variable when starting the application: |
| 32 | + |
| 33 | +```bash |
| 34 | +METRICS_NAME_PREFIX=myapp_ node app.mjs |
| 35 | +``` |
| 36 | + |
| 37 | +On Code Engine set the environment variable in the application configuration: |
| 38 | + |
| 39 | +```bash |
| 40 | +ibmcloud ce app update "metrics-example-app-node" --env METRICS_NAME_PREFIX=myapp_ |
| 41 | +``` |
| 42 | + |
| 43 | +Following metrics are emitted by the metrics-example-app-node: |
| 44 | + |
| 45 | +**Request Metrics** |
| 46 | +- `mymetrics_requests_total`: Total requests by method and path |
| 47 | + |
| 48 | +**Outbound Call Metrics** |
| 49 | +- `mymetrics_outbound_request_duration_seconds`: Histogram of outbound request durations |
| 50 | +- `mymetrics_outbound_requests_total`: Total outbound requests by target, method, and status |
| 51 | + |
| 52 | +**Database Metrics** |
| 53 | +- `mymetrics_db_query_duration_seconds`: Histogram of query durations by operation and table |
| 54 | +- `mymetrics_db_queries_total`: Total queries by operation, table, and status |
| 55 | +- `mymetrics_db_connections_active`: Active database connections gauge |
| 56 | + |
| 57 | +**Compute Metrics** |
| 58 | +- `mymetrics_compute_duration_seconds`: Histogram of compute operation durations |
| 59 | + |
| 60 | + |
| 61 | +## Put some load on your app |
| 62 | + |
| 63 | +## Load Testing |
| 64 | + |
| 65 | +Generate test traffic using the included script: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Local testing |
| 69 | +./load-test.sh |
| 70 | + |
| 71 | +# IBM Cloud Code Engine deployment |
| 72 | +TARGET_URL=https://your-app.example.com ./load-test.sh |
| 73 | + |
| 74 | +# Custom configuration |
| 75 | +TARGET_URL=https://your-app.example.com DURATION=120 CONCURRENT_REQUESTS=10 ./load-test.sh |
| 76 | +``` |
| 77 | + |
| 78 | +Configuration options: |
| 79 | +- `TARGET_URL`: Application endpoint (default: http://localhost:8080) |
| 80 | +- `DURATION`: Test duration in seconds (default: 60) |
| 81 | +- `CONCURRENT_REQUESTS`: Number of concurrent workers (default: 5) |
| 82 | + |
| 83 | + |
| 84 | +### Deploying httpbin Backend |
| 85 | + |
| 86 | +To deploy your own httpbin instance on IBM Cloud Code Engine instead of using the public service, use the following command with an image from a registry other than docker.io: |
| 87 | + |
| 88 | +```bash |
| 89 | +ibmcloud ce application update \ |
| 90 | + --name httpbin \ |
| 91 | + --src https://github.com/mark-sivill/httpbin \ |
| 92 | + --memory 0.5G \ |
| 93 | + --cpu 0.25 \ |
| 94 | + --min-scale 1 \ |
| 95 | + --max-scale 3 \ |
| 96 | + --concurrency 100 \ |
| 97 | + --port 9000 |
| 98 | +``` |
| 99 | + |
| 100 | +After deployment, get the application URL: |
| 101 | + |
| 102 | +```bash |
| 103 | +ibmcloud ce application get --name httpbin --output url |
| 104 | +``` |
| 105 | + |
| 106 | +Then configure the network-test-app to use your httpbin instance: |
| 107 | + |
| 108 | +```bash |
| 109 | +ibmcloud ce application update \ |
| 110 | + --name network-test-app \ |
| 111 | + --env HTTPBIN_BASE_URL=https://httpbin.your-project.us-south.codeengine.appdomain.cloud |
| 112 | +``` |
| 113 | + |
| 114 | +The httpbin image from GitHub Container Registry (ghcr.io) is the official Postman-maintained implementation that works well in Code Engine environments. |
0 commit comments