Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect encoding for traceId and spanId in OTLP JSON body #1059

Closed
mkaisercross opened this issue Jun 27, 2023 · 4 comments
Closed

Incorrect encoding for traceId and spanId in OTLP JSON body #1059

mkaisercross opened this issue Jun 27, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@mkaisercross
Copy link

Describe your environment
PHP 8.1.16 (cli) (built: Feb 14 2023 18:59:41) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.16, Copyright (c) Zend Technologies
with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans

From my composer.json file

"open-telemetry/api": "^0.0.17",
"open-telemetry/exporter-otlp": "^0.0.17",
"open-telemetry/exporter-zipkin": "^0.0.17",
"open-telemetry/sdk": "^0.0.17",

Steps to reproduce

<?php

namespace App\Logging\Trace;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;

$transport = (new OtlpHttpTransportFactory())->create('http://tempo:4318/v1/traces', 'application/json');
$exporter = new SpanExporter($transport);                    
$processor = new SimpleSpanProcessor($exporter);

$this->tracer = (new TracerProvider(
    [$processor],
    new AlwaysOnSampler(),
))->getTracer('open-telemetry/opentelemetry-php', '0.0.17');

$span = $this->tracer->spanBuilder('myspan')->startSpan();
$scope = $span->activate();
$span->end();
$scope->detach();

What is the expected behavior?

Currently the OpenTelemetry\Contrib\Oltp classes are producing JSON that is not conforming to the standard. The traceId and spanId are not the correct encoding/length. It looks like they are base64 encoding but they should be base16 (aka hex). See evidence in links below.

OTLP Repo Trace Example
OTLP Repo Spec
OTLP Docs Spec

What is the actual behavior?

Here is a simplified version of the JSON I was able to dump from $payload argument in the 'vendor/open-telemetry/sdk/Common/Export/Http/PsrTransport.php::send() function. As you can see the length of traceId and spanId are 24 and 12 characters. These should be 32 and 16 characters for base16 encoding of 16 and 8 bytes as is defined by the specification.

{
    "resourceSpans": [
        {
            "resource": {
                "attributes": [
                ]
            },
            "scopeSpans": [
                {
                    "scope": {
                        "name": "open-telemetry\/opentelemetry-php",
                        "version": "0.0.17"
                    },
                    "spans": [
                        {
                            "traceId": "MF9BNwwLEm6IC2hDR31+Ww==",
                            "spanId": "v0ZBmry1Gg4=",
                            "parentSpanId": "em2aw6O/E7I=",
                            "name": "myspan",
                            "kind": "SPAN_KIND_INTERNAL",
                            "startTimeUnixNano": "1687820190611267258",
                            "endTimeUnixNano": "1687820190791811031",
                            "attributes": [
                            ],
                            "droppedAttributesCount": 1,
                            "status": {}
                        }
                    ]
                }
            ],
            "schemaUrl": "https:\/\/opentelemetry.io\/schemas\/1.12.0"
        }
    ]
}

Additional context

Additionally when I try to send the above JSON to grafana tempo for example I get back the following:

# curl -X POST -H "Content-Type: application/json" -d @traces.json -i http://tempo:4318/v1/traces
HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Content-Type: application/json
Date: Tue, 27 Jun 2023 00:53:17 GMT
Content-Length: 267

{"code":3,"message":"readSpan.traceId: parse trace_id:invalid length for ID
@mkaisercross mkaisercross added the bug Something isn't working label Jun 27, 2023
@brettmc
Copy link
Collaborator

brettmc commented Jun 27, 2023

Hi @mkaisercross
v0.0.17 is ancient, can you please try with the latest beta releases? You'll need to set minimum-stability in composer.json

@mkaisercross
Copy link
Author

Hi @brettmc. Ok I set in my composer.json file the following:

"open-telemetry/api": "^1.0.0@beta",
"open-telemetry/exporter-otlp": "^1.0.0@beta",
"open-telemetry/sdk": "^1.0.0@beta",

That resulted in these version in composer.lock

open-telemetry/api -> 1.0.0beta10
open-telemetry/sdk ->1.0.0beta12
open-telemetry/exporter-otlp -> 1.0.0beta10

Unfortunately now I am getting a runtime exception.

[2023-06-27 03:01:57] dev.ERROR: RuntimeException: No protobuf implementation found (ext-protobuf or google/protobuf) in /application/vendor/open-telemetry/exporter-otlp/SpanExporter.php:32

@brettmc
Copy link
Collaborator

brettmc commented Jun 27, 2023

https://github.com/open-telemetry/opentelemetry-php/tree/main/src/Contrib/Otlp#protobuf-runtime-library

To do otlp exporting (via http or grpc), you need to provide one of google/protobuf or ext-protobuf. I'm trying to work out how to make that more obvious, without insisting on one or the other (they each have benefits and tradeoffs).

@mkaisercross
Copy link
Author

Ok yeah installing google/protobuf solved the issue for me and I can see the traceId and spanId have the correct format now. Thanks!

@brettmc brettmc closed this as completed Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants