Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/serkan-ozal/otel-cli

CLI to send traces to an external OpenTelemetry collector OTLP endpoint
https://github.com/serkan-ozal/otel-cli

cli jeager monitoring observability open-telemetry open-telemetry-collector opentelemetry opentelemetry-collector tracing zipkin

Last synced: about 1 month ago
JSON representation

CLI to send traces to an external OpenTelemetry collector OTLP endpoint

Awesome Lists containing this project

README

        

# OTEL (OpenTelemetry) CLI

![Build Status](https://github.com/serkan-ozal/otel-cli/actions/workflows/build.yml/badge.svg)
![NPM Version](https://badge.fury.io/js/otel-cli.svg)
![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)

`otel-cli`, an application written in Node.js, is a command-line utility designed to
send OpenTelemetry traces to an external OpenTelemetry collector OTLP endpoint.

Its main use case is within shell scripts and other situations
where trace sending is most efficiently achieved by running an additional program.

## Prerequisites
- Node.js 14+

## Setup

```
npm install -g otel-cli
```

After install, check whether it is installed successfully:
```
otel-cli --version
```
By this command, you should see the installed version number if everything is installed properly.

## Configuration

### Common

| CLI Option | Environment Variable | Mandatory | Choices | Description |
|----------------------------|----------------------|-----------|---------|-------------------------------|
| - `--version`
- `-V` | | NO | | Output the CLI version number |
| - `--help`
- `-h` | | NO | | Display help for commands |

### Commands
- `otel-cli export [options]`: Create the span by given options and exports the created span to the OTEL collector OTLP endpoint.
- `otel-cli generate-id [options]`: Generate id of the specified type (`trace` or `span`) and outputs the generated id.
- `otel-cli start-server [options]`: Starts OTEL CLI server to be able to export traces in background.
- `otel-cli shutdown-server [options]`: Gracefully shutdowns OTEL CLI server by exporting buffered traces before terminate.
- `otel-cli help [command]`: Display help for the given command.

### `export` command

| CLI Option | Environment Variable | Mandatory | Choices | Default Value | Description | Example |
|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|-----------|--------------------------------------------------------------------------------------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| - `--verbose`
- `-v` | `OTEL_CLI_VERBOSE=true` | NO | | `false` | Enable verbose mode | `--verbose` |
| - `--endpoint `
- `-e ` | `OTEL_EXPORTER_OTLP_ENDPOINT=` | YES | | | OTEL Exporter OTLP endpoint | `--endpoint https://collector.otel.io` |
| - `--protocol `
- `-p ` | `OTEL_EXPORTER_OTLP_PROTOCOL=` | NO | - `http/json`
- `grpc` | `http/json` | OTEL Exporter OTLP protocol | - `--protocol http/json`
- `--protocol grpc` |
| - `--headers ...`
- `-h ...` | `OTEL_EXPORTER_OTLP_HEADERS=key1=value1>,` | NO | | | OTEL Exporter OTLP headers
- In CLI options, headers are specified as space (` `) seperated key-value pairs (`key1=value1 key2=value2 key3=value3`)
- In environment variable, headers are specified as comma (`,`) seperated key-value pairs (`key1=value1,key2=value2,key3=value3`)
| `--headers x-api-key=abcd-1234 x-project-id=efgh-5678` |
| - `--traceparent `
- `-tp ` | `TRACEPARENT=` | NO | | | Traceparent header in W3C trace context format | `--traceparent 00-84b54e9330faae5350f0dd8673c98146-279fa73bc935cc05-01` |
| - `--traceparent-disable`
- `-tpd` | `OTEL_CLI_TRACEPARENT_DISABLE=true` | NO | | `false` | Disable traceparent header based W3C trace context propagation for the exported span | `--traceparent-disable` |
| - `--traceparent-print`
- `-tpp` | `OTEL_CLI_TRACEPARENT_PRINT=true` | NO | | `false` | Print traceparent header in W3C trace context format for the exported span (the exported span id will be injected as parent span id in the header) | `--traceparent-print` |
| - `--trace-id `
- `-t ` | `OTEL_CLI_TRACE_ID=` | NO | | | Trace id | `--trace-id 84b54e9330faae5350f0dd8673c98146` |
| - `--span-id `
- `-s ` | | NO | | | Span id | `--span-id b2746bb26cd13726` |
| - `--parent-span-id `
- `-p ` | | NO | | | Parent span id | `--parent-span-id 279fa73bc935cc05` |
| - `--name `
- `-s ` | | YES | | | Span name | `--name doPayment` |
| - `--service-name `
- `-sn ` | - `OTEL_CLI_SERVICE_NAME=`
- `OTEL_SERVICE_NAME=` | YES | | | Service name | `--service-name payment-service` |
| - `--kind `
- `-k ` | | NO | - `INTERNAL`
- `SERVER`
- `CLIENT`
- `PRODUCER`
- `CONSUMER`
| `INTERNAL` | Span kind | - `--kind CLIENT`
- `--kind PRODUCER`
- ...
|
| - `--start-time-nanos ` | | NO | | | Start time in nanoseconds | `--start-time-nanos 1688811191123456789` |
| - `--start-time-micros ` | | NO | | | Start time in microseconds | `--start-time-micros 1688811191123456` |
| - `--start-time-millis ` | | NO | | | Start time in milliseconds | `--start-time-millis 1688811191123` |
| - `--start-time-secs ` | | NO | | | Start time in seconds | `--start-time-secs 1688811191` |
| - `--end-time-nanos ` | | NO | | | End time in nanoseconds | `--end-time-nanos 1688811192123456789` |
| - `--end-time-micros ` | | NO | | | End time in microseconds | `--end-time-micros 1688811192123456` |
| - `--end-time-millis ` | | NO | | | End time in milliseconds | `--end-time-millis 1688811192123` |
| - `--end-time-secs ` | | NO | | | End time in seconds | `--start-time-secs 1688811192` |
| - `--status-code `
- `-sc ` | | NO | - `UNSET`
- `OK`
- `ERROR`
| `UNSET` | Status code | - `--status-code OK`
- `--status-code ERROR`
- ...
|
| - `--status-message `
- `-sm ` | | NO | | | Status message | `--status-message "Invalid argument"` |
| - `--attributes `
- `-a ` | | NO | | | Span attributes as space (` `) seperated key-value pairs (`key1=value1 key2=value2 key3=value3`) | `--attributes key1=value1 key2=\"my value\" key3=true key4=123 key5=67.89 key6=\"456\"` |
| - `--resource-attributes `
- `-ra ` | | NO | | | Resource attributes as space (` `) seperated key-value pairs (`key1=value1 key2=value2 key3=value3`) | `--resource-attributes key1=value1 key2=\"my value\" key3=true key4=123 key5=67.89 key6=\"456\"` |
| - `--server-port`
- `-sp ` | `OTEL_CLI_SERVER_PORT=` | NO | | `7777` | OTEL CLI server port for communicating over to export traces asynchronously in background | - `--server-port 12345`
- `-sp 12345` |

#### How OTEL Exporter OTLP HTTP traces endpoint resolved?
- if `--endpoint` (or `-e`) option is specified,
OTLP HTTP traces endpoint is generated from the option value by appending `/v1/traces` to the end of the value.
- Else, if `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable is specified,
OTLP HTTP traces endpoint is generated from the environment variable value by appending `/v1/traces` to the end of the value.
- Else, CLI fails with the error.

#### How trace id is resolved?
- If `--trace-id` (or `-t`) option is specified,
trace id is used from the option value.
- Else, if `OTEL_CLI_TRACE_ID` environment variable is specified,
trace id is used from the environment variable value.
- Else, if `--traceparent` option (or `-tp`) is specified,
trace id is extracted from the traceparent header option value.
- Else, if `TRACEPARENT` environment variable is specified,
trace id is extracted from the traceparent header environment variable value.
- Else, CLI fails with the error (`Trace id is not specified`).

#### How span id is resolved?
- If `--span-id` (or `-s`) option is specified,
span id is used from the option value.
- Else, random span id (16-hex-character lowercase string) is generated.

#### How parent span id resolved?
- If `--parent-span-id` (or `-p`) option is specified,
parent span id is used from the option value.
- Else, if `OTEL_CLI_PARENT_SPAN_ID` environment variable is specified,
parent span id is used from the environment variable value.
- Else, if `--traceparent` option (or `-tp`) is specified,
parent span id is extracted from the traceparent header option value.
- Else, if `TRACEPARENT` environment variable is specified,
parent span id is extracted from the traceparent header environment variable value.
- Else, it is assumed that there is no associated parent span.

#### How start time is resolved?
- If `--start-time-nanos` option is specified,
start time is used from the option value.
- Else, if `--start-time-micros` option is specified,
start time is calculated by multiplying the option value by `1000` (to convert microseconds to nanoseconds).
- Else, if `--start-time-millis` option is specified,
start time is calculated by multiplying the option value by `1000000` (to convert milliseconds to nanoseconds).
- Else, if `--start-time-secs` option is specified,
start time is calculated by multiplying the option value by `1000000000` (to convert seconds to nanoseconds).
- Else, CLI fails with the error (`Span start time must be specified in one of the supported formats (nanoseconds, microseconds, milliseconds, or seconds)!`).

#### How end time is resolved?
- If `--end-time-nanos` option is specified,
end time is used from the option value.
- Else, if `--end-time-micros` option is specified,
end time is calculated by multiplying the option value by `1000` (to convert microseconds to nanoseconds).
- Else, if `--end-time-millis` option is specified,
end time is calculated by multiplying the option value by `1000000` (to convert milliseconds to nanoseconds).
- Else, if `--end-time-secs` option is specified,
end time is calculated by multiplying the option value by `1000000000` (to convert seconds to nanoseconds).
- Else, CLI fails with the error (`Span end time must be specified in one of the supported formats (nanoseconds, microseconds, milliseconds, or seconds)!`).

#### How to export traces asynchronously in background?
By default, `export` command sends traces synchronously to the configured OTLP endpoint by blocking the caller in the script.
But OTEL CLI also supports sending traces asynchronously through OTEL CLI server by exporting traces to the OTEL CLI server first over the specified HTTP port.
Then OTEL CLI server buffers the received traces and sends them to the target OTLP endpoint asynchronously in background.

##### Start OTEL CLI server
To be able to start OTEL CLI server, you can use `start-server` [command](#start-server-command).
By default, `start-server` command is blocking, so you should run it in the background yourself to not to block your program/script.

For example, in the Linux and MacOS environments, you can use `&` operation after the command to run it in the background:
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
# OTEL CLI server port is "7777" by default
export OTEL_CLI_SERVER_PORT=12345

# "start-server" command is blocking for the caller.
# So we put "&" at the end of command to run OTEL CLI server in background without blocking here.
otel-cli start-server &
```

or by specifying configurations through the options:
```bash
# "start-server" command is blocking for the caller.
# So we put "&" at the end of command to run OTEL CLI server in background without blocking here.
otel-cli start-server \
--endpoint \
--headers = \
--server-port 12345 \
&
```

##### Shutdown OTEL CLI server
Since the OTEL CLI server buffers the received traces to be send them asynchronously,
it should be shutdown gracefully to flush the buffered traces by exporting them to the configured OTLP endpoint before terminated.
Otherwise, some of the traces might be lost.

To be able to shutdown OTEL CLI server gracefully, you can use `shutdown-server` [command](#shutdown-server-command)
by specifying the **same** port number you use while starting server.

```bash
# OTEL CLI server port is "7777" by default
export OTEL_CLI_SERVER_PORT=12345

otel-cli shutdown-server
```

or by specifying configurations through the options:
```bash
otel-cli shutdown-server --server-port 12345
```

> :warning:
Even you don't shutdown the server manually by yourself,
OTEL CLI server shutdown itself automatically when the parent process (program or script) exits.
But in any way, it is good practice to shutdown by yourself explicitly.

### `generate-id` command

| CLI Option | Environment Variable | Mandatory | Choices | Default Value | Description | Example |
|---------------------------------------------|-------------------------|-----------|-------------------------|---------------|--------------------------------|----------------------------------------|
| - `--verbose`
- `-v` | `OTEL_CLI_VERBOSE=true` | NO | | `false` | Enables verbose mode | `--verbose` |
| - `--type `
- `-t ` | | YES | - `trace`
- `span` | | Type of the id to be generated | - `--type trace`
- `--type span` |

### `start-server` command

| CLI Option | Environment Variable | Mandatory | Choices | Default Value | Description | Example |
|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|-----------|------------------------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
| - `--verbose`
- `-v` | `OTEL_CLI_VERBOSE=true` | NO | | `false` | Enable verbose mode | `--verbose` |
| - `--endpoint `
- `-e ` | `OTEL_EXPORTER_OTLP_ENDPOINT=` | YES | | | OTEL Exporter OTLP endpoint | `--endpoint https://collector.otel.io` |
| - `--protocol `
- `-p ` | `OTEL_EXPORTER_OTLP_PROTOCOL=` | NO | - `http/json`
- `grpc` | `http/json` | OTEL Exporter OTLP protocol | - `--protocol http/json`
- `--protocol grpc` |
| - `--headers ...`
- `-h ...` | `OTEL_EXPORTER_OTLP_HEADERS=key1=value1>,` | NO | | | OTEL Exporter OTLP headers
- In CLI options, headers are specified as space (` `) seperated key-value pairs (`key1=value1 key2=value2 key3=value3`)
- In environment variable, headers are specified as comma (`,`) seperated key-value pairs (`key1=value1,key2=value2,key3=value3`)
| `--headers x-api-key=abcd-1234 x-project-id=efgh-5678` |
| - `--server-port`
- `-sp ` | `OTEL_CLI_SERVER_PORT=` | NO | | `7777` | OTEL CLI server port to start on | - `--server-port 12345`
- `-sp 12345` |

### `shutdown-server` command

| CLI Option | Environment Variable | Mandatory | Choices | Default Value | Description | Example |
|--------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|-----------|--------------------------------------------------------------------------------------------|---------------|--------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| - `--verbose`
- `-v` | `OTEL_CLI_VERBOSE=true` | NO | | `false` | Enable verbose mode | `--verbose` |
| - `--server-port`
- `-sp ` | `OTEL_CLI_SERVER_PORT=` | NO | | `7777` | OTEL CLI server port for communicating over to shutdown gracefully | - `--server-port 12345`
- `-sp 12345` |

## Examples

#### Export trace [Linux]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# 1. Build auth service
########################################

# Get start time of auth service project build process in nanoseconds
start_time=$(date +%s%9N)

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in nanoseconds
end_time=$(date +%s%9N)

# Export span of the auth service build process
otel-cli export \
--name build-auth-service --start-time-nanos ${start_time} --end-time-nanos ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 2. Build payment service
########################################

# Get start time of payment service project build process in nanoseconds
start_time=$(date +%s%9N)

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in nanoseconds
end_time=$(date +%s%9N)

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --start-time-nanos ${start_time} --end-time-nanos ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################
```

#### Export trace [MacOS]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# 1. Build auth service
########################################

# Get start time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time=$(node -e 'console.log(Date.now())')

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time=$(node -e 'console.log(Date.now())')

# Export span of the auth service build process
otel-cli export \
--name build-auth-service --start-time-millis ${start_time} --end-time-millis ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 1. Build payment service
########################################

# Get start time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time=$(node -e 'console.log(Date.now())')

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time=$(node -e 'console.log(Date.now())')

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --start-time-millis ${start_time} --end-time-millis ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################

```

#### Export trace (Parent-Child) [Linux]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# 1. Build services
################################################################################

root_span_id=$(otel-cli generate-id -t span)

# Get start time of whole build process in nanoseconds
start_time0=$(date +%s%9N)

# 1.1. Build auth service
########################################

# Get start time of auth service project build process in nanoseconds
start_time1=$(date +%s%9N)

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in nanoseconds
end_time1=$(date +%s%9N)

# Export span of the auth service project build process
otel-cli export \
--name build-auth-service --parent-span-id ${root_span_id} --start-time-nanos ${start_time1} --end-time-nanos ${end_time1} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 1.2. Build payment service
########################################

# Get start time of payment service project build process in nanoseconds
start_time2=$(date +%s%9N)

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in nanoseconds
end_time2=$(date +%s%9N)

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --parent-span-id ${root_span_id} --start-time-millis ${start_time2} --end-time-millis ${end_time2} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################

# Get end time of whole build process in nanoseconds
end_time0=$(date +%s%9N)

# Export span of the whole build process
otel-cli export \
--name build-services --span-id ${root_span_id} --start-time-millis ${start_time0} --end-time-millis ${end_time0} \
--kind INTERNAL --status-code OK

################################################################################
```

#### Export trace (Parent-Child) [MacOS]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# 1. Build services
################################################################################

root_span_id=$(otel-cli generate-id -t span)

# Get start time of whole build process in milliseconds ("date" command only support second resolution in MacOS)
start_time0=$(node -e 'console.log(Date.now())')

# 1.1. Build auth service
########################################

# Get start time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time1=$(node -e 'console.log(Date.now())')

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time1=$(node -e 'console.log(Date.now())')

# Export span of the auth service project build process
otel-cli export \
--name build-auth-service --parent-span-id ${root_span_id} --start-time-millis ${start_time1} --end-time-millis ${end_time1} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 1.2. Build payment service
########################################

# Get start time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time2=$(node -e 'console.log(Date.now())')

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time2=$(node -e 'console.log(Date.now())')

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --parent-span-id ${root_span_id} --start-time-millis ${start_time2} --end-time-millis ${end_time2} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################

# Get end time of whole build process in milliseconds ("date" command only support second resolution in MacOS)
end_time0=$(node -e 'console.log(Date.now())')

# Export span of the whole build process
otel-cli export \
--name build-services --span-id ${root_span_id} --start-time-millis ${start_time0} --end-time-millis ${end_time0} \
--kind INTERNAL --status-code OK

################################################################################
```

#### Export trace asynchronously in background [Linux]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
# Specify port number to start server port on (the default value is "7777")
# to be used by "otel-cli server-start" command.
# Additionally, this environment variable will also be picked up by "otel-cli export" command automatically
# while exporting traces to send asynchronously over OTEL CLI server.
export OTEL_CLI_SERVER_PORT=12345
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# "start-server" command is blocking for the caller.
# So we put "&" at the end of command to run OTEL CLI server in background without blocking here.
otel-cli start-server &

function shutdown_server {
# Shutdown OTEL CLI server.
#
# Note:
# Even we don't shutdown manually, OTEL CLI server shutdown itself automatically
# when this bash process (its parent process) exits.
# But in any way, it is good practice to shutdown by ourself explicitly.
otel-cli shutdown-server
}
trap shutdown_server EXIT

# 1. Build auth service
########################################

# Get start time of auth service project build process in nanoseconds
start_time=$(date +%s%9N)

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in nanoseconds
end_time=$(date +%s%9N)

# Export span of the auth service build process
otel-cli export \
--name build-auth-service --start-time-nanos ${start_time} --end-time-nanos ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 2. Build payment service
########################################

# Get start time of payment service project build process in nanoseconds
start_time=$(date +%s%9N)

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in nanoseconds
end_time=$(date +%s%9N)

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --start-time-nanos ${start_time} --end-time-nanos ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################
```

#### Export trace asynchronously in background [MacOS]
```bash
export OTEL_EXPORTER_OTLP_ENDPOINT=
export OTEL_EXPORTER_OTLP_HEADERS==
export OTEL_SERVICE_NAME=build
# Specify port number to start server port on (the default value is "7777")
# to be used by "otel-cli server-start" command.
# Additionally, this environment variable will also be picked up by "otel-cli export" command automatically
# while exporting traces to send asynchronously over OTEL CLI server.
export OTEL_CLI_SERVER_PORT=12345
export OTEL_CLI_TRACE_ID=$(otel-cli generate-id -t trace)

# "start-server" command is blocking for the caller.
# So we put "&" at the end of command to run OTEL CLI server in background without blocking here.
otel-cli start-server &

function shutdown_server {
# Shutdown OTEL CLI server.
#
# Note:
# Even we don't shutdown manually, OTEL CLI server shutdown itself automatically
# when this bash process (its parent process) exits.
# But in any way, it is good practice to shutdown by ourself explicitly.
otel-cli shutdown-server
}
trap shutdown_server EXIT

# 1. Build auth service
########################################

# Get start time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time=$(node -e 'console.log(Date.now())')

# Build auth service project
pushd auth-service
mvn clean package
popd

# Get end time of auth service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time=$(node -e 'console.log(Date.now())')

# Export span of the auth service build process
otel-cli export \
--name build-auth-service --start-time-millis ${start_time} --end-time-millis ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=auth-service buildTool=maven runtime=java

########################################

# 1. Build payment service
########################################

# Get start time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
start_time=$(node -e 'console.log(Date.now())')

# Build payment service project
pushd payment-service
npm run build
popd

# Get end time of payment service project build process in milliseconds ("date" command only support second resolution in MacOS)
end_time=$(node -e 'console.log(Date.now())')

# Export span of the payment service project build process
otel-cli export \
--name build-payment-service --start-time-millis ${start_time} --end-time-millis ${end_time} \
--kind INTERNAL --status-code OK --attributes serviceName=payment-service buildTool=npm runtime=node

########################################
```

## Roadmap
- Automated bash command tracing by wrapping command to be executed
- `http/protobuf` support as OTLP protocol
- `grpc` support as OTLP protocol
- Batch transmission support while sending traces to OTLP endpoint to reduce network RTT (Round Trip Time)

## Issues and Feedback

[![Issues](https://img.shields.io/github/issues/serkan-ozal/otel-cli.svg)](https://github.com/serkan-ozal/otel-cli/issues?q=is%3Aopen+is%3Aissue)
[![Closed issues](https://img.shields.io/github/issues-closed/serkan-ozal/otel-cli.svg)](https://github.com/serkan-ozal/otel-cli/issues?q=is%3Aissue+is%3Aclosed)

Please use [GitHub Issues](https://github.com/serkan-ozal/otel-cli/issues) for any bug report, feature request and support.

## Contribution

[![Pull requests](https://img.shields.io/github/issues-pr/serkan-ozal/otel-cli.svg)](https://github.com/serkan-ozal/otel-cli/pulls?q=is%3Aopen+is%3Apr)
[![Closed pull requests](https://img.shields.io/github/issues-pr-closed/serkan-ozal/otel-cli.svg)](https://github.com/serkan-ozal/otel-cli/pulls?q=is%3Apr+is%3Aclosed)
[![Contributors](https://img.shields.io/github/contributors/serkan-ozal/otel-cli.svg)]()

If you would like to contribute, please
- Fork the repository on GitHub and clone your fork.
- Create a branch for your changes and make your changes on it.
- Send a pull request by explaining clearly what is your contribution.

> Tip:
> Please check the existing pull requests for similar contributions and
> consider submit an issue to discuss the proposed feature before writing code.

## License

Licensed under [Apache License 2.0](LICENSE).