Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
chore: updates and fixes after testing with collector, refactored hel…
…per for tests
  • Loading branch information
obecny committed Apr 2, 2020
commit c1c1a2d40e611ebf83ea32158a9fdb216fd07553
24 changes: 14 additions & 10 deletions packages/opentelemetry-exporter-collector/src/CollectorExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { NoopLogger } from '@opentelemetry/core';
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
import { Attributes, Logger } from '@opentelemetry/api';
import { onInit, onShutdown, sendSpans } from './platform/index';
import { CollectorExporterError } from './types';
import { opentelemetryProto } from './types';

/**
* Collector Exporter Config
Expand Down Expand Up @@ -84,16 +84,20 @@ export class CollectorExporter implements SpanExporter {
.then(() => {
resultCallback(ExportResult.SUCCESS);
})
.catch((error: CollectorExporterError) => {
if (error.message) {
this.logger.error(error.message);
.catch(
(
error: opentelemetryProto.collector.trace.v1.ExportTraceServiceError
) => {
if (error.message) {
this.logger.error(error.message);
}
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
}
}
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
}
});
);
}

private _exportSpans(spans: ReadableSpan[]): Promise<unknown> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2019, OpenTelemetry Authors
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,9 @@
*/

import { Logger } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { ReadableSpan } from '@opentelemetry/tracing';
import { CollectorExporter } from '../../CollectorExporter';
import {
toCollectorExportTraceServiceRequest,
toCollectorResource,
toCollectorSpan,
} from '../../transform';
import { toCollectorExportTraceServiceRequest } from '../../transform';
import * as collectorTypes from '../../types';

/**
Expand Down Expand Up @@ -55,15 +50,9 @@ export function sendSpans(
onError: (error: collectorTypes.CollectorExporterError) => void,
collectorExporter: CollectorExporter
) {
const spansToBeSent: collectorTypes.opentelemetryProto.trace.v1.Span[] = spans.map(
span => toCollectorSpan(span)
);
const resource: Resource | undefined =
spans.length > 0 ? spans[0].resource : Resource.empty();

const exportTraceServiceRequest = toCollectorExportTraceServiceRequest(
spansToBeSent,
toCollectorResource(resource)
spans,
collectorExporter
);

const body = JSON.stringify(exportTraceServiceRequest);
Expand Down Expand Up @@ -121,7 +110,7 @@ function sendSpansWithBeacon(
* @param collectorUrl
*/
function sendSpansWithXhr(
body: any,
body: string,
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void,
logger: Logger,
Expand All @@ -130,7 +119,8 @@ function sendSpansWithXhr(
const xhr = new XMLHttpRequest();
xhr.open('POST', collectorUrl);
xhr.setRequestHeader(collectorTypes.OT_REQUEST_HEADER, '1');
// xhr.setRequestHeader('Content-Length', String(body.length));
xhr.setRequestHeader('Accept', 'application/octet-stream');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(body);

xhr.onreadystatechange = () => {
Expand All @@ -139,7 +129,8 @@ function sendSpansWithXhr(
logger.debug('xhr success', body);
onSuccess();
} else {
logger.error('xhr error', xhr.status, body);
logger.error('body', body);
logger.error('xhr error', xhr);
onError({
code: xhr.status,
message: xhr.responseText,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
### Important!
**Submodule is always pointing to certain revision number. So updating the master of the submodule repo will not have impact on your code.
Knowing this if you want to change the submodule to point to a different version (when for example proto has changed) here is how to do it:**

### Updating submodule to point to certain revision number

1. Make sure you are in the same folder as this instruction

2. Update your submodules by running this command

```shell script
git submodule sync --recursive
git submodule update --init --recursive
```

3. Find the SHA which you want to update to and copy it (the long one)
the latest sha when this guide was written is `e6c3c4a74d57f870a0d781bada02cb2b2c497d14`

4. Enter a submodule directory from this directory

```shell script
cd protos
```

5. Updates files in the submodule tree to given commit:

```shell script
git checkout -q <sha>
```

6. Return to the main directory:

```shell script
cd ../
```

7. Please run `git status` you should see something like `Head detached at`. This is correct, go to next step

8. Now thing which is very important. You have to commit this to apply these changes

```shell script
git commit -am "chore: updating submodule for opentelemetry-proto"
```

9. If you look now at git log you will notice that the folder `protos` has been changed and it will show what was the previous sha and what is current one
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
*/

import * as protoLoader from '@grpc/proto-loader';
import { Resource } from '@opentelemetry/resources';
import { ReadableSpan } from '@opentelemetry/tracing';
import * as grpc from 'grpc';
import * as path from 'path';

import { CollectorExporter } from '../../CollectorExporter';
import * as collectorTypes from '../../types';
import {
toCollectorExportTraceServiceRequest,
toCollectorResource,
toCollectorSpan,
} from '../../transform';
import { toCollectorExportTraceServiceRequest } from '../../transform';
import { CollectorData, GRPCQueueItem } from './types';
import { fixGRPCUrl } from './util';

const traceServiceClients: WeakMap<
CollectorExporter,
Expand All @@ -43,7 +39,7 @@ export function onInit(collectorExporter: CollectorExporter) {
isShutDown: false,
grpcSpansQueue: [],
});
const serverAddress = collectorExporter.url;
const serverAddress = fixGRPCUrl(collectorExporter.url);
const credentials: grpc.ChannelCredentials = grpc.credentials.createInsecure();

const traceServiceProtoPath =
Expand Down Expand Up @@ -118,15 +114,9 @@ export function sendSpans(
return;
}
if (exporter.traceServiceClient) {
const spansToBeSent: collectorTypes.opentelemetryProto.trace.v1.Span[] = spans.map(
span => toCollectorSpan(span)
);
const resource: Resource | undefined =
spans.length > 0 ? spans[0].resource : Resource.empty();

const exportTraceServiceRequest = toCollectorExportTraceServiceRequest(
spansToBeSent,
toCollectorResource(resource)
spans,
collectorExporter
);

exporter.traceServiceClient.export(
Expand All @@ -135,11 +125,11 @@ export function sendSpans(
err: collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceError
) => {
if (err) {
onError({
code: err.code,
message: err.message,
stack: err.stack,
});
collectorExporter.logger.error(
'exportTraceServiceRequest',
exportTraceServiceRequest
);
onError(err);
} else {
onSuccess();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* Copyright 2020, 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.
*/

/**
* It will remove http or https from the link as grpc requires link without
* protocol
* @param url
*/
export function fixGRPCUrl(url: string): string {
let fixedUrl = url;
const removeArr = ['http://', 'https://'];
removeArr.forEach(el => {
if (fixedUrl.indexOf(el) === 0) {
fixedUrl = fixedUrl.replace(el, '');
}
});
return fixedUrl;
}
Loading