Skip to content

Commit

Permalink
refactor(exporters): introduce packages for shared exporter classes (#…
Browse files Browse the repository at this point in the history
…2893)

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
pichlermarc and dyladan authored Apr 21, 2022
1 parent 0f3e4de commit e5031bd
Show file tree
Hide file tree
Showing 124 changed files with 2,873 additions and 402 deletions.
14 changes: 4 additions & 10 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
[submodule "experimental/packages/exporter-trace-otlp-proto/protos"]
path = experimental/packages/exporter-trace-otlp-proto/protos
[submodule "experimental/packages/otlp-grpc-exporter-base/protos"]
path = experimental/packages/otlp-grpc-exporter-base/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
[submodule "experimental/packages/exporter-trace-otlp-grpc/protos"]
path = experimental/packages/exporter-trace-otlp-grpc/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
[submodule "experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/protos"]
path = experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
[submodule "experimental/packages/opentelemetry-exporter-metrics-otlp-proto/protos"]
path = experimental/packages/opentelemetry-exporter-metrics-otlp-proto/protos
[submodule "experimental/packages/otlp-proto-exporter-base/protos"]
path = experimental/packages/otlp-proto-exporter-base/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ All notable changes to experimental packages in this project will be documented
* updated `opentelemetry-sdk-node` to the latest Metrics SDK.
* updated `otlp-transformer` to the latest Metrics SDK.
* updated all `instrumentation-*` packages to use local implementations of `parseUrl()` due to #2884
* refactor(otlp-exporters) move base classes and associated types into their own packages #2893 @pichlermarc
* `otlp-exporter-base` => `OTLPExporterBase`, `OTLPExporterBrowserBase`, `OTLPExporterNodeBase`
* `otlp-grpc-exporter-base` => `OTLPGRPCExporterNodeBase`
* `otlp-proto-exporter-base` => `OTLPProtoExporterNodeBase`

### :rocket: (Enhancement)

Expand Down
7 changes: 3 additions & 4 deletions experimental/packages/exporter-trace-otlp-grpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
"clean": "tsc --build --clean",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"postcompile": "npm run submodule && npm run protos:copy",
"protos:copy": "cpx protos/opentelemetry/**/*.* build/protos/opentelemetry",
"submodule": "git submodule sync --recursive && git submodule update --init --recursive",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"version": "node ../../../scripts/version-update.js",
"watch": "npm run protos:copy && tsc -w",
"watch": "tsc -w",
"precompile": "lerna run version --scope $(npm pkg get name) --include-dependencies",
"prewatch": "npm run precompile",
"peer-api-check": "node ../../../scripts/peer-api-check.js",
Expand Down Expand Up @@ -52,6 +49,7 @@
"devDependencies": {
"@babel/core": "7.16.0",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/otlp-exporter-base": "0.27.0",
"@types/mocha": "8.2.3",
"@types/node": "14.17.33",
"@types/sinon": "10.0.6",
Expand All @@ -73,6 +71,7 @@
"@grpc/grpc-js": "^1.5.9",
"@grpc/proto-loader": "^0.6.9",
"@opentelemetry/exporter-trace-otlp-http": "0.27.0",
"@opentelemetry/otlp-grpc-exporter-base": "0.27.0",
"@opentelemetry/resources": "1.1.1",
"@opentelemetry/sdk-trace-base": "1.1.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@
*/

import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
import { OTLPExporterNodeBase } from './OTLPExporterNodeBase';
import {
otlpTypes,
toOTLPExportTraceServiceRequest,
} from '@opentelemetry/exporter-trace-otlp-http';
import { OTLPExporterConfigNode, ServiceClientType } from './types';
import { baggageUtils, getEnv } from '@opentelemetry/core';
import { validateAndNormalizeUrl } from './util';
import { Metadata } from '@grpc/grpc-js';
import {
OTLPGRPCExporterConfigNode,
OTLPGRPCExporterNodeBase,
ServiceClientType,
validateAndNormalizeUrl
} from '@opentelemetry/otlp-grpc-exporter-base';

const DEFAULT_COLLECTOR_URL = 'localhost:4317';

/**
* OTLP Trace Exporter for Node
*/
export class OTLPTraceExporter
extends OTLPExporterNodeBase<
ReadableSpan,
otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest
>
extends OTLPGRPCExporterNodeBase<ReadableSpan,
otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest>
implements SpanExporter {

constructor(config: OTLPExporterConfigNode = {}) {
constructor(config: OTLPGRPCExporterConfigNode = {}) {
super(config);
const headers = baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS);
this.metadata ||= new Metadata();
Expand All @@ -52,14 +53,14 @@ export class OTLPTraceExporter
return toOTLPExportTraceServiceRequest(spans, this);
}

getDefaultUrl(config: OTLPExporterConfigNode) {
getDefaultUrl(config: OTLPGRPCExporterConfigNode) {
return typeof config.url === 'string'
? validateAndNormalizeUrl(config.url)
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)
: getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT)
: DEFAULT_COLLECTOR_URL;
? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)
: getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
? validateAndNormalizeUrl(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT)
: DEFAULT_COLLECTOR_URL;
}

