NAME

MFab::Plugins::Datadog - Mojolicious plugin for Datadog APM integration

SYNOPSIS

# In your Mojolicious application
push(@{ $self->plugins->namespaces }, 'MFab::Plugins');

$app->plugin('Datadog', {
    enabled => "true",
    service => "MyApp",
    serviceEnv => "production"
});

DESCRIPTION

This module provides seamless integration between Mojolicious web applications and Datadog's Application Performance Monitoring (APM) system. It automatically instruments your Mojolicious application to send distributed traces and metrics to Datadog, giving you visibility into:

  • HTTP request/response cycles

  • Route matching and dispatch timing

  • Controller action execution

  • Custom application spans

The plugin automatically propagates trace context across service boundaries using Datadog's trace headers, enabling distributed tracing across your microservices architecture.

CUSTOM SPANS

When you want to measure a specific function, you can use the startSpan/endSpan functions to mark the start and end points:

sub customHandler {
    my($c) = @_;
    my $span = startSpan($c->tx, "TestCode::customHandler", "customHandler");
    # do work here
    sleep(10);
    endSpan($span);
}

NOTES

  • If the worker is killed through a heartbeat failure, the spans for that worker won't be sent

  • Websockets only generate a mojolicious-transaction span

FUNCTIONS

register()

Register Mojolicious plugin and hook into the application - called by the Mojolicious framework

It accepts the following config items

  • "datadogHost" - the datadog agent host, also looks in the ENV for "DD_AGENT_HOST", defaults to "localhost"

  • "enabled" - if "true", traces are sent to datadog, also looks in the ENV for "DD_TRACE_ENABLED", defaults to "false"

  • "service" - the value to send to datadog for the service name, defaults to "MFab::Plugins::Datadog"

  • "serviceEnv" - the value to send to datadog for the service environment, defaults to "test"

datadogId()

Generate a 64 bit integer that JSON::XS will serialize as an integer

configItem($config_host)

Get the config item from: the app setting, the environment variable, or use the default

setTraceId($c, $connection_data)

Set the traceid in the connection data

aroundActionHook()

The around_action hook - wrapped around the action

afterDispatchHook()

The after_dispatch hook - called after the request is finished the sync stage of processing, more async processing can happen after this

afterBuildTxHook()

The after_build_tx hook - called after the transaction is built but before it is parsed

startSpan($tx, $name, $resource, [$parent_id])

Start a new span, associates it to the transaction via $tx

endSpan($span, [$error_message])

End a span, optional error message

timestampToDatadog($timestamp)

Datadog wants number of nanoseconds since the epoch

durationToDatadog($duration)

Datadog wants duration in nanoseconds

submitDatadog($app, $connection_data, $args)

Submit spans to datadog agent