opentracing / opentracing-php

OpenTracing API for PHP
Apache License 2.0
508 stars 56 forks source link

No ability to add a new tag for a Span using interface #51

Closed ellisv closed 6 years ago

ellisv commented 6 years ago

Problem

Currently we have an ability to setTags() on a Span but no way to add an additional tag using the interface. So in order to add aditional tag you would have to keep the state of tags yourself as such:

$tags = [
    Tags\SPAN_KIND => Tags\SPAN_KIND_CLIENT,
    Tags\HTTP_URL => $httpUrl,
    Tags\HTTP_METHOD => $httpMethod,
];
$span = $tracer->startSpan('api_call', [
    'tags' => $tags,
]);

try {
    // making the api call
    $span->setTags(array_merge($tags, [Tags\HTTP_STATUS_CODE => $responseCode]));
} catch (ApiException $e) {
    // ...
    $span->setTags(array_merge($tags, [Tags\ERROR => true]));
    // ...
} finally {
    $span->finish();
}

This would look worse if you'd like to pass it between functions.

Proposal

Get rid of setTags() and introduce setTag(). All other OpenTracing libraries do so.

Usage would look as such:

$span = $tracer->startSpan('api_call', [
    'tags' => [
        Tags\SPAN_KIND => Tags\SPAN_KIND_CLIENT,
        Tags\HTTP_URL => $httpUrl,
        Tags\HTTP_METHOD => $httpMethod,
    ],
]);

try {
    // making the api call
    $span->setTag(Tags\HTTP_STATUS_CODE, $responseCode);
} catch (ApiException $e) {
    // ...
    $span->setTag(Tags\ERROR, true);
    // ...
} finally {
    $span->finish();
}

This will also help having more consistent interface between libraries.