getServiceClientType() {
Expand Down
3 changes: 0 additions & 3 deletions experimental/packages/exporter-trace-otlp-grpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@
* limitations under the License.
*/

export * from './OTLPExporterNodeBase';
export * from './OTLPTraceExporter';
export { ServiceClientType, OTLPExporterConfigNode } from './types';
export { validateAndNormalizeUrl } from './util';
46 changes: 0 additions & 46 deletions experimental/packages/exporter-trace-otlp-grpc/submodule.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import {
ensureResourceIsCorrect,
mockedReadableSpan,
} from './traceHelper';

import { CompressionAlgorithm } from '../src/types';
import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';
import { GrpcCompressionAlgorithm } from '@opentelemetry/otlp-grpc-exporter-base';

const traceServiceProtoPath =
'opentelemetry/proto/collector/trace/v1/trace_service.proto';
const includeDirs = [path.resolve(__dirname, '../protos')];
const includeDirs = [path.resolve(__dirname, '../../otlp-grpc-exporter-base/protos')];

const address = 'localhost:1501';

Expand Down Expand Up @@ -267,7 +267,7 @@ const testCollectorExporter = (params: TestParams) =>
credentials,
metadata: params.metadata,
});
assert.strictEqual(collectorExporter.compression, CompressionAlgorithm.GZIP);
assert.strictEqual(collectorExporter.compression, GrpcCompressionAlgorithm.GZIP);
delete envSource.OTEL_EXPORTER_OTLP_COMPRESSION;
});
});
Expand Down Expand Up @@ -337,3 +337,5 @@ describe('when configuring via environment', () => {
testCollectorExporter({ useTLS: true });
testCollectorExporter({ useTLS: false });
testCollectorExporter({ metadata });


6 changes: 6 additions & 0 deletions experimental/packages/exporter-trace-otlp-grpc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
},
{
"path": "../exporter-trace-otlp-http"
},
{
"path": "../otlp-exporter-base"
},
{
"path": "../otlp-grpc-exporter-base"
}
]
}
3 changes: 2 additions & 1 deletion experimental/packages/exporter-trace-otlp-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"dependencies": {
"@opentelemetry/core": "1.1.1",
"@opentelemetry/resources": "1.1.1",
"@opentelemetry/sdk-trace-base": "1.1.1"
"@opentelemetry/sdk-trace-base": "1.1.1",
"@opentelemetry/otlp-exporter-base": "0.27.0"
}
}
2 changes: 0 additions & 2 deletions experimental/packages/exporter-trace-otlp-http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

export * from './OTLPExporterBase';
export * from './platform';
export * as otlpTypes from './types';
export { toCollectorResource, toOTLPExportTraceServiceRequest } from './transform';
export { appendResourcePathToUrlIfNotPresent } from './util';
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

