PHP SDK

Instana PHP SDKは非推奨となりました。 2026年5月30日より、 Instana は正式にサポートを終了し、製品ライフサイクルの終了(EOL)となります。

Instana すべてのSDKユーザーの皆様に対し、アプリケーションを OTel SDKの Instana エクスポーターへ移行することを強く推奨します。 使用方法については、 「 OpenTelemetry PHP exporter(パブリック プレビュー) 」のドキュメントを参照してください。

PHP のトレース拡張機能には、最小限のSDKが付属しています。 この SDK を使用して、コードを手動でインスツルメンテーションすることができます。 この SDK を使用して作成されたスパンは、自動生成されたトレースに注入されます。 このSDKを使用すると、 Instana による検出されなかった例外の自動記録を補完するために、例外を手動で記録することもできます。

SDK のインストール

PHP SDKのスタブは、次のコマンドを使用してComposer経由でインストールできます:

composer require instana/instana-php-sdk

これらのスタブは、 PHP プロセスが Instana によって監視されていない場合、SDK APIへのすべての呼び出しがエラーではなく、 no-op正常に処理されるようにします。

お使いのIDEで利用するための PHP SDKのスタブは、Packagist で入手可能です。

SDK の使用例

$tracer = new \Instana\Tracer();

// sets the globaly service name without having to use the INSTANA_SERVICE_NAME variable
$tracer->setServiceName('my-service');
$span = $tracer->createSpan('foo');
$span->annotate('function', 'doSomething');
try {
    doSomething();
} catch (\Exception $e) {
    $tracer->logException($e);
    $span->markError();
} finally {
    $span->stop();
}

カスタムENTRYおよびEXITスパンを作成する

PHP のトレース拡張機能を使用すると、ENTRY型またはEXIT型のカスタムスパンを作成できます。

// get the tracer
$tracer = new \Instana\Tracer();

$entry_span = $tracer->createSpan('entry', \Instana\SPAN_ENTRY);

$exit_span = $tracer->createSpan('exit', \Instana\SPAN_EXIT);

Setting tags

// get the tracer
$tracer = new \Instana\Tracer();
$entry_span = $tracer->createSpan('entry', \Instana\SPAN_ENTRY);
$entry_span->annotate('service', 'my service')

親スパンの設定

PHP SDKを使用して新しいスパンを作成する際、親スパンのIDを設定することができます。これは、他のシステムによって作成された継続トレースにおいて役立つ場合があります。 以下の例は、関連するコード・スニペットがどのように表示されるかを示しています。

// get the tracer
$tracer = new \Instana\Tracer();

$span = $tracer->createSpan('entry', \Instana\SPAN_INTERMEDIATE, $parentId);

トレースの継続を設定する

PHP のトレース拡張機能を使用すると、メッセージングシステムを介してトレースの継続を設定できます。 これを行うには、パブリッシャーのトレース・コンテキストをメッセージに挿入し、コンシューマー・サイドで抽出する必要があります。

パブリッシャー

パブリッシャーのトレース・コンテキストをメッセージに注入するには、次のコマンドを実行します。

// get the tracer
$tracer = new \Instana\Tracer();

// GENERIC: an artificial message you want to send to your queue
$message = ['task' => 'convert-image'];

// retrieve the current trace context
$context = $tracer->getActiveContext();

// GENERIC: inject the current active context into your message, so you can retrieve it in the worker later
$attributes = $message['X-INSTANA-S'] = $context->getSpanId();
$attributes = $message['X-INSTANA-T'] = $context->getTraceId();

// send your message to the queue
$queue->publish($message);

コンシューマー

コンシューマー側でパブリッシャーのトレース・コンテキストを抽出するには、次のコマンドを実行します。

// get the tracer
$tracer = new \Instana\Tracer();

// GENERIC: pull your message from the queue
$message = $queue->pull();

// GENERIC: extract trace context so we can continue the trace, assumes it's always present
$messageContext = new \Instana\TraceContext($message['X-INSTANA-T'], $message['X-INSTANA-S']);

// continue the trace that published the message
$tracer->continueTrace($messageContext);

// OPTIONAL: Create a span to wrap the unit of work
$span = $tracer->createSpan('work');
$span->annotate('job', 'my-important-job');

// do the actual work

// stop the optional span started above
$span->stop();

Google Cloud Pub/Sub を使用したトレースの継続設定

PHP のトレース拡張機能は、 Google Cloud Pub/Sub とのアウトバウンドのやり取りに対して、自動的にEXITスパンを作成します。

トレースを継続するには、パブリッシュ/サブスクライブ・メッセージをコンシュームするコードを手動でラップする必要があります。

use Google\Cloud\PubSub\PubSubClient;

$tracer = new \Instana\Tracer();

$pubSub = new PubSubClient([
    'projectId' => $myProjectname
]);

$topic = $pubSub->topic($myTopicName);
$subscription = $pubSub->subscription($mySubscriptionName, $myTopicName);

$messages = $subscription->pull();
foreach ($messages as $message) {
    $attributes = $message->attributes();

    // extract the trace continouation headers from your message, assumes it's always present
    $messageContext = new \Instana\TraceContext($attributes['x-instana-t'], $attributes['x-instana-s']);

    // continue the trace that published the message
    $tracer->continueTrace($messageContext);

    // convert the current span
    $span = $tracer->getEntrySpan();
    $span->asGCPubSubReceive($project, 'php-consumer');

    // OPTIONAL: Create a span to wrap the unit of work
    $span = $tracer->createSpan('work');
    $span->annotate('job', 'my-important-job');

    // do the actual work

    // stop the optional span started above
    $span->stop();

    $subscription->acknowledge($message);
}

サービス名の設定

PHP のSDKを使用してスパンを作成する際は、 PHPINSTANA_SERVICE_NAME -SDKの関数を手動で呼び出すことで設定を行うことができます。 この値は、 $__ENV 配列および $_SERVER 配列に設定された INSTANA_SERVICE_NAME 値よりも優先されます。 以下の例は、関連するコード・スニペットがどのように表示されるかを示しています。

//get the tracer
$tracer = new \Instana\Tracer();

Instana\Tracer::setServiceName(“MY_SERVICE_NAME”);

SDK の削除

PHP のトレース拡張機能を削除する際は、手動で追加したSDKコードもすべて削除する必要があります。そうしないと、関数が不足しているために PHP がスクリプトの実行に失敗します。