Skip to content

Commit 3ffb058

Browse files
mayurkale22dyladan
andcommitted
chore: improve API documentation (open-telemetry#1106)
* chore: improve API documentation * chore: add @link * fix: code review Co-authored-by: Daniel Dyla <[email protected]>
1 parent 05604bc commit 3ffb058

File tree

11 files changed

+101
-33
lines changed

11 files changed

+101
-33
lines changed

api/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ This package provides everything needed to interact with the OpenTelemetry API,
99

1010
## Quick Start
1111

12-
To get started tracing you need to install the SDK and plugins, create a TracerProvider, and register it with the API.
12+
To get started you need to install the SDK and plugins, create a TracerProvider and/or MeterProvider, and register it with the API.
1313

1414
### Install Dependencies
1515

1616
```sh
1717
$ # Install tracing dependencies
1818
$ npm install \
19+
@opentelemetry/api \
1920
@opentelemetry/core \
2021
@opentelemetry/node \
2122
@opentelemetry/tracing \
@@ -28,6 +29,8 @@ $ npm install \
2829
@opentelemetry/exporter-prometheus # add exporters as needed
2930
```
3031

32+
> Note: this example is for node.js. See [examples/tracer-web](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/tracer-web) for a browser example.
33+
3134
### Initialize the SDK
3235

3336
Before any other module in your application is loaded, you must initialize the global tracer and meter providers. If you fail to initialize a provider, no-op implementations will be provided to any library which acquires them from the API.
@@ -50,9 +53,9 @@ const tracerProvider = new NodeTracerProvider();
5053
*/
5154
tracerProvider.addSpanProcessor(
5255
new SimpleSpanProcessor(
53-
new JaegerExporter(
54-
/* export options */
55-
)
56+
new JaegerExporter({
57+
serviceName: 'my-service'
58+
})
5659
)
5760
);
5861

@@ -124,18 +127,18 @@ If you are writing an instrumentation library, or prefer to call the API methods
124127
```javascript
125128
const api = require("@opentelemetry/api");
126129

127-
/* Initialize TraceProvider */
128-
api.trace.setGlobalTracerProvider(traceProvider);
129-
/* returns traceProvider (no-op if a working provider has not been initialized) */
130+
/* Initialize TracerProvider */
131+
api.trace.setGlobalTracerProvider(tracerProvider);
132+
/* returns tracerProvider (no-op if a working provider has not been initialized) */
130133
api.trace.getTracerProvider();
131-
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized); */
134+
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized) */
132135
api.trace.getTracer(name, version);
133136

134137
/* Initialize MeterProvider */
135138
api.metrics.setGlobalMeterProvider(meterProvider);
136139
/* returns meterProvider (no-op if a working provider has not been initialized) */
137140
api.metrics.getMeterProvider();
138-
/* returns a meter from the registered global meter provider (no-op if a working provider has not been initialized); */
141+
/* returns a meter from the registered global meter provider (no-op if a working provider has not been initialized) */
139142
api.metrics.getMeter(name, version);
140143

141144
/* Initialize Propagator */
@@ -172,7 +175,6 @@ async function doSomething() {
172175
}
173176
```
174177

175-
176178
## Useful links
177179
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
178180
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>

api/src/context/propagation/HttpTextPropagator.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ import { SetterFunction } from './setter';
1919
import { GetterFunction } from './getter';
2020

2121
/**
22-
* Injects {@link Context} into and extracts it from carriers that travel
22+
* Injects `Context` into and extracts it from carriers that travel
2323
* in-band across process boundaries. Encoding is expected to conform to the
2424
* HTTP Header Field semantics. Values are often encoded as RPC/HTTP request
2525
* headers.
2626
*
2727
* The carrier of propagated data on both the client (injector) and server
28-
* (extractor) side is usually an object such as http headers.
28+
* (extractor) side is usually an object such as http headers. Propagation is
29+
* usually implemented via library-specific request interceptors, where the
30+
* client-side injects values and the server-side extracts them.
2931
*/
3032
export interface HttpTextPropagator {
3133
/**
32-
* Injects values from a given {@link Context} into a carrier.
34+
* Injects values from a given `Context` into a carrier.
3335
*
34-
* OpenTelemetry defines a common set of format values (HttpTextPropagator), and
35-
* each has an expected `carrier` type.
36+
* OpenTelemetry defines a common set of format values (HttpTextPropagator),
37+
* and each has an expected `carrier` type.
3638
*
3739
* @param context the Context from which to extract values to transmit over
3840
* the wire.
@@ -44,7 +46,7 @@ export interface HttpTextPropagator {
4446
inject(context: Context, carrier: unknown, setter: SetterFunction): void;
4547

4648
/**
47-
* Given a {@link Context} and a carrier, extract context values from a
49+
* Given a `Context` and a carrier, extract context values from a
4850
* carrier and return a new context, created from the old context, with the
4951
* extracted values.
5052
*

api/src/metrics/MeterProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { Meter } from './Meter';
1818

1919
/**
20-
* MeterProvider provides an interface for creating {@link Meter}s
20+
* A registry for creating named {@link Meter}s.
2121
*/
2222
export interface MeterProvider {
2323
/**

api/src/metrics/Metric.ts

+15
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ export interface UnboundMetric<T> extends Metric {
106106
unbind(labels: Labels): void;
107107
}
108108

109+
/**
110+
* Counter is the most common synchronous instrument. This instrument supports
111+
* an `Add(increment)` function for reporting a sum, and is restricted to
112+
* non-negative increments. The default aggregation is Sum, as for any additive
113+
* instrument.
114+
*
115+
* Example uses for Counter:
116+
* <ol>
117+
* <li> count the number of bytes received. </li>
118+
* <li> count the number of requests completed. </li>
119+
* <li> count the number of accounts created. </li>
120+
* <li> count the number of checkpoints run. </li>
121+
* <li> count the number of 5xx errors. </li>
122+
* <ol>
123+
*/
109124
export interface Counter extends UnboundMetric<BoundCounter> {
110125
/**
111126
* Adds the given value to the current value. Values cannot be negative.

api/src/trace/NoopTracerProvider.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import { Tracer } from './tracer';
1919
import { TracerProvider } from './tracer_provider';
2020

2121
/**
22-
* An implementation of the {@link TracerProvider} which returns an impotent Tracer
23-
* for all calls to `getTracer`
22+
* An implementation of the {@link TracerProvider} which returns an impotent
23+
* Tracer for all calls to `getTracer`.
24+
*
25+
* All operations are no-op.
2426
*/
2527
export class NoopTracerProvider implements TracerProvider {
2628
getTracer(_name?: string, _version?: string): Tracer {

api/src/trace/SpanOptions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ export interface SpanOptions {
3030
*/
3131
kind?: SpanKind;
3232

33-
/** A spans attributes */
33+
/** A span's attributes */
3434
attributes?: Attributes;
3535

36-
/** A spans links */
36+
/** {@link Link}s span to other spans */
3737
links?: Link[];
3838

3939
/**

api/src/trace/attributes.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
/** Defines a attributes interface. */
17+
/**
18+
* Defines a attributes interface.
19+
* These attributes provides additional data about the {@link Span}.
20+
*/
1821
export interface Attributes {
1922
[attributeKey: string]: unknown;
2023
}

api/src/trace/link.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ import { LinkContext } from './link_context';
1919

2020
/**
2121
* A pointer from the current {@link Span} to another span in the same trace or
22-
* in a different trace. Used (for example) in batching operations, where a
23-
* single batch handler processes multiple requests from different traces.
22+
* in a different trace.
23+
* Few examples of Link usage.
24+
* 1. Batch Processing: A batch of elements may contain elements associated
25+
* with one or more traces/spans. Since there can only be one parent
26+
* SpanContext, Link is used to keep reference to SpanContext of all
27+
* elements in the batch.
28+
* 2. Public Endpoint: A SpanContext in incoming client request on a public
29+
* endpoint is untrusted from service provider perspective. In such case it
30+
* is advisable to start a new trace with appropriate sampling decision.
31+
* However, it is desirable to associate incoming SpanContext to new trace
32+
* initiated on service provider side so two traces (from Client and from
33+
* Service Provider) can be correlated.
2434
*/
2535
export interface Link {
2636
/** The {@link LinkContext} of a linked span. */

api/src/trace/span.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ import { TimeInput } from '../common/Time';
2525
* in-process function calls to sub-components. A Trace has a single, top-level
2626
* "root" Span that in turn may have zero or more child Spans, which in turn
2727
* may have children.
28+
*
29+
* Spans are created by the {@link Tracer.startSpan} method.
2830
*/
2931
export interface Span {
3032
/**
3133
* Returns the {@link SpanContext} object associated with this Span.
3234
*
35+
* Get an immutable, serializable identifier for this span that can be used
36+
* to create new child spans. Returned SpanContext is usable even after the
37+
* span ends.
38+
*
3339
* @returns the SpanContext object associated with this Span.
3440
*/
3541
context(): SpanContext;
3642

3743
/**
3844
* Sets an attribute to the span.
3945
*
46+
* Sets a single Attribute with the key and value passed as arguments.
47+
*
4048
* @param key the key for this attribute.
4149
* @param value the value for this attribute.
4250
*/
@@ -66,7 +74,8 @@ export interface Span {
6674

6775
/**
6876
* Sets a status to the span. If used, this will override the default Span
69-
* status. Default is {@link CanonicalCode.OK}.
77+
* status. Default is {@link CanonicalCode.OK}. SetStatus overrides the value
78+
* of previous calls to SetStatus on the Span.
7079
*
7180
* @param status the Status to set.
7281
*/
@@ -75,6 +84,11 @@ export interface Span {
7584
/**
7685
* Updates the Span name.
7786
*
87+
* This will override the name provided via {@link Tracer.startSpan}.
88+
*
89+
* Upon this update, any sampling behavior based on Span name will depend on
90+
* the implementation.
91+
*
7892
* @param name the Span name.
7993
*/
8094
updateName(name: string): this;
@@ -97,7 +111,7 @@ export interface Span {
97111
* Returns the flag whether this span will be recorded.
98112
*
99113
* @returns true if this Span is active and recording information like events
100-
* with the AddEvent operation and attributes using setAttributes.
114+
* with the `AddEvent` operation and attributes using `setAttributes`.
101115
*/
102116
isRecording(): boolean;
103117
}

api/src/trace/tracer.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,48 @@ export interface Tracer {
2929
/**
3030
* Returns the current Span from the current context if available.
3131
*
32-
* If there is no Span associated with the current context, null is returned.
32+
* If there is no Span associated with the current context, `null` is
33+
* returned.
34+
*
35+
* To install a {@link Span} to the current Context use
36+
* {@link Tracer.withSpan}.
3337
*
3438
* @returns Span The currently active Span
3539
*/
3640
getCurrentSpan(): Span | undefined;
3741

3842
/**
39-
* Starts a new {@link Span}.
43+
* Starts a new {@link Span}. Start the span without setting it as the current
44+
* span in this tracer's context.
45+
*
46+
* This method do NOT modify the current Context. To install a {@link
47+
* Span} to the current Context use {@link Tracer.withSpan}.
48+
*
4049
* @param name The name of the span
4150
* @param [options] SpanOptions used for span creation
4251
* @param [context] Context to use to extract parent
4352
* @returns Span The newly created span
53+
* @example
54+
* const span = tracer.startSpan('op');
55+
* span.setAttribute('key', 'value');
56+
* span.end();
4457
*/
4558
startSpan(name: string, options?: SpanOptions, context?: Context): Span;
4659

4760
/**
48-
* Executes the function given by fn within the context provided by Span
61+
* Executes the function given by fn within the context provided by Span.
62+
*
63+
* This is a convenience method for creating spans attached to the tracer's
64+
* context. Applications that need more control over the span lifetime should
65+
* use {@link Tracer.startSpan} instead.
4966
*
5067
* @param span The span that provides the context
5168
* @param fn The function to be executed inside the provided context
5269
* @example
53-
* tracer.withSpan(span, function() { ... });
70+
* tracer.withSpan(span, () => {
71+
* tracer.getCurrentSpan().addEvent("parent's event");
72+
* doSomeOtherWork(); // Here "span" is the current Span.
73+
* });
5474
*/
5575
withSpan<T extends (...args: unknown[]) => ReturnType<T>>(
5676
span: Span,

api/src/trace/tracer_provider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
import { Tracer } from './tracer';
1818

1919
/**
20-
* TracerProvider provides an interface for creating {@link Tracer}s
20+
* A registry for creating named {@link Tracer}s.
2121
*/
2222
export interface TracerProvider {
2323
/**
2424
* Returns a Tracer, creating one if one with the given name and version is
2525
* not already created.
2626
*
27-
* If there is no Span associated with the current context, `null` is
28-
* returned.
27+
* This function may return different Tracer types (e.g.
28+
* {@link NoopTracerProvider} vs. a functional tracer).
2929
*
3030
* @param name The name of the tracer or instrumentation library.
3131
* @param version The version of the tracer or instrumentation library.

0 commit comments

Comments
 (0)