import { OTLPExporterConfigBase } from '../../types';
import { OTLPExporterBrowserBase } from './OTLPExporterBrowserBase';
import { appendResourcePathToUrlIfNotPresent, OTLPExporterBrowserBase } from '@opentelemetry/otlp-exporter-base';
import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
import { toOTLPExportTraceServiceRequest } from '../../transform';
import * as otlpTypes from '../../types';
import { getEnv, baggageUtils } from '@opentelemetry/core';
import { appendResourcePathToUrlIfNotPresent } from '../../util';
import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';

const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/traces';
const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
*/

export * from './OTLPTraceExporter';
export * from './OTLPExporterBrowserBase';
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
*/

export * from './node';
export { OTLPExporterBrowserBase } from './browser';
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
*/

import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
import { OTLPExporterNodeBase } from './OTLPExporterNodeBase';
import { OTLPExporterNodeConfigBase } from './types';
import * as otlpTypes from '../../types';
import { toOTLPExportTraceServiceRequest } from '../../transform';
import { getEnv, baggageUtils } from '@opentelemetry/core';
import { appendResourcePathToUrlIfNotPresent } from '../../util';
import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrlIfNotPresent
} from '@opentelemetry/otlp-exporter-base';

const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/traces';
const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;
const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`;

/**
* Collector Trace Exporter for Node
*/
export class OTLPTraceExporter
extends OTLPExporterNodeBase<
ReadableSpan,
otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest
>
extends OTLPExporterNodeBase<ReadableSpan,
otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest>
implements SpanExporter {
constructor(config: OTLPExporterNodeConfigBase = {}) {
super(config);
Expand All @@ -50,13 +50,13 @@ export class OTLPTraceExporter
return toOTLPExportTraceServiceRequest(spans, this, true);
}

getDefaultUrl(config: OTLPExporterNodeConfigBase) : string {
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
? getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
: getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
? appendResourcePathToUrlIfNotPresent(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
: DEFAULT_COLLECTOR_URL;
? getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
: getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
? appendResourcePathToUrlIfNotPresent(getEnv().OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
: DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@
*/

export * from './OTLPTraceExporter';
export * from './OTLPExporterNodeBase';
export * from './util';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import {
import * as core from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
import { OTLPExporterBase } from './OTLPExporterBase';
import {
OTLP_SPAN_KIND_MAPPING,
opentelemetryProto,
OTLPExporterConfigBase,
} from './types';
import { OTLPExporterBase, OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';

const MAX_INTEGER_VALUE = 2147483647;
const MIN_INTEGER_VALUE = -2147483648;
Expand Down
40 changes: 1 addition & 39 deletions experimental/packages/exporter-trace-otlp-http/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { SpanAttributes, SpanKind, SpanStatusCode } from '@opentelemetry/api';
import { SpanKind, SpanStatusCode } from '@opentelemetry/api';

/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down Expand Up @@ -311,44 +311,6 @@ export namespace opentelemetryProto {
}
}

/**
* Interface for handling error
*/
export class OTLPExporterError extends Error {
readonly code?: number;
override readonly name: string = 'OTLPExporterError';
readonly data?: string;

constructor(message?: string, code?: number, data?: string) {
super(message);
this.data = data;
this.code = code;
}
}

/**
* Interface for handling export service errors
*/
export interface ExportServiceError {
name: string;
code: number;
details: string;
metadata: { [key: string]: unknown };
message: string;
stack: string;
}

/**
* Collector Exporter base config
*/
export interface OTLPExporterConfigBase {
headers?: Partial<Record<string, unknown>>;
hostname?: string;
attributes?: SpanAttributes;
url?: string;
concurrencyLimit?: number;
}

/**
* Mapping between api SpanKind and proto SpanKind
*/
Expand Down
Loading

0 comments on commit e5031bd

Please sign in to comment.