@mukhy/mtn-telemetry-sdk 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/node.js.yml +10 -37
- package/.github/workflows/npm-publish.yml +45 -0
- package/README.md +174 -146
- package/dist/context-manager/stack.d.ts +10 -0
- package/dist/context-manager/stack.js +48 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +42 -11
- package/dist/instrumentations/app-state.d.ts +1 -1
- package/dist/instrumentations/app-state.js +6 -31
- package/dist/instrumentations/fetch.d.ts +1 -1
- package/dist/instrumentations/fetch.js +66 -21
- package/dist/instrumentations/react-nav.d.ts +7 -1
- package/dist/instrumentations/react-nav.js +38 -12
- package/dist/providers.d.ts +8 -9
- package/dist/providers.js +105 -58
- package/dist/resource.d.ts +2 -1
- package/dist/resource.js +63 -24
- package/dist/types.d.ts +10 -2
- package/package.json +6 -6
- package/shims/async_hooks/index.js +61 -0
- package/shims/async_hooks/package.json +6 -0
- package/src/@types/react-native-device-info.d.ts +15 -0
- package/src/@types/react-native.d.ts +26 -0
- package/src/context-manager/stack.ts +55 -0
- package/src/index.ts +52 -14
- package/src/instrumentations/app-state.ts +7 -37
- package/src/instrumentations/fetch.ts +109 -31
- package/src/instrumentations/react-nav.ts +57 -16
- package/src/providers.ts +132 -63
- package/src/resource.ts +44 -26
- package/src/types.ts +9 -9
- package/tsconfig.json +1 -1
- package/.github/workflows/npm-publish-github-packages.yml +0 -53
- package/__test__/demo-test.js +0 -3
- package/src/context.js +0 -74
- package/src/index.js +0 -127
- package/src/instrumentations/app-state.js +0 -70
- package/src/instrumentations/fetch.js +0 -106
- package/src/instrumentations/react-nav.js +0 -73
- package/src/providers.js +0 -83
- package/src/resource.js +0 -22
- package/src/types.js +0 -2
|
@@ -1,43 +1,29 @@
|
|
|
1
|
-
name:
|
|
1
|
+
name: Publish MTN Telemetry SDK to NPM
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [ "master" ]
|
|
6
4
|
release:
|
|
7
5
|
types: [created]
|
|
8
6
|
|
|
9
7
|
jobs:
|
|
10
8
|
build:
|
|
11
9
|
runs-on: ubuntu-latest
|
|
12
|
-
|
|
13
|
-
strategy:
|
|
14
|
-
matrix:
|
|
15
|
-
node-version: [20.19.4, 22.x]
|
|
16
|
-
|
|
17
10
|
steps:
|
|
18
11
|
- name: Checkout repository
|
|
19
12
|
uses: actions/checkout@v4
|
|
20
13
|
|
|
21
|
-
- name: Remove package-lock.json (avoid warnings)
|
|
22
|
-
run: |
|
|
23
|
-
if [ -f package-lock.json ]; then
|
|
24
|
-
rm package-lock.json
|
|
25
|
-
fi
|
|
26
|
-
|
|
27
14
|
- name: Setup Node.js
|
|
28
15
|
uses: actions/setup-node@v4
|
|
29
16
|
with:
|
|
30
|
-
node-version:
|
|
31
|
-
cache: 'yarn'
|
|
17
|
+
node-version: 20
|
|
32
18
|
|
|
33
19
|
- name: Install dependencies
|
|
34
|
-
run:
|
|
20
|
+
run: npm ci
|
|
35
21
|
|
|
36
22
|
- name: Build SDK
|
|
37
|
-
run:
|
|
23
|
+
run: npm run build
|
|
38
24
|
|
|
39
25
|
- name: Run tests
|
|
40
|
-
run:
|
|
26
|
+
run: npm test
|
|
41
27
|
|
|
42
28
|
publish:
|
|
43
29
|
needs: build
|
|
@@ -50,32 +36,19 @@ jobs:
|
|
|
50
36
|
- name: Checkout repository
|
|
51
37
|
uses: actions/checkout@v4
|
|
52
38
|
|
|
53
|
-
- name: Setup Node.js for
|
|
39
|
+
- name: Setup Node.js for NPM
|
|
54
40
|
uses: actions/setup-node@v4
|
|
55
41
|
with:
|
|
56
42
|
node-version: 20
|
|
57
|
-
registry-url: https://
|
|
58
|
-
scope: '@triplemcoder'
|
|
43
|
+
registry-url: https://registry.npmjs.org/
|
|
59
44
|
|
|
60
45
|
- name: Install dependencies
|
|
61
|
-
run:
|
|
46
|
+
run: npm ci
|
|
62
47
|
|
|
63
48
|
- name: Ensure package is public
|
|
64
49
|
run: npm pkg set publishConfig.access=public
|
|
65
50
|
|
|
66
|
-
- name:
|
|
67
|
-
id: version-check
|
|
68
|
-
run: |
|
|
69
|
-
VERSION=$(node -p "require('./package.json').version")
|
|
70
|
-
echo "PACKAGE_VERSION=$VERSION" >> $GITHUB_ENV
|
|
71
|
-
# Attempt to fetch package info
|
|
72
|
-
if npm info @triplemcoder14/mtn-telemetry-sdk@$VERSION >/dev/null 2>&1; then
|
|
73
|
-
echo "Version $VERSION already exists, skipping publish"
|
|
74
|
-
exit 1
|
|
75
|
-
fi
|
|
76
|
-
|
|
77
|
-
- name: Publish to GitHub Packages
|
|
78
|
-
if: success()
|
|
51
|
+
- name: Publish to NPM
|
|
79
52
|
run: npm publish
|
|
80
53
|
env:
|
|
81
|
-
NODE_AUTH_TOKEN: ${{ secrets.
|
|
54
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
name: Build & Publish MTN Telemetry SDK to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- master
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build-and-publish:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
permissions:
|
|
15
|
+
contents: read
|
|
16
|
+
packages: write
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Checkout repository
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Setup Node.js
|
|
23
|
+
uses: actions/setup-node@v4
|
|
24
|
+
with:
|
|
25
|
+
node-version: 20
|
|
26
|
+
registry-url: https://registry.npmjs.org/
|
|
27
|
+
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: |
|
|
30
|
+
echo "Installing dependencies..."
|
|
31
|
+
npm install
|
|
32
|
+
|
|
33
|
+
- name: Build SDK
|
|
34
|
+
run: npm run build
|
|
35
|
+
|
|
36
|
+
- name: Run tests
|
|
37
|
+
run: npm test
|
|
38
|
+
|
|
39
|
+
- name: Publish to npm
|
|
40
|
+
if: github.event_name == 'release'
|
|
41
|
+
env:
|
|
42
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
43
|
+
run: |
|
|
44
|
+
echo "Publishing version $(node -p "require('./package.json').version") to npm..."
|
|
45
|
+
npm publish --access public
|
package/README.md
CHANGED
|
@@ -1,146 +1,174 @@
|
|
|
1
|
-
### MTN Telemetry SDK
|
|
2
|
-
|
|
3
|
-
#### MTN Telemetry SDK is a lightweight OpenTelemetry wrapper for React and React Native applications, designed to help developers collect traces, metrics, and logs with minimal configuration
|
|
4
|
-
|
|
5
|
-
### Features
|
|
6
|
-
|
|
7
|
-
* Easy initialization for React and React Native apps
|
|
8
|
-
* Automatic instrumentation for:
|
|
9
|
-
* Navigation
|
|
10
|
-
* API requests / network calls
|
|
11
|
-
* Supports custom spans and metrics
|
|
12
|
-
* TypeScript-ready, with type definitions
|
|
13
|
-
* Compatible with GitHub Packages for easy installation
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
Install via npm:
|
|
19
|
-
|
|
20
|
-
``npm
|
|
21
|
-
``
|
|
22
|
-
|
|
23
|
-
or via yarn:
|
|
24
|
-
|
|
25
|
-
``yarn add @
|
|
26
|
-
``
|
|
27
|
-
|
|
28
|
-
**Make sure your project uses React >=18.0.0 and React Native >=0.72.0.**
|
|
29
|
-
|
|
30
|
-
#### Getting Started
|
|
31
|
-
|
|
32
|
-
1. Initialize the SDK
|
|
33
|
-
|
|
34
|
-
Create a file like src/telemetry/initTelemetry.ts:
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
1
|
+
### MTN Telemetry SDK
|
|
2
|
+
|
|
3
|
+
#### MTN Telemetry SDK is a lightweight OpenTelemetry wrapper for React and React Native applications, designed to help developers collect traces, metrics, and logs with minimal configuration
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
* Easy initialization for React and React Native apps
|
|
8
|
+
* Automatic instrumentation for:
|
|
9
|
+
* Navigation
|
|
10
|
+
* API requests / network calls
|
|
11
|
+
* Supports custom spans and metrics
|
|
12
|
+
* TypeScript-ready, with type definitions
|
|
13
|
+
* Compatible with GitHub Packages for easy installation
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Installation
|
|
17
|
+
|
|
18
|
+
Install via npm:
|
|
19
|
+
|
|
20
|
+
``npm i @mukhy/mtn-telemetry-sdk
|
|
21
|
+
``
|
|
22
|
+
|
|
23
|
+
or via yarn:
|
|
24
|
+
|
|
25
|
+
``yarn add @mukhy/mtn-telemetry-sdk
|
|
26
|
+
``
|
|
27
|
+
|
|
28
|
+
**Make sure your project uses React >=18.0.0 and React Native >=0.72.0.**
|
|
29
|
+
|
|
30
|
+
#### Getting Started
|
|
31
|
+
|
|
32
|
+
1. Initialize the SDK
|
|
33
|
+
|
|
34
|
+
Create a file like src/telemetry/initTelemetry.ts:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
import { MTNOTel } from 'mtn-telemetry-sdk';
|
|
38
|
+
import type { OTelRNOptions } from 'mtn-telemetry-sdk/dist/types';
|
|
39
|
+
|
|
40
|
+
export async function initTelemetry(navigationRef?: any) {
|
|
41
|
+
const options: OTelRNOptions = {
|
|
42
|
+
serviceName: 'demo-app',
|
|
43
|
+
environment: 'production',
|
|
44
|
+
navigationRef,
|
|
45
|
+
otlp: {
|
|
46
|
+
tracesUrl: 'https://collector.example.com/v1/traces',
|
|
47
|
+
headers: {
|
|
48
|
+
Authorization: 'Bearer example-token',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
await MTNOTel.init(options);
|
|
54
|
+
console.log('Telemetry initialized');
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
2. Integrate in React / React Native App
|
|
59
|
+
In your root component (e.g., App.tsx):
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
import React, { useEffect, useRef } from 'react';
|
|
63
|
+
import { initTelemetry } from './telemetry/initTelemetry';
|
|
64
|
+
import { NavigationContainer } from '@react-navigation/native';
|
|
65
|
+
import MainStack from './screens/MainStack';
|
|
66
|
+
|
|
67
|
+
export default function App() {
|
|
68
|
+
const navigationRef = useRef(null);
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
initTelemetry(navigationRef.current);
|
|
72
|
+
}, []);
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<NavigationContainer ref={navigationRef}>
|
|
76
|
+
<MainStack />
|
|
77
|
+
</NavigationContainer>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
3. Creating Custom Spans
|
|
83
|
+
|
|
84
|
+
You can measure the performance of any async operation:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
import { startSpan, endSpan } from 'mtn-telemetry-sdk';
|
|
88
|
+
|
|
89
|
+
async function fetchUserData() {
|
|
90
|
+
const span = startSpan('fetch_user_data', { url: '/users' });
|
|
91
|
+
try {
|
|
92
|
+
const response = await fetch('/users');
|
|
93
|
+
const data = await response.json();
|
|
94
|
+
return data;
|
|
95
|
+
} finally {
|
|
96
|
+
endSpan(span);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
4. Automatic Navigation Instrumentation (Optional)
|
|
102
|
+
If using React Navigation:
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
import { useNavigationContainerRef } from '@react-navigation/native';
|
|
106
|
+
import { MTNOTel } from 'mtn-telemetry-sdk';
|
|
107
|
+
|
|
108
|
+
const navigationRef = useNavigationContainerRef();
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
MTNOTel.attachNavigation(navigationRef);
|
|
112
|
+
return () => MTNOTel.detachNavigation();
|
|
113
|
+
}, [navigationRef]);
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<NavigationContainer ref={navigationRef}>
|
|
117
|
+
<MainStack />
|
|
118
|
+
</NavigationContainer>
|
|
119
|
+
);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
Configuration Options
|
|
124
|
+
|
|
125
|
+
The ``OTELRNOptions`` object can include:
|
|
126
|
+
|
|
127
|
+
| Option | Type | Description |
|
|
128
|
+
| ------------------ | ----------------------- | ---------------------------------------------------------------------------- |
|
|
129
|
+
| `serviceName` | string | Required service name passed to the OpenTelemetry resource. |
|
|
130
|
+
| `environment` | string | Optional deployment environment tag (`dev`, `staging`, `prod`, …). |
|
|
131
|
+
| `release` | string | Optional release/build identifier; falls back to `react-native-device-info`. |
|
|
132
|
+
| `otlp` | object | Optional OTLP exporter configuration. |
|
|
133
|
+
| `otlp.tracesUrl` | string | Custom OTLP HTTP traces endpoint (defaults to `http://localhost:4318`). |
|
|
134
|
+
| `otlp.metricsUrl` | string | Custom OTLP HTTP metrics endpoint. |
|
|
135
|
+
| `otlp.headers` | Record<string, string> | Headers injected into every OTLP export request. |
|
|
136
|
+
| `enableFetch` | boolean | Toggle automatic fetch instrumentation (defaults to `true`). |
|
|
137
|
+
| `enableNavigation` | boolean | Toggle React Navigation instrumentation (defaults to `true`). |
|
|
138
|
+
| `enableAppState` | boolean | Toggle React Native AppState spans and metrics (defaults to `true`). |
|
|
139
|
+
| `navigationRef` | NavigationContainerLike | Optional navigation container to attach automatically during init. |
|
|
140
|
+
| `attributes` | Record<string, unknown> | Extra resource attributes merged into the default OpenTelemetry resource. |
|
|
141
|
+
| `samplingRatio` | number | Root sampler trace-id ratio (defaults to `1.0`). |
|
|
142
|
+
|
|
143
|
+
Publishing & Versioning
|
|
144
|
+
* The SDK is hosted on GitHub Packages:
|
|
145
|
+
``npm install @triplemcoder/mtn-telemetry-sdk``
|
|
146
|
+
|
|
147
|
+
Always increment ``version`` in ``package.json`` before publishing.
|
|
148
|
+
|
|
149
|
+
Contributing
|
|
150
|
+
1. Fork the repo
|
|
151
|
+
2. Create a feature branch:
|
|
152
|
+
``git checkout -b feature/my-feature``
|
|
153
|
+
3. Commit changes
|
|
154
|
+
4. Push branch and open a PR
|
|
155
|
+
|
|
156
|
+
Example Usage in React Native
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
import { MTNOTel } from 'mtn-telemetry-sdk';
|
|
160
|
+
|
|
161
|
+
MTNOTel.init({
|
|
162
|
+
serviceName: 'demo-app',
|
|
163
|
+
environment: 'production'
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Custom span for API request
|
|
167
|
+
const span = MTNOTel.startSpan('fetch_users');
|
|
168
|
+
// ...fetch API
|
|
169
|
+
MTNOTel.endSpan(span);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Context, ContextManager } from '@opentelemetry/api';
|
|
2
|
+
export declare class StackContextManager implements ContextManager {
|
|
3
|
+
private _stack;
|
|
4
|
+
private _enabled;
|
|
5
|
+
active(): Context;
|
|
6
|
+
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(context: Context, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
|
|
7
|
+
bind<T>(context: Context, target: T): T;
|
|
8
|
+
enable(): this;
|
|
9
|
+
disable(): this;
|
|
10
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StackContextManager = void 0;
|
|
4
|
+
const api_1 = require("@opentelemetry/api");
|
|
5
|
+
class StackContextManager {
|
|
6
|
+
constructor() {
|
|
7
|
+
this._stack = [];
|
|
8
|
+
this._enabled = false;
|
|
9
|
+
}
|
|
10
|
+
active() {
|
|
11
|
+
return this._stack.length > 0 ? this._stack[this._stack.length - 1] : api_1.ROOT_CONTEXT;
|
|
12
|
+
}
|
|
13
|
+
with(context, fn, thisArg, ...args) {
|
|
14
|
+
if (!this._enabled) {
|
|
15
|
+
return fn.apply(thisArg, args);
|
|
16
|
+
}
|
|
17
|
+
this._stack.push(context);
|
|
18
|
+
try {
|
|
19
|
+
return fn.apply(thisArg, args);
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
this._stack.pop();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
bind(context, target) {
|
|
26
|
+
if (!this._enabled) {
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
29
|
+
if (typeof target === 'function') {
|
|
30
|
+
const boundContextManager = this;
|
|
31
|
+
const boundFn = function bound(...args) {
|
|
32
|
+
return boundContextManager.with(context, target, this, ...args);
|
|
33
|
+
};
|
|
34
|
+
return boundFn;
|
|
35
|
+
}
|
|
36
|
+
return target;
|
|
37
|
+
}
|
|
38
|
+
enable() {
|
|
39
|
+
this._enabled = true;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
disable() {
|
|
43
|
+
this._enabled = false;
|
|
44
|
+
this._stack = [];
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.StackContextManager = StackContextManager;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import type { OTelRNOptions } from './types';
|
|
1
|
+
import type { NavigationContainerLike, OTelRNOptions } from './types';
|
|
2
2
|
export declare class MTNOTel {
|
|
3
3
|
private static _instance;
|
|
4
4
|
private shutdownFns;
|
|
5
|
+
private navigation?;
|
|
6
|
+
private options;
|
|
5
7
|
static init(opts: OTelRNOptions): Promise<MTNOTel>;
|
|
8
|
+
attachNavigation(ref: NavigationContainerLike | null): void;
|
|
9
|
+
detachNavigation(): void;
|
|
10
|
+
static attachNavigation(ref: NavigationContainerLike | null): void;
|
|
11
|
+
static detachNavigation(): void;
|
|
6
12
|
flushAndShutdown(): Promise<void>;
|
|
7
13
|
}
|
|
14
|
+
export type { OTelRNOptions, NavigationContainerLike } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,44 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MTNOTel = void 0;
|
|
4
|
+
const api_1 = require("@opentelemetry/api");
|
|
4
5
|
const providers_1 = require("./providers");
|
|
5
6
|
const resource_1 = require("./resource");
|
|
7
|
+
const app_state_1 = require("./instrumentations/app-state");
|
|
6
8
|
const fetch_1 = require("./instrumentations/fetch");
|
|
7
9
|
const react_nav_1 = require("./instrumentations/react-nav");
|
|
8
|
-
const app_state_1 = require("./instrumentations/app-state");
|
|
9
|
-
const api_1 = require("@opentelemetry/api");
|
|
10
10
|
class MTNOTel {
|
|
11
11
|
constructor() {
|
|
12
12
|
this.shutdownFns = [];
|
|
13
13
|
}
|
|
14
14
|
static async init(opts) {
|
|
15
|
-
|
|
15
|
+
var _a, _b, _c;
|
|
16
|
+
if (MTNOTel._instance) {
|
|
17
|
+
if (opts.navigationRef) {
|
|
18
|
+
MTNOTel._instance.attachNavigation(opts.navigationRef);
|
|
19
|
+
}
|
|
16
20
|
return MTNOTel._instance;
|
|
21
|
+
}
|
|
22
|
+
const normalizedOptions = {
|
|
23
|
+
enableFetch: (_a = opts.enableFetch) !== null && _a !== void 0 ? _a : true,
|
|
24
|
+
enableNavigation: (_b = opts.enableNavigation) !== null && _b !== void 0 ? _b : true,
|
|
25
|
+
enableAppState: (_c = opts.enableAppState) !== null && _c !== void 0 ? _c : true,
|
|
26
|
+
};
|
|
17
27
|
// await and async resource builder
|
|
18
28
|
const resource = await (0, resource_1.buildResource)(opts);
|
|
19
|
-
const {
|
|
29
|
+
const { shutdown } = (0, providers_1.buildProviders)({ ...opts, resource });
|
|
20
30
|
const instance = new MTNOTel();
|
|
31
|
+
instance.options = normalizedOptions;
|
|
21
32
|
instance.shutdownFns.push(shutdown);
|
|
22
33
|
// using otel api to get global tracer and meter
|
|
23
|
-
|
|
34
|
+
api_1.trace.getTracer(opts.serviceName);
|
|
24
35
|
const meter = api_1.metrics.getMeter(opts.serviceName);
|
|
25
36
|
// conditionally install instrumentation
|
|
26
|
-
if (
|
|
27
|
-
instance.shutdownFns.push((0, fetch_1.installFetchInstrumentation)(
|
|
37
|
+
if (normalizedOptions.enableFetch) {
|
|
38
|
+
instance.shutdownFns.push((0, fetch_1.installFetchInstrumentation)());
|
|
28
39
|
}
|
|
29
|
-
if (
|
|
30
|
-
instance.
|
|
40
|
+
if (normalizedOptions.enableNavigation) {
|
|
41
|
+
instance.navigation = (0, react_nav_1.installReactNavigationInstrumentation)(opts.navigationRef);
|
|
42
|
+
instance.shutdownFns.push(instance.navigation.shutdown);
|
|
31
43
|
}
|
|
32
|
-
if (
|
|
33
|
-
instance.shutdownFns.push((0, app_state_1.installAppStateInstrumentation)(
|
|
44
|
+
if (normalizedOptions.enableAppState) {
|
|
45
|
+
instance.shutdownFns.push((0, app_state_1.installAppStateInstrumentation)(meter));
|
|
34
46
|
}
|
|
35
47
|
MTNOTel._instance = instance;
|
|
36
48
|
return instance;
|
|
37
49
|
}
|
|
50
|
+
attachNavigation(ref) {
|
|
51
|
+
var _a;
|
|
52
|
+
if (!this.options.enableNavigation)
|
|
53
|
+
return;
|
|
54
|
+
(_a = this.navigation) === null || _a === void 0 ? void 0 : _a.attach(ref);
|
|
55
|
+
}
|
|
56
|
+
detachNavigation() {
|
|
57
|
+
var _a;
|
|
58
|
+
(_a = this.navigation) === null || _a === void 0 ? void 0 : _a.detach();
|
|
59
|
+
}
|
|
60
|
+
static attachNavigation(ref) {
|
|
61
|
+
var _a;
|
|
62
|
+
(_a = MTNOTel._instance) === null || _a === void 0 ? void 0 : _a.attachNavigation(ref);
|
|
63
|
+
}
|
|
64
|
+
static detachNavigation() {
|
|
65
|
+
var _a;
|
|
66
|
+
(_a = MTNOTel._instance) === null || _a === void 0 ? void 0 : _a.detachNavigation();
|
|
67
|
+
}
|
|
38
68
|
async flushAndShutdown() {
|
|
39
69
|
for (const fn of this.shutdownFns) {
|
|
40
70
|
await fn();
|
|
41
71
|
}
|
|
72
|
+
MTNOTel._instance = null;
|
|
42
73
|
}
|
|
43
74
|
}
|
|
44
75
|
exports.MTNOTel = MTNOTel;
|
|
@@ -4,4 +4,4 @@ import { type Meter } from '@opentelemetry/api';
|
|
|
4
4
|
* triplemcoder14
|
|
5
5
|
* enters foreground/background and emits spans + metrics.
|
|
6
6
|
*/
|
|
7
|
-
export declare function installAppStateInstrumentation(
|
|
7
|
+
export declare function installAppStateInstrumentation(meter: Meter): () => Promise<void>;
|