Moesif
Users can observe service integrations with Moesif, which is a powerful API analytics and monetization platform that helps businesses understand, debug, and monetize their API usage. It provides comprehensive API observability with real-time monitoring, behavioral analytics, and AI-powered insights to track customer API adoption and usage patterns.
Follow the steps given below to view integration metrics, traces and logs in Moesif.
Step 1: Create a Moesif Account and Get an Application ID
After you log into Moesif Portal, get your Moesif Application ID during the onboarding steps.
Application ID can be accessed by following the below steps from Moesif Portal after logging in.
Go to Account -> Settings -> API keys -> Collector Application ID.
Step 2: Set Up a Service Integration
-
In your integration, Navigate to file explorer.
-
Open the
main.balfile in the Ballerina package and add the following imports.import ballerinax/moesif as _; -
Create the
Config.tomlfile in the package directory to set the runtime configurations. -
To enable the extension and publish traces to Moesif, add the following to the
Config.toml.[ballerina.observe]tracingEnabled=truetracingProvider="moesif"[ballerinax.moesif]applicationId = "<MOESIF_APPLICATION_ID>" # Mandatory Configuration.reporterBaseUrl = "https://api.moesif.net" # Optional Configuration. Default value is 'https://api.moesif.net'tracingReporterFlushInterval = 1000 # Optional Configuration. Default value is 1000tracingReporterBufferSize = 10000 # Optional Configuration. Default value is 10000isTraceLoggingEnabled = false # Optional Configuration. Default value is falseisPayloadLoggingEnabled = false # Optional Configuration. Default value is false -
To enable metrics publishing, add the following to the
Config.toml.[ballerina.observe]metricsEnabled=truemetricsReporter="moesif"[ballerinax.moesif]applicationId = "<MOESIF_APPLICATION_ID>" # Mandatory Configuration.reporterBaseUrl = "https://api.moesif.net" # Optional Configuration. Default value is 'https://api.moesif.net'metricsReporterFlushInterval = 15000 # Optional Configuration. Default value is 15000metricsReporterClientTimeout = 10000 # Optional Configuration. Default value is 10000isTraceLoggingEnabled = false # Optional Configuration. Default value is falseisPayloadLoggingEnabled = false # Optional Configuration. Default value is falseidleTimePublishingEnabled = false # Optional Configuration. Default value is false# Additional attributes for metrics[ballerinax.moesif.additionalAttributes]key1 = "value1"key2 = "value2" -
Replace
<MOESIF_APPLICATION_ID>with the application ID obtained in Step 1.
The table below provides the descriptions of each configuration option and possible values that can be assigned.
| Configuration key | Description | Default value | Possible values |
|---|---|---|---|
| ballerina.observe.tracingEnabled | Enables or disables the collection of trace data. | false | true or false |
| ballerina.observe.tracingProvider | Specifies Moesif as the tracing provider. | none | "moesif" |
| ballerina.observe.metricsEnabled | Enables or disables the collection of metrics data. | false | true or false |
| ballerina.observe.metricsReporter | Specifies Moesif as the metrics reporter. | none | "moesif" |
| ballerinax.moesif.applicationId | Moesif application ID used for authentication. Mandatory configuration. | none | A valid Moesif application ID string |
| ballerinax.moesif.reporterBaseUrl | The base URL of the Moesif API. | https://api.moesif.net | Any valid Moesif API endpoint URL |
| ballerinax.moesif.tracingReporterFlushInterval | Interval (in milliseconds) for flushing trace data to Moesif. | 1000 | Any positive integer value |
| ballerinax.moesif.tracingReporterBufferSize | Maximum buffer size for trace data before sending to Moesif. | 10000 | Any positive integer value |
| ballerinax.moesif.metricsReporterFlushInterval | Interval (in milliseconds) for flushing metrics data to Moesif. | 15000 | Any positive integer value |
| ballerinax.moesif.metricsReporterClientTimeout | Timeout (in milliseconds) for the metrics reporter client requests. | 10000 | Any positive integer value |
| ballerinax.moesif.isTraceLoggingEnabled | Enables or disables trace logging for debugging purposes. | false | true or false |
| ballerinax.moesif.isPayloadLoggingEnabled | Enables or disables payload logging for debugging purposes. | false | true or false |
| ballerinax.moesif.idleTimePublishingEnabled | Enables or disables publishing metrics in idle time. | false | true or false |
| ballerinax.moesif.additionalAttributes | Additional key-value attributes to include with metrics reporting. | none | Any valid set of key-value pairs. e.g., key1="value1", key2="value2" |
These configurations enable traces and metrics publishing for the Ballerina application and configure the Moesif exporter.
Step 3: Publish Integration Logs to Moesif
This setup leverages Fluent Bit to forward logs to an OTEL Collector, which then sends the logs to Moesif's log endpoint.
Integration → Fluent Bit → OTEL Collector → Moesif
-
Copy the following configs into a local directory to set up containerized log publishing.
.├── docker-compose.yaml├── fluent-bit.conf└── otelcol.yaml- docker-compose.yaml – Container setup for Fluent Bit and OTEL Collector.
- fluent-bit.conf – Reads Ballerina logs and forwards them.
- otelcol.yaml – Processes logs and sends to Moesif.
docker-compose.yaml
Update the
<ballerina-log-path>with the log storage location, and<MOESIF_APPLICATION_ID>with the application ID obtained in Step 1.services:otelcol:image: otel/opentelemetry-collector-contrib:0.132.0container_name: otelcolcommand: ["--config", "/etc/otelcol.yaml"]environment:MOESIF_APP_ID: "<MOESIF_APPLICATION_ID>"ports:- "4317:4317"- "4318:4318"volumes:- ./otelcol.yaml:/etc/otelcol.yaml:ronetworks:- otelnetfluent-bit:image: fluent/fluent-bit:3.0container_name: fluent-bitdepends_on:- otelcolports:- "2020:2020"volumes:- ./fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf:ro# Mount the local log directory into the container- <ballerina-log-path>:/app/logs:ronetworks:- otelnetnetworks:otelnet:driver: bridgefluent-bit.conf
[SERVICE]Flush 1Log_Level debugDaemon offHTTP_Server OnHTTP_Listen 0.0.0.0HTTP_Port 2020# Read logs from local Ballerina app[INPUT]Name tailPath /app/logs/app.logTag ballerina.*Read_from_Head trueSkip_Long_Lines OnSkip_Empty_Lines OnRefresh_Interval 1# Add metadata[FILTER]Name modifyMatch ballerina.*Add service.name ballerina-serviceAdd deployment.environment prod# Convert to OTEL format and send to collector[OUTPUT]Name opentelemetryMatch ballerina.*Host otelcolPort 4318Logs_uri /v1/logsLog_response_payload TrueTls OffTls.verify Off# Debug output to see what's being processed[OUTPUT]Name stdoutMatch ballerina.*Format json_linesotelcol.yaml
Update the
<MOESIF_APPLICATION_ID>with the application ID obtained in Step 1.receivers:otlp:protocols:grpc:endpoint: "0.0.0.0:4317"http:endpoint: "0.0.0.0:4318"processors:resource:attributes:- key: service.namevalue: ballerina-serviceaction: upsert- key: deployment.environmentvalue: prodaction: upserttransform/severity_from_message:log_statements:- context: logstatements:# Set default severity to INFO for all logs first- set(severity_number, 9) where body != nil- set(severity_text, "INFO") where body != nil# Try to parse JSON body, but handle parsing errors gracefully- set(cache["is_json"], false)- set(cache["is_json"], true) where body != nil and IsMatch(body, "^\\s*\\{")# For JSON logs, parse and extract level- set(cache["parsed_body"], ParseJSON(body)) where cache["is_json"] == true# Override with specific levels based on JSON level field- set(severity_number, 1) where cache["is_json"] == true and cache["parsed_body"]["level"] == "TRACE"- set(severity_text, "TRACE") where cache["is_json"] == true and cache["parsed_body"]["level"] == "TRACE"- set(severity_number, 5) where cache["is_json"] == true and cache["parsed_body"]["level"] == "DEBUG"- set(severity_text, "DEBUG") where cache["is_json"] == true and cache["parsed_body"]["level"] == "DEBUG"- set(severity_number, 9) where cache["is_json"] == true and cache["parsed_body"]["level"] == "INFO"- set(severity_text, "INFO") where cache["is_json"] == true and cache["parsed_body"]["level"] == "INFO"- set(severity_number, 13) where cache["is_json"] == true and cache["parsed_body"]["level"] == "WARN"- set(severity_text, "WARN") where cache["is_json"] == true and cache["parsed_body"]["level"] == "WARN"- set(severity_number, 17) where cache["is_json"] == true and cache["parsed_body"]["level"] == "ERROR"- set(severity_text, "ERROR") where cache["is_json"] == true and cache["parsed_body"]["level"] == "ERROR"- set(severity_number, 21) where cache["is_json"] == true and cache["parsed_body"]["level"] == "FATAL"- set(severity_text, "FATAL") where cache["is_json"] == true and cache["parsed_body"]["level"] == "FATAL"batch: {}exporters:# OTLP over HTTP to Moesifotlphttp:endpoint: "https://api.moesif.net"logs_endpoint: "https://api.moesif.net/v1/logs"headers:X-Moesif-Application-Id: "<MOESIF_APPLICATION_ID>"compression: nonetimeout: 10ssending_queue:enabled: truenum_consumers: 2queue_size: 512retry_on_failure:enabled: trueinitial_interval: 1smax_interval: 10smax_elapsed_time: 0sservice:telemetry:logs:level: debugpipelines:logs:receivers: [otlp]processors: [resource, transform/severity_from_message, batch]exporters: [otlphttp] -
Run the above components stack using the following command.
docker compose up -
Create the
Config.tomlfile by navigating to file explorer with the following content to log to a file injsonformat.[ballerina.log]format = "json"[[ballerina.log.destinations]]# Replace /path/to/your/ballerina/logs with the absolute path to the Ballerina application's log directorypath = "/path/to/your/ballerina/logs/app.log"
Step 4: Run the Integration
When observability is enabled, the runtime collects metrics, logs, and traces.
Step 5: Send Requests
Send requests as below to the service.
Example cURL commands:
curl -X GET http://localhost:8090/shop/products
curl -X POST http://localhost:8090/shop/product \
-H "Content-Type: application/json" \
-d '{
"id": 4,
"name": "Laptop Charger",
"price": 50.00
}'
curl -X POST http://localhost:8090/shop/order \
-H "Content-Type: application/json" \
-d '{
"productId": 1,
"quantity": 1
}'
curl -X GET http://localhost:8090/shop/order/0
Step 6: Visualize Observability Data in Moesif Dashboards
Traces, metrics, and logs are published to Moesif as events and can be explored in the Live Event Log for real-time monitoring. Moesif provides a set of pre-built dashboards that help visualize and analyze this data effectively. In addition, custom dashboards can be created to gain deeper, domain-specific insights.
What's next
- New Relic — Alternative full-stack observability platform
- Datadog — Full-stack observability with Datadog
- Overview — Full observability architecture
Was this page helpful?