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

Add SpanData type #72

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions packages/opentelemetry-types/src/trace/span_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { SpanContext } from './span_context';
import { Resource } from '../resources/Resource';
import { SpanKind } from './span_kind';
import { Attributes } from './attributes';
import { Link } from './link';
import { Status } from './status';

/**
* SpanData an object that is used to report out-of-band completed spans.
* The object and its members have to be treated as immutable.
*/
export interface SpanData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about having this just be a structural object type, so something like

export interface SpanData {
   name: string;
   kind: SpanKind;
   ...
}

The advantage of a structural type is that you can easily create it and it also doesn't take extra JS code for the methods that would be added to the JS bundles (especially relevant for browsers)

Copy link
Contributor Author

@danielkhan danielkhan Jun 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@draffensperger I did it like that because SpanData MUST be immutable.
So modeling it this way in TypeScript made sense to me.
But I am not a TypeScript wizard and I can also imagine simply using readonly for the properties. What do you think?

Copy link
Contributor

@draffensperger draffensperger Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for making SpanData immutable? My understanding is that is important for multi-threaded languages like Java/Go (looking at Concurrency and Thread-Safety of OpenTelemetry API).

However, in Node and the browser, due to the evented model, it's not actually possible for two threads to modify the same object, so the concerns about immutability for the sake of thread safety don't apply. (Multiple threads can mutate a SharedArrayBuffer, but that's a low level type, whereas objects get cloned when passed between say the browser UI thread and a WebWorker).

Would there be another reason for making this immutable besides thread safety?

Having the types be readonly is a nice way to document and enforce immutability if all consumers of the API used TypeScript, but for plain JS users of the API, the readonly just gets compiled away. So I agree that having getter methods or even get property accessors is the way to go if we need immutability.

Copy link
Contributor Author

@danielkhan danielkhan Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpanData is used to report out-of-band spans. The spec emphasizes the immutability of the members and also the members' contents if they are compound types.

Example:
SpanData is passed into the tracer.
Now let's assume that this is not immediately passed to the exporter but buffered somewhere.

If someone starts to modify the object itself after it has been passed to the tracer - like setting new links or another resource or just changing SpanKind - the exported Span would be different from what has been passed to the tracer in the first place because of pass-by-reference.

Or let's say that links or context is changed somewhere else while the Span has not yet been exported.
The spec demands to make sure that this must not happen.
To tackle compounds, we might even need to clone the objects.
This is also emphasized in the spec.

This is why I kept the immutability rule to make sure that implementors are fully aware of that.

This may be a fallacy - I'm open for discussions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining more of how this would be used! Makes sense on immutability making it more clear that you can't modify it after passing to the tracer, and that it keeps consistency with other languages.

I still have some lingering feelings of wishing this could just be a structure, but I think I can put those to rest particularly if others like this design. I also just learned about Object.freeze, and wonder if that could help us in some way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpanData is specified here.

I need SpanData in Tracer as specified here.

Here is the discussion and Sergeys reasons to keep it like that for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like LocalContext is kind of same as SpanData.

SpanData and LocalContext are 2 different things. SpanData is used only for exporters, while LocalContext is to be used within the tracer.

I strongly disagree with this design. It's a prime example of over-engineering. In its current state, it also doesn't provide a way to access the LocalContext, so how can vendor-specific properties be retrieved? Having only a single SpanContext to hold everything would be much simpler and easier to work with, both for vendors and for users. Especially since the 3 classes will basically hold the same kind of information and return them in different ways, which is unnecessary and confusing.

As @draffensperger mentioned, the main goals of immutability are specific to threaded languages, and there are no reasons to block the user from modifying the object in JavaScript if they want to do it. Immutability must be a hint and not enforced or it could block certain use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpanData is passed to the tracer and it's part of the spec as is immutability.
Further above I describe how mutation can lead to unexpected results and this has nothing to do with thread safety.
I think all these API design discussions should be done within the specification SIG.
I don't find it efficient to discuss that again for every language and platform.

I don't mind implementation details. The only strong opinion I have is that if there is a spec we should stick to it or request a change if we disagree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with a restrictive spec is that it cannot work cross-language. The spec should only describe concepts and a loose reference API. Since the current iteration is only for Java, we should experiment with an API that works better for JavaScript and then we'll be able to change the spec accordingly.

Also, an API that is 100% theoretical never works. We need to prove that it works, similar to the current Java work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further above I describe how mutation can lead to unexpected results and this has nothing to do with thread safety.

It can also prevent vendors to do certain things that they need. I think the idea is that APM vendors will be able to use the project implementation. If we end up needing to reimplement everything because of limitations that were added on purpose, then the value of OpenTelemetry becomes null. We can document that this should not be changed at a certain point in the lifecycle (mostly for users), but it should not be completely blocked.

/**
* Returns the name of this SpanData.
*/
getName(): string;

/**
* Returns the SpanKind of this SpanData.
*/
getKind(): SpanKind;

/**
* Returns the start timestamp of this SpanData.
*/
getStartTimestamp(): number;

/**
* Returns the end timestamp of this SpanData.
*/
getEndTimestamp: number;

/**
* Returns the SpanContext associated with this SpanData.
*/
getContext(): SpanContext;

/**
* Returns the SpanId of the parent of this SpanData.
*/
getParentSpanId(): string;

/**
* Returns the Resource associated with this SpanData.
* When null is returned the assumption is that Resource
* will be taken from the Tracer that is used to record this SpanData.
*/
getResource(): Resource;

/**
* Returns the Attributes collection associated with this SpanData.
* The order of attributes in this collection is not significant.
* This collection MUST be immutable.
*/
getAttributes(): Attributes;

/**
* Return the collection of Events with the timestamps associated with this SpanData.
* The order of events in collection is not guaranteed.
* This collection MUST be immutable.
*
* @todo: Add Event type
*/
getTimedEvents(): unknown[];

/**
* Returns the Links collection associated with this SpanData.
* The order of links in this collection is not significant.
* This collection MUST be immutable.
*/
getLinks(): Link[];

/**
* Returns the Status of this SpanData.
*/
getStatus(): Status;
}