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

feat: Add React Plugin #164

Merged
merged 30 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d728c14
feat(opentelemetry-plugin-react-load): instruments mounting
thgao Jul 2, 2020
00480bd
test(opentelemetry-plugin-react-load): added tests for mounting spans
thgao Jul 3, 2020
557cbd6
refactor(opentelemetry-plugin-react-load): cleaned up types
thgao Jul 3, 2020
05a259e
feat(opentelemetry-plugin-react-load): added attributes
thgao Jul 6, 2020
2d7fe04
feat(opentelemetry-plugin-react-load): updating flow spans + tests
thgao Jul 9, 2020
3bdaa50
fix(opentelemetry-plugin-react-load): root of codecov test command fix
thgao Jul 9, 2020
3ce9c36
fix(opentelemetry-plugin-react-load): make attr names snake case
thgao Jul 13, 2020
97fe0d6
fix(opentelemetry-plugin-react-load): removed logs from test components
thgao Jul 14, 2020
2697443
feat(opentelemetry-plugin-react-load): unmounting instrument
thgao Jul 13, 2020
504bd0d
feat(opentelemetry-plugin-react-load): base func for error handling
thgao Jul 14, 2020
5869d25
refactor(opentelemetry-plugin-react-load): working component approach
thgao Jul 20, 2020
6a3ffd6
fix(opentelemetry-plugin-react-load): fixed js docs
thgao Jul 20, 2020
cd8c580
fix(opentelemetry-plugin-react-load): fixed error span naming and bug
thgao Jul 21, 2020
980a564
fix(opentelemetry-plugin-react-load): use version
thgao Jul 21, 2020
d41cf17
style(opentelemetry-plugin-react-load): cleaned up typos
thgao Jul 23, 2020
6d0bb8c
style(opentelemetry-plugin-react-load): set up eslint + ran linter
thgao Jul 23, 2020
4d53551
test(opentelemetry-plugin-react-load): unmounting test
thgao Jul 24, 2020
ee7d473
style(opentelemetry-plugin-react-load): ran linter
thgao Jul 24, 2020
7aae7ef
fix(opentelemetry-plugin-react-load): jsonified state stringify error
thgao Jul 24, 2020
ab0bb37
docs(opentelemetry-plugin-react-load): react plugin docs & example
thgao Jul 27, 2020
ef3604f
fix(opentelemetry-plugin-react-load): make logs warn & fix react depend
thgao Jul 29, 2020
47335ab
docs(opentelemetry-plugin-react-load): preact example
thgao Jul 29, 2020
57f693c
style(opentelemetry-plugin-react-load): eof newlines
thgao Jul 30, 2020
589aa1b
fix: Update examples/react-load/react/README.md
thgao Jul 31, 2020
a0658c3
fix: context propagation for other plugins
thgao Jul 31, 2020
cf97370
fix: removed unsed version variable
thgao Jul 31, 2020
0278307
style: redundant variable initializer
thgao Jul 31, 2020
14c4925
refactor: name parent spans when starting span
thgao Jul 31, 2020
cc5bf08
style: clean up react load examples/docs
thgao Jul 31, 2020
9e37ecd
docs: added docker to react load examples
thgao Jul 31, 2020
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ OpenTelemetry can collect tracing data automatically using plugins. Vendors/User
- [@opentelemetry/plugin-document-load][otel-plugin-document-load]
- [@opentelemetry/plugin-xml-http-request][otel-plugin-xml-http-request]
- [@opentelemetry/plugin-user-interaction][otel-plugin-user-interaction]
- [@opentelemetry/plugin-react-load][otel-plugin-react-load]

## Contributing

Expand All @@ -92,6 +93,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[otel-plugin-https]: https:/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-https
[otel-plugin-dns]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-dns
[otel-plugin-document-load]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/web/opentelemetry-plugin-document-load
[otel-plugin-react-load]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/web/opentelemetry-plugin-react-load
[otel-plugin-ioredis]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-ioredis
[otel-plugin-mongodb]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mongodb
[otel-plugin-mysql]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mysql
Expand Down
9 changes: 9 additions & 0 deletions examples/react-load/preact/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"test": {
"presets": [
["preact-cli/babel", { "modules": "commonjs" }]
]
}
}
}
1 change: 1 addition & 0 deletions examples/react-load/preact/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
size-plugin.json
65 changes: 65 additions & 0 deletions examples/react-load/preact/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Overview

