Symfony SDK Guide
To integrate your Symfony application with monoscope, you need to use this SDK to monitor incoming traffic, aggregate the requests, and then send them to monoscope’s servers. Follow this guide to set up the Symfony SDK.
Prerequisites
You need a monoscope project and its API key. If you do not have them yet, follow Get started.
Set Up With Your Coding Agent
Your coding agent can add the instrumentation for you. Install the monoscope skills and the monoscope CLI:
# Claude Code
claude plugin marketplace add monoscope-tech/skills
claude plugin install monoscope-skills@monoscope-skills
# Cursor, Codex, Cline and other agents
npx skills add monoscope-tech/skills
# monoscope CLI
curl https://monoscopehq.com/install.sh | sh
monoscope auth login
Then ask your agent to instrument the project. In Claude Code, run:
/monoscope-skills:instrument Add OpenTelemetry for monoscope to this project
The skill uses the CLI to add the SDK and to make sure that events arrive. To do the setup by hand, follow the steps below. For help from a person, email us.
Installation
Run the command below to install the SDK:
composer require apitoolkit/apitoolkit-symfony:dev-master
Note
The package has no tagged release yet, so require the dev-master version.
OpenTelemetry Configuration
The Symfony bundle does not use OpenTelemetry. It sends requests to monoscope directly with your API key. If you add your own OpenTelemetry spans, as in Non-HTTP Entry Points below, set these environment variables to configure the exporter:
OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.monoscope.tech:4317"
OTEL_EXPORTER_OTLP_PROTOCOL="grpc"
OTEL_RESOURCE_ATTRIBUTES="x-api-key=YOUR_API_KEY"
Configuration
First, add the MONOSCOPE_API_KEY environment variable to your .env file:
MONOSCOPE_API_KEY=YOUR_API_KEY
Then add the APIToolkit\EventSubscriber\APIToolkitService listener, the API key and the monoscope URL to your config/services.yaml file:
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
locale: "en"
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# Initialize the monoscope client
APIToolkit\EventSubscriber\APIToolkitService:
arguments:
$apiKey: "%env(MONOSCOPE_API_KEY)%"
$rootURL: "https://app.monoscope.tech"
# Request and response bodies are not captured unless enabled here.
$captureRequestBody: false
$captureResponseBody: false
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: "../src/"
exclude:
- "../src/DependencyInjection/"
- "../src/Entity/"
- "../src/Kernel.php"
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
Tip
Replace YOUR_API_KEY with an API key from Settings > API Keys in monoscope.
Redacting Sensitive Data
<div class=”callout”> <i class=”fa-solid fa-circle-info”></i> <p><code>authorization</code>, <code>cookie</code>, <code>set-cookie</code> and <code>proxy-authorization</code> headers are always redacted, on requests and responses and on outgoing calls, whatever you configure. Header names match case-insensitively.</p> </div>
If you have fields that are sensitive and should not be sent to monoscope servers, you can mark those fields to be redacted (the fields will never leave your servers).
To redact fields, add these arguments to the service configuration:
-
$redactHeaders: A list of HTTP header keys. -
$redactRequestBody: A list of JSONPaths from the request body. -
$redactResponseBody: A list of JSONPaths from the response body.
JSONPath is a query language used to select and extract data from JSON files. For example, given the following sample user data JSON object:
{
"user": {
"name": "John Martha",
"email": "[email protected]",
"addresses": [
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
],
"credit_card": {
"number": "4111111111111111",
"expiration": "12/28",
"cvv": "123"
}
}
}
Examples of valid JSONPath expressions would be:
| JSONPath | Description |
|---|---|
$.user.addresses[*].zip |
monoscope replaces the zip field in all the objects of the addresses list inside the user object with the string [CLIENT_REDACTED]. |
$.user.credit_card |
monoscope replaces the entire credit_card object inside the user object with the string [CLIENT_REDACTED]. |
Tip
To learn more about JSONPaths, please take a look at the official docs or use this JSONPath Evaluator to validate your JSONPath expressions.
You can also use our JSON Redaction Tool to preview what the final data sent from your API to monoscope will look like, after redacting any given JSON object.
Here is the config/services.yaml configuration with redacted fields:
services:
APIToolkit\EventSubscriber\APIToolkitService:
arguments:
$apiKey: "%env(MONOSCOPE_API_KEY)%"
$rootURL: "https://app.monoscope.tech"
$redactHeaders:
- "content-type"
- "Authorization"
- "HOST"
$redactRequestBody:
- "$.user.email"
- "$.user.addresses"
- "$.user.addresses[*]"
$redactResponseBody:
- "$.users[*].email"
- "$.users[*].credit_card"
Note
- The
$redactHeadersconfig field expects a list of case-insensitive headers as strings. - The
$redactRequestBodyand$redactResponseBodyconfig fields expect a list of JSONPaths as strings. - The list of items to be redacted will be applied to all endpoint requests and responses on your server.
Non-HTTP Entry Points (Background Jobs, Workers, CLIs)
The Symfony bundle only covers HTTP requests. Symfony Messenger handlers (messenger:consume), scheduled commands, and Console commands are invisible until you wrap each handler in a span yourself. Always cover these alongside your HTTP routes.
For Messenger, register a middleware that wraps each message-handling pipeline in a span:
namespace App\Messenger;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\API\Trace\SpanKind;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\BusNameStamp;
class TraceMiddleware implements MiddlewareInterface {
public function handle(Envelope $envelope, StackInterface $stack): Envelope {
$tracer = Globals::tracerProvider()->getTracer('my-service-worker');
$messageClass = get_class($envelope->getMessage());
$bus = $envelope->last(BusNameStamp::class)?->getBusName() ?? 'messenger';
$span = $tracer->spanBuilder($messageClass)
->setSpanKind(SpanKind::KIND_CONSUMER)
->setAttribute('messaging.system', 'symfony.messenger')
->setAttribute('messaging.operation', 'process')
->setAttribute('messaging.destination.name', $bus)
->setAttribute('code.function', $messageClass)
->startSpan();
$scope = $span->activate();
try {
$envelope = $stack->next()->handle($envelope, $stack);
$span->setStatus(StatusCode::STATUS_OK);
return $envelope;
} catch (\Throwable $e) {
$span->recordException($e);
$span->setStatus(StatusCode::STATUS_ERROR);
throw $e;
} finally {
$scope->detach();
$span->end();
}
}
}
Wire it into config/packages/messenger.yaml under framework.messenger.buses.<bus>.middleware. For Console commands and scheduled tasks, wrap the body of execute() the same way and call Globals::tracerProvider()->shutdown() before returning so the BatchSpanProcessor flushes; otherwise spans are dropped silently.
Next Steps
- Send a few requests to your application. Then open Log Explorer in monoscope to see the requests, logs and traces.
- Find your way around the dashboard: issues, monitors, dashboards and the API catalog.
- Investigate from your terminal or your agent with the CLI, the skills and the MCP server.
Explore the Symfony SDK