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

Span empty attributes are default mutable #713

Closed
AndrewAXue opened this issue May 20, 2020 · 1 comment
Closed

Span empty attributes are default mutable #713

AndrewAXue opened this issue May 20, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@AndrewAXue
Copy link
Contributor

AndrewAXue commented May 20, 2020

Describe your environment
Python 3.7. Irrelevant.

Steps to reproduce

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    SimpleExportSpanProcessor,
)
from opentelemetry.sdk.trace.export import ConsoleSpanExporter

trace.set_tracer_provider(TracerProvider())

console_exporter = ConsoleSpanExporter()
trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(console_exporter)
)

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span('foo') as cur_span:
    cur_span.attributes['key'] = 'value'
with tracer.start_as_current_span('other'):
    pass

What is the expected behavior?

{
    "name": "foo",
    "context": {
        "trace_id": "0x500ef2fb2813de1abadf791877a0b05e",
        "span_id": "0x853082db85da172f",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2020-05-20T18:52:55.773473Z",
    "end_time": "2020-05-20T18:52:55.773584Z",
    "status": {
        "canonical_code": "OK"
    },
    "attributes": {
        "key": "value"
    },
    "events": [],
    "links": []
}
{
    "name": "other",
    "context": {
        "trace_id": "0xcaaed6c2f0d5e5d538401b7b212348cc",
        "span_id": "0x35ec0706aa1df654",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2020-05-20T18:52:55.773922Z",
    "end_time": "2020-05-20T18:52:55.773940Z",
    "status": {
        "canonical_code": "OK"
    },
    "attributes": {},
    "events": [],
    "links": []
}

What is the actual behavior?

{
    "name": "foo",
    "context": {
        "trace_id": "0x500ef2fb2813de1abadf791877a0b05e",
        "span_id": "0x853082db85da172f",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2020-05-20T18:52:55.773473Z",
    "end_time": "2020-05-20T18:52:55.773584Z",
    "status": {
        "canonical_code": "OK"
    },
    "attributes": {
        "key": "value"
    },
    "events": [],
    "links": []
}
{
    "name": "other",
    "context": {
        "trace_id": "0xcaaed6c2f0d5e5d538401b7b212348cc",
        "span_id": "0x35ec0706aa1df654",
        "trace_state": "{}"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2020-05-20T18:52:55.773922Z",
    "end_time": "2020-05-20T18:52:55.773940Z",
    "status": {
        "canonical_code": "OK"
    },
    "attributes": {
        "key": "value"
    },
    "events": [],
    "links": []
}

Additional context
Span._empty_attributes is a default mutable when the user (incorrectly) accesses a span's attributes directly. We can solve this by doing

        if not attributes:
            self.attributes = BoundedDict.from_map(MAX_NUM_ATTRIBUTES)
        else:
            self.attributes = BoundedDict.from_map(
                MAX_NUM_ATTRIBUTES, attributes
            )

i.e. deepcopying.

@AndrewAXue AndrewAXue added the bug Something isn't working label May 20, 2020
@AndrewAXue AndrewAXue changed the title Span. Span empty attributes are default mutable May 20, 2020
@AndrewAXue
Copy link
Contributor Author

Addessed in #714

codeboten pushed a commit that referenced this issue May 27, 2020
Addresses #713. Previously it was possible for a user (acting against the api) to mutate a default variable.
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