The React Load plugin provides auto-instrumentation for react lifecycle methods.
This example uses the plugin and exports them to the console.

The example will show traces belong to the mounting, updating, and umounting flows as defined by React 16.4+. In this example we can show how this plugin can also be used in a Preact app.

# Installation
```
# from this directory
npm install
```

# Run the example
Run docker
```
# from this directory
npm run docker:start
```

Run app
```
# from this directory
npm run build
npm start
```

By default, the application runs on port 8080.

Open Zipkin page at http://localhost:9411/zipkin/ - you should be able to see the spans in zipkin

## Screenshots of traces
Take note of the parent-child relationships.
### First load
Upon loading, http://localhost:8080 mounting spans will be exported
<p align="center"><img src="./images/mounting.png?raw=true"/></p>
<p align="center"><img src="./images/zipkin-mounting.png?raw=true"/></p>

### Pressing 'Enter'
Here we can see the previous component unmounting and the new component mounting.
<p align="center"><img src="./images/redirect.png?raw=true"/></p>
<p align="center"><img src="./images/zipkin-redirect.png?raw=true"/></p>
<p align="center"><img src="./images/zipkin-redirect2.png?raw=true"/></p>

### Pressing 'Make Request'
While in loading state:
<p align="center"><img src="./images/updating.png?raw=true"/></p>
<p align="center"><img src="./images/zipkin-updating.png?raw=true"/></p>

After a few seconds (when the request is fulfilled):
<p align="center"><img src="./images/updating2.png?raw=true"/></p>
<p align="center"><img src="./images/zipkin-updating2.png?raw=true"/></p>


# Useful links
- For more information on OpenTelemetry, visit: [opentelemetry.io][otel]
- For more information on OpenTelemetry for Node.js, visit: [@opentelemetry/node][otel-node]

# LICENSE

Apache 2.0 - See [LICENSE][license-url] for more information.

[license-url]: https:/open-telemetry/opentelemetry-js-contrib/blob/master/LICENSE
[otel]: https://opentelemetry.io/
[otel-node]: https:/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node
18 changes: 18 additions & 0 deletions examples/react-load/preact/docker/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
receivers:
otlp:
endpoint: 0.0.0.0:55678

exporters:
zipkin:
url: "http://zipkin-all-in-one:9411/api/v2/spans"

processors:
batch:
queued_retry:

service:
pipelines:
traces:
receivers: [otlp]
exporters: [zipkin]
processors: [batch, queued_retry]
19 changes: 19 additions & 0 deletions examples/react-load/preact/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
services:

# Collector
collector:
image: omnition/opentelemetry-collector-contrib:0.2.8
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
volumes:
- ./collector-config.yaml:/conf/collector-config.yaml
ports:
- "55678:55678"
depends_on:
- zipkin-all-in-one

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
ports:
- "9411:9411"
Binary file added examples/react-load/preact/images/mounting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/react-load/preact/images/redirect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/react-load/preact/images/updating.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/react-load/preact/images/updating2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions examples/react-load/preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
thgao marked this conversation as resolved.
Show resolved Hide resolved
"private": true,
"name": "react-load-preact-example",
"version": "0.9.0",
"description": "Example of using @opentelemetry/plugin-react-load in browser with Preact",
"main": "index.js",
"scripts": {
"build": "preact build --no-prerender",
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
"docker:startd": "cd ./docker && docker-compose down && docker-compose up -d",
"docker:stop": "cd ./docker && docker-compose down",
"start": "sirv build --port 8080 --cors --single"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]:open-telemetry/opentelemetry-js-contrib.git"
},
"keywords": [
"opentelemetry",
"react",
"preact",
"web",
"tracing"
],
"engines": {
"node": ">=8"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https:/open-telemetry/opentelemetry-js-contrib/issues"
},
"devDependencies": {
"identity-obj-proxy": "^3.0.0",
"preact-cli": "^3.0.0",
"preact-render-spy": "^1.2.1",
"sirv-cli": "1.0.3"
},
"dependencies": {
thgao marked this conversation as resolved.
Show resolved Hide resolved
"@opentelemetry/context-zone": "^0.10.1",
"@opentelemetry/exporter-collector": "^0.10.1",
"@opentelemetry/plugin-react-load": "^0.8.0",
"@opentelemetry/tracing": "^0.10.1",
"@opentelemetry/web": "^0.10.1",
"preact": "^10.3.2",
"preact-render-to-string": "^5.1.4",
"preact-router": "^3.2.1"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"homepage": "https:/opentelemetry-js-contrib/examples/react-load/preact#readme"
}
10 changes: 10 additions & 0 deletions examples/react-load/preact/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
thgao marked this conversation as resolved.
Show resolved Hide resolved
<head>
<meta charset=utf-8 />
<title>React Load Example: Preact</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
59 changes: 59 additions & 0 deletions examples/react-load/preact/src/components/Content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { BaseOpenTelemetryComponent } from '@opentelemetry/plugin-react-load'
thgao marked this conversation as resolved.
Show resolved Hide resolved

class Content extends BaseOpenTelemetryComponent {
constructor(props){
super(props)
this.state = {
results: null,
isLoading: false
}
}

componentDidMount(){
// Example, do something here
}

buttonHandler() {
this.setState({isLoading: true})
const randomDelay = Math.random() * 10000;
setTimeout(() => {
this.setState({
isLoading: false,
results: randomDelay
})
},
randomDelay);
}

renderResults(){
if(this.state.isLoading){
return <div> Loading results...</div>;
}
if (!this.state.results){
return (
<div>No Results</div>
)
}
return (
<div>
Request was delayed {this.state.results} ms
</div>
)
}

render() {
return (
<div>
<h1>React Plugin Demo App</h1>
<button onClick={() => this.buttonHandler()} style={{marginBottom: '20px'}}>
Make Request
</button>
<div id="results">
{this.renderResults()}
</div>
</div>
)
}
}

export default Content;
17 changes: 17 additions & 0 deletions examples/react-load/preact/src/components/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Link } from 'preact-router/match';
import { BaseOpenTelemetryComponent } from '@opentelemetry/plugin-react-load';

class Home extends BaseOpenTelemetryComponent {
render() {
return (
<div>
<h1>
React Plugin Demo App: Preact
</h1>
<Link href='/test'><button>Enter</button></Link>
</div>
)
}
}

export default Home;
29 changes: 29 additions & 0 deletions examples/react-load/preact/src/components/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { h, Component} from 'preact';
import { Router } from 'preact-router';
import Home from './Home';
import Content from './Content';
import Tracer from '../web-tracer';

Tracer('react-load-preact-examples');

export default class App extends Component {

/** Gets fired when the route changes.
* @param {Object} event "change" event from [preact-router](http://git.io/preact-router)
* @param {string} event.url The newly routed URL
*/
handleRoute = e => {
this.currentUrl = e.url;
};

render() {
return (
<div id="app">
<Router onChange={this.handleRoute}>
<Home path="/" />
<Content path="/test"/>
</Router>
</div>
);
}
}
3 changes: 3 additions & 0 deletions examples/react-load/preact/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import App from './components/app';

export default App;
4 changes: 4 additions & 0 deletions examples/react-load/preact/src/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getFiles, setupPrecaching, setupRouting } from 'preact-cli/sw/';

setupRouting();
setupPrecaching(getFiles());
27 changes: 27 additions & 0 deletions examples/react-load/preact/src/web-tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
thgao marked this conversation as resolved.
Show resolved Hide resolved
import { WebTracerProvider } from '@opentelemetry/web';
import { BaseOpenTelemetryComponent } from '@opentelemetry/plugin-react-load';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { CollectorTraceExporter } from '@opentelemetry/exporter-collector';

export default (serviceName) => {
const provider = new WebTracerProvider();

const exporter = new CollectorTraceExporter({
url: 'http://localhost:55678/v1/trace',
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register({
contextManager: new ZoneContextManager(),
});

const tracer = provider.getTracer(serviceName);

BaseOpenTelemetryComponent.setTracer(serviceName)
BaseOpenTelemetryComponent.setLogger(provider.logger)

return tracer;
}
Loading