/**
  * @dataProvider samplingDataProvider
  */
 public function testSampling($data, $sampleRate, $seed, $expectWrite)
 {
     $sender = $this->getMock('Liuggio\\StatsdClient\\Sender\\SenderInterface');
     $sender->expects($this->any())->method('open')->will($this->returnValue(true));
     if ($expectWrite) {
         $sender->expects($this->once())->method('write')->with($this->anything(), $this->equalTo($data));
     } else {
         $sender->expects($this->never())->method('write');
     }
     mt_srand($seed);
     $client = new SamplingStatsdClient($sender);
     $client->send($data, $sampleRate);
 }
 public function testSetSamplingRates()
 {
     $matching = new StatsdData();
     $matching->setKey('foo.bar');
     $matching->setValue(1);
     $nonMatching = new StatsdData();
     $nonMatching->setKey('oof.bar');
     $nonMatching->setValue(1);
     $sender = $this->getMock('Liuggio\\StatsdClient\\Sender\\SenderInterface');
     $sender->expects($this->any())->method('open')->will($this->returnValue(true));
     $sender->expects($this->once())->method('write')->with($this->anything(), $this->equalTo($nonMatching));
     $client = new SamplingStatsdClient($sender);
     $client->setSamplingRates(['foo.*' => 0.2]);
     mt_srand(0);
     // next random is 0.44
     $client->send($matching);
     mt_srand(0);
     $client->send($nonMatching);
 }
Exemple #3
0
/**
 * @todo document
 */
function wfLogProfilingData()
{
    global $wgDebugLogGroups, $wgDebugRawPage;
    $context = RequestContext::getMain();
    $request = $context->getRequest();
    $profiler = Profiler::instance();
    $profiler->setContext($context);
    $profiler->logData();
    $config = $context->getConfig();
    if ($config->get('StatsdServer')) {
        try {
            $statsdServer = explode(':', $config->get('StatsdServer'));
            $statsdHost = $statsdServer[0];
            $statsdPort = isset($statsdServer[1]) ? $statsdServer[1] : 8125;
            $statsdSender = new SocketSender($statsdHost, $statsdPort);
            $statsdClient = new SamplingStatsdClient($statsdSender, true, false);
            $statsdClient->send($context->getStats()->getBuffer());
        } catch (Exception $ex) {
            MWExceptionHandler::logException($ex);
        }
    }
    # Profiling must actually be enabled...
    if ($profiler instanceof ProfilerStub) {
        return;
    }
    if (isset($wgDebugLogGroups['profileoutput']) && $wgDebugLogGroups['profileoutput'] === false) {
        // Explicitly disabled
        return;
    }
    if (!$wgDebugRawPage && wfIsDebugRawPage()) {
        return;
    }
    $ctx = array('elapsed' => $request->getElapsedTime());
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ctx['forwarded_for'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ctx['client_ip'] = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (!empty($_SERVER['HTTP_FROM'])) {
        $ctx['from'] = $_SERVER['HTTP_FROM'];
    }
    if (isset($ctx['forwarded_for']) || isset($ctx['client_ip']) || isset($ctx['from'])) {
        $ctx['proxy'] = $_SERVER['REMOTE_ADDR'];
    }
    // Don't load $wgUser at this late stage just for statistics purposes
    // @todo FIXME: We can detect some anons even if it is not loaded.
    // See User::getId()
    $user = $context->getUser();
    $ctx['anon'] = $user->isItemLoaded('id') && $user->isAnon();
    // Command line script uses a FauxRequest object which does not have
    // any knowledge about an URL and throw an exception instead.
    try {
        $ctx['url'] = urldecode($request->getRequestURL());
    } catch (Exception $ignored) {
        // no-op
    }
    $ctx['output'] = $profiler->getOutput();
    $log = LoggerFactory::getInstance('profileoutput');
    $log->info("Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx);
}