Skip to content

Commit

Permalink
add Events SDK (open-telemetry#4629)
Browse files Browse the repository at this point in the history
* feat(sdk-events): add Events SDK

* updated changelog

* markdown lint

* updated changelog with changes to the events API

* added missing implements

* set defaults for severityNumber and timestamp

* added OTLP exporter to example

* updated package-lock.json

* updated versions

* pinned api-logs and api-events versions

* removed getting global LoggerProvider

* updated example version

* removed unnecessary constant

* lint

* updated events example

* pinned api-logs version for logs sdk

* updated package-lock

* removed unused configuration

* added forceFlush and shutdown to EventLoggerProvider

* updated package-lock.json

* updated tsconfig files

* fixed package-lock.json

* removed shutdown method, updated example

* cleanup

* added domain to examples

* updated versions

* fix browser tests
  • Loading branch information
martinkuba authored and Zirak committed Sep 14, 2024
1 parent 1f0dcff commit 89ddaa3
Show file tree
Hide file tree
Showing 31 changed files with 2,344 additions and 717 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ All notable changes to experimental packages in this project will be documented
* `InstrumentationBase`
* `InstrumentationNodeModuleDefinition`
* `InstrumentationNodeModuleFile`
* feat(api-events): removed traceId and spanId from Event interface, added context and severityNumber [#4629](https://github.com/open-telemetry/opentelemetry-js/pull/4629)

### :rocket: (Enhancement)

Expand All @@ -85,6 +86,7 @@ All notable changes to experimental packages in this project will be documented
* The value can be overwritten by
* merging a resource containing the `service.instance.id` attribute
* using another resource detector which writes `service.instance.id`
* feat(sdk-events): add Events SDK [#4629](https://github.com/open-telemetry/opentelemetry-js/pull/4629)

### :bug: (Bug Fix)

Expand Down
21 changes: 21 additions & 0 deletions experimental/examples/events/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Installation

```sh
# from this directory
npm install
```

## Run the Application

```sh
npm start
```

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on OpenTelemetry logs, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/sdk-events>

## LICENSE

Apache License 2.0
55 changes: 55 additions & 0 deletions experimental/examples/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright The 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 { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api';
import {
LoggerProvider,
ConsoleLogRecordExporter,
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';
import { events } from '@opentelemetry/api-events';
import { EventLoggerProvider } from '@opentelemetry/sdk-events';

// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

// configure global LoggerProvider
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())
);

// uncomment to use OTLP exporter
// import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
// const logExporter = new OTLPLogExporter();
// loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(logExporter));

// configure global EventLoggerProvider
const eventLoggerProvider = new EventLoggerProvider(loggerProvider);
events.setGlobalEventLoggerProvider(eventLoggerProvider);

// emit a log record
const eventLogger = events.getEventLogger('example');
eventLogger.emit({
name: 'my-domain.my-event',
data: {
a: 1,
b: 'hello',
c: {
d: 123
}
}
});
20 changes: 20 additions & 0 deletions experimental/examples/events/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "events-example",
"version": "0.51.1",
"private": true,
"scripts": {
"start": "ts-node index.ts"
},
"dependencies": {
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/api-logs": "0.51.1",
"@opentelemetry/sdk-logs": "0.51.1",
"@opentelemetry/api-events": "0.51.1",
"@opentelemetry/sdk-events": "0.51.1",
"@opentelemetry/exporter-logs-otlp-http": "0.51.1"
},
"devDependencies": {
"@types/node": "18.6.5",
"ts-node": "^10.9.1"
}
}
28 changes: 28 additions & 0 deletions experimental/examples/events/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "build",
"rootDir": "."
},
"include": ["./index.ts"],
"references": [
{
"path": "../../../api"
},
{
"path": "../../../experimental/packages/api-events"
},
{
"path": "../../../experimental/packages/api-logs"
},
{
"path": "../../../experimental/packages/sdk-events"
},
{
"path": "../../../experimental/packages/sdk-logs"
},
{
"path": "../../../experimental/packages/exporter-logs-otlp-http"
}
]
}
2 changes: 0 additions & 2 deletions experimental/examples/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ npm install

## Run the Application

LogRecord

```sh
npm start
```
Expand Down
14 changes: 7 additions & 7 deletions experimental/packages/api-events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
},
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"prepublishOnly": "npm run compile",
"compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"build": "npm run compile",
"clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
"test:browser": "karma start --single-run",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
"build": "npm run compile",
"compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"version": "node ../../../scripts/version-update.js",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"prepublishOnly": "npm run compile",
"precompile": "cross-var lerna run version --scope $npm_package_name --include-dependencies",
"prewatch": "node ../../../scripts/version-update.js",
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
"test:browser": "karma start --single-run",
"version": "node ../../../scripts/version-update.js",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"align-api-deps": "node ../../../scripts/align-api-deps.js"
},
"keywords": [
Expand Down
19 changes: 7 additions & 12 deletions experimental/packages/api-events/src/types/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import { AnyValue } from '@opentelemetry/api-logs';
import { Attributes, Context, TimeInput } from '@opentelemetry/api';
import { AnyValue, SeverityNumber } from '@opentelemetry/api-logs';

export interface Event {
/**
* The time when the event occurred as UNIX Epoch time in nanoseconds.
*/
timestamp?: number;
timestamp?: TimeInput;

/**
* The name of the event.
Expand All @@ -40,17 +40,12 @@ export interface Event {
attributes?: Attributes;

/**
* 8 least significant bits are the trace flags as defined in W3C Trace Context specification.
* Numerical value of the severity.
*/
traceFlags?: number;
severityNumber?: SeverityNumber;

/**
* A unique identifier for a trace.
* The Context associated with the Event.
*/
traceId?: string;

/**
* A unique identifier for a span within a trace.
*/
spanId?: string;
context?: Context;
}
1 change: 1 addition & 0 deletions experimental/packages/sdk-events/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
7 changes: 7 additions & 0 deletions experimental/packages/sdk-events/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
env: {
mocha: true,
node: true,
},
...require('../../../eslint.base.js'),
};
4 changes: 4 additions & 0 deletions experimental/packages/sdk-events/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin
/coverage
/doc
/test
Loading

0 comments on commit 89ddaa3

Please sign in to comment.