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

Adding TypeScript to "Getting Started" #1800

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
08c0abe
feat: Tabs added to all the requiredcode snippets
kaiwalyakoparkar Oct 4, 2022
14a486c
feat: Changes in the description and documentation for language
kaiwalyakoparkar Oct 4, 2022
e7996da
feat: Browser.md updated with the ts code
kaiwalyakoparkar Oct 5, 2022
8401446
feat: Browser.md updated with necessary documentation
kaiwalyakoparkar Oct 5, 2022
11f736a
fix: File name updated in the documentation
kaiwalyakoparkar Oct 5, 2022
b97843a
fix: Code fixes in ts api code
kaiwalyakoparkar Oct 5, 2022
5e5c7d2
fix: Code and doc fixes
kaiwalyakoparkar Oct 5, 2022
cd95d8b
fix: Tracing.ts file updated according to ts formats
kaiwalyakoparkar Oct 5, 2022
c3cdb96
Merge branch 'open-telemetry:main' into kaiwalya-1799
kaiwalyakoparkar Oct 8, 2022
65e7fd2
fix: Fixed the file extension mentions in browser.md
kaiwalyakoparkar Oct 8, 2022
9ae588f
fix: Fixed the sentence framing in browser.md
kaiwalyakoparkar Oct 8, 2022
60b8aa9
fix: Restructred the initialization commands
kaiwalyakoparkar Oct 8, 2022
72d2ea9
feat: Added lang parameter to the tab
kaiwalyakoparkar Oct 8, 2022
9c974d7
fix: Tabbing removed for similar code
kaiwalyakoparkar Oct 8, 2022
be48543
fix: Added links to ts and js download sites
kaiwalyakoparkar Oct 8, 2022
f6dc3ef
fix: Title changed back to nodejs
kaiwalyakoparkar Oct 8, 2022
f70fc68
fix: Statement changed back to original
kaiwalyakoparkar Oct 8, 2022
c1d2834
feat: Added more documentation for the command tab
kaiwalyakoparkar Oct 8, 2022
3330eb7
fix: Removed nodemon from the list of deps to import
kaiwalyakoparkar Oct 8, 2022
ff993ef
fix: Removed additional typescript mention
kaiwalyakoparkar Oct 8, 2022
a6747df
fix: tsc --init command shifted to code section
kaiwalyakoparkar Oct 8, 2022
bf95704
feat
kaiwalyakoparkar Oct 8, 2022
5131635
fix: File mention fixed
kaiwalyakoparkar Oct 8, 2022
d4eabcd
feat: Added console property to tab tag
kaiwalyakoparkar Oct 8, 2022
12b283d
feat: Added console property to tab tag
kaiwalyakoparkar Oct 8, 2022
41a15af
fix: bash linting changed to shell
kaiwalyakoparkar Oct 14, 2022
32c88eb
fix: Additional instructions removed
kaiwalyakoparkar Oct 14, 2022
c266751
feat: Language attributes added to the tab tag
kaiwalyakoparkar Oct 14, 2022
c2994b4
ref: Tab removed for single typescript command
kaiwalyakoparkar Oct 14, 2022
35bd2da
fix: nodemon changed with default app runner command
kaiwalyakoparkar Oct 14, 2022
37fd8a1
fix: nodemon replaced with ts-node
kaiwalyakoparkar Oct 14, 2022
8af9242
feat: $ character appended to all the script commands
kaiwalyakoparkar Oct 14, 2022
6fc732e
ref: Added spacing for linting
kaiwalyakoparkar Oct 15, 2022
074a926
feat: Prerequisites section added
kaiwalyakoparkar Oct 19, 2022
5426d05
fix: Removed tabbing
kaiwalyakoparkar Oct 19, 2022
2f56fcc
feat: added npm init command
kaiwalyakoparkar Oct 19, 2022
b6a249b
feat: added tsc command in init and config section
kaiwalyakoparkar Oct 19, 2022
1245930
ref: Rewrote the comment
kaiwalyakoparkar Oct 19, 2022
a6a30a0
fix: Removed $ sign assigned to the shell command
kaiwalyakoparkar Oct 19, 2022
914840d
ref: Rewrote the comment
kaiwalyakoparkar Oct 19, 2022
ae1aa59
feat: Prerequisites section added
kaiwalyakoparkar Oct 19, 2022
97b87ab
fix: Removed $ sign assigned to the shell command
kaiwalyakoparkar Oct 19, 2022
7fdf245
fix: Restructure the steps
kaiwalyakoparkar Oct 19, 2022
27a4b23
fix: Restructed the the step
kaiwalyakoparkar Oct 19, 2022
b7c4fe9
fix: Orphaned space removed
kaiwalyakoparkar Oct 19, 2022
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
39 changes: 30 additions & 9 deletions content/en/docs/instrumentation/js/getting-started/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ title: Browser
aliases: [/docs/js/getting_started/browser]
---

This guide uses the example application in HTML & javascript provided below, but the steps to instrument your own application should be broadly the same.
While this guide uses the example application presented below, the steps to
instrument your own application should be similar.

## Prerequisites

Ensure that you have the following installed locally:
- [Node.js](https://nodejs.org/en/download/)
- [TypeScript](https://www.typescriptlang.org/download), if you will be using TypeScript.

## Example Application
kaiwalyakoparkar marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -42,24 +49,37 @@ To create traces in the browser, you will need `@opentelemetry/sdk-trace-web`, a

```shell
npm init -y
npm install --save @opentelemetry/api @opentelemetry/sdk-trace-web @opentelemetry/instrumentation-document-load @opentelemetry/context-zone
npm install @opentelemetry/api @opentelemetry/sdk-trace-web @opentelemetry/instrumentation-document-load @opentelemetry/context-zone
```

### Initialization and Configuration

kaiwalyakoparkar marked this conversation as resolved.
Show resolved Hide resolved
Create a empty file called `document-load.js` and add the following code to your html right before the body end tag:
If you are coding in TypeScript, then run the following command:
```shell
tsc --init
```
Create an empty code file named `document-load` with a `.ts` or `.js` extension, as appropriate, based on the language you've chosen to write your app in. Add the following code to your HTML right before the `</body>` closing tag:

{{< tabs TypeScript JavaScript >}}

```html
{{< tab lang="html">}}
<script type="module" src="document-load.ts"></script>
{{< /tab >}}

{{< tab lang="html">}}
<script type="module" src="document-load.js"></script>
```
{{< /tab >}}

{{< /tabs >}}

We will add some code that will trace the document load timings and output those as OpenTelemetry Spans.

### Creating a Tracer Provider

Add the following code to the `document-load.js` to create a tracer provider, which brings the instrumentation to trace document load:
Add the following code to the `document-load.ts|js` to create a tracer provider, which brings the instrumentation to trace document load:

```javascript
```js
/* document-load.ts|js file - the code snippet is the same for both the languages */
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load';
import { ZoneContextManager } from '@opentelemetry/context-zone';
Expand Down Expand Up @@ -101,9 +121,10 @@ Follow [these instructions](../../exporters) for setting up a backend and export

You may also want to use the `BatchSpanProcessor` to export spans in batches in order to more efficiently use resources.

To export traces to the console, modify `document-load.js` so that it matches the following code snippet:
To export traces to the console, modify `document-load.ts|js` so that it matches the following code snippet:

```javascript
```js
/* document-load.ts|js file - the code is the same for both the languages */
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load';
Expand Down
111 changes: 95 additions & 16 deletions content/en/docs/instrumentation/js/getting-started/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ aliases: [/docs/js/getting_started/nodejs]

This guide will show you how to get started with tracing in Node.js.

## Prerequisites

Ensure that you have the following installed locally:
- [Node.js](https://nodejs.org/en/download/)
- [TypeScript](https://www.typescriptlang.org/download), if you will be using TypeScript.

## Example Application
kaiwalyakoparkar marked this conversation as resolved.
Show resolved Hide resolved

This is a small example application we will monitor in this guide.
Expand All @@ -13,23 +19,54 @@ This is a small example application we will monitor in this guide.

Create an empty package.json:
kaiwalyakoparkar marked this conversation as resolved.
Show resolved Hide resolved

```sh
```shell
npm init -f
```

Install dependencies used by the example.

```sh
{{< tabs TypeScript JavaScript >}}

{{< tab lang="shell">}}
npm install express typescript ts-node express @types/express @types/node
{{< /tab >}}

{{< tab lang="shell">}}
npm install express
```
{{< /tab >}}

{{< /tabs >}}

### Code

Please save the following code as `app.js`.
If you are using TypeScript, then run following command:

```shell
tsc --init
```

kaiwalyakoparkar marked this conversation as resolved.
Show resolved Hide resolved
Create `app.ts|js` and add the following code to the file:

{{< tabs TypeScript JavaScript >}}

```javascript
/* app.js */
{{< tab >}}
/*app.ts*/
import express, { Express } from "express";

const PORT: number = parseInt(process.env.PORT || "8080");
const app: Express = express();

app.get("/", (req, res) => {
res.send("Hello World");
});

app.listen(PORT), () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
};
{{< /tab >}}

{{< tab >}}
/*app.js*/
const express = require("express");

const PORT = process.env.PORT || "8080";
Expand All @@ -42,14 +79,26 @@ app.get("/", (req, res) => {
app.listen(parseInt(PORT, 10), () => {
console.log(`Listening for requests on http://localhost:${PORT}`);
});
```
{{< /tab >}}

{{< /tab>}}

Run the application with the following request and open <http://localhost:8080> in your web browser to ensure it is working.

```sh
$ node app.js
{{< tabs TypeScript JavaScript >}}

{{< tab lang="console">}}
ts-node app.ts
Listening for requests on http://localhost:8080
```
{{< /tab >}}

{{< tab lang="console">}}
node app.js
Listening for requests on http://localhost:8080
{{< /tab >}}

{{< /tabs >}}


## Tracing

Expand Down Expand Up @@ -88,11 +137,30 @@ npm install @opentelemetry/auto-instrumentations-node

The tracing setup and configuration should be run before your application code. One tool commonly used for this task is the [`-r, --require module`](https://nodejs.org/api/cli.html#cli_r_require_module) flag.

Create a file with a name like `tracing.js` which will contain your tracing setup code.
Create a file named `tracing.ts|js`, which will contain your tracing setup code.

```javascript
/* tracing.js */
{{< tabs TypeScript JavaScript >}}

{{< tab >}}
/*tracing.ts*/
// Require dependencies
import * as opentelemetry from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';

// For troubleshooting, set the log level to DiagLogLevel.DEBUG
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

const sdk: any = new opentelemetry.NodeSDK({
traceExporter: new opentelemetry.tracing.ConsoleSpanExporter(),
instrumentations: [getNodeAutoInstrumentations()]
});

sdk.start()
{{< /tab >}}

{{< tab >}}
/*tracing.js*/
// Require dependencies
const opentelemetry = require("@opentelemetry/sdk-node");
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
Expand All @@ -107,16 +175,27 @@ const sdk = new opentelemetry.NodeSDK({
});

sdk.start()
```
{{< /tab >}}

{{< /tabs >}}

### Run Application

Now you can run your application as you normally would, but you can use the `--require` flag to load the tracing code before the application code.

```shell
{{< tabs TypeScript JavaScript >}}

{{< tab lang="console">}}
$ ts-node --require './tracing.ts' app.ts
Listening for requests on http://localhost:8080
{{< /tab >}}

{{< tab lang="console">}}
$ node --require './tracing.js' app.js
Listening for requests on http://localhost:8080
```
{{< /tab >}}

{{< /tabs >}}

Open <http://localhost:8080> in your web browser and reload the page a few times, after a while you should see the spans printed in the console by the `ConsoleSpanExporter`.

Expand Down