Skip to content

Commit

Permalink
Add end to end tracing header and refactore resource header
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Dec 10, 2024
1 parent cc95960 commit 35e68fd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
22 changes: 21 additions & 1 deletion OBSERVABILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,27 @@ const spanner = new Spanner({
observabilityOptions: {
tracerProvider: provider,
enableExtendedTracing: true,
},
}
}),
```

#### End to end tracing

In addition to client-side tracing, you can opt in for end-to-end tracing. End-to-end tracing helps you understand and debug latency issues that are specific to Spanner. Refer [here](https://cloud.google.com/spanner/docs/tracing-overview) for more information.

You can opt-in by either:

* Setting the environment variable `SPANNER_ENABLE_END_TO_END_TRACING=true` before your application is started
* In code, setting `enableEndToEndTracing: true` in your SpannerOptions before creating the Cloud Spanner client

```javascript
const spanner = new Spanner({
projectId: projectId,
observabilityOptions: {
tracerProvider: provider,
enableEndToEndTracing: true,
}
}),
```

#### OpenTelemetry gRPC instrumentation
Expand Down
57 changes: 57 additions & 0 deletions observability-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as mockInstanceAdmin from '../test/mockserver/mockinstanceadmin';
import * as mockDatabaseAdmin from '../test/mockserver/mockdatabaseadmin';
import * as sinon from 'sinon';
import {Row} from '../src/partial-result-stream';
import {END_TO_END_TRACING_HEADER} from '../src/common';
const {
AlwaysOnSampler,
NodeTracerProvider,
Expand Down Expand Up @@ -1930,3 +1931,59 @@ describe('Traces for ExecuteStream broken stream retries', () => {
);
});
});

describe('End to end tracing headers', () => {
let server: grpc.Server;
let spanner: Spanner;
let spannerMock: mock.MockSpanner;
let observabilityOptions: typeof ObservabilityOptions;

beforeEach(async () => {
observabilityOptions = {
enableEndToEndTracing: true,
};

const setupResult = await setup(observabilityOptions);
spanner = setupResult.spanner;
server = setupResult.server;
spannerMock = setupResult.spannerMock;
});

afterEach(async () => {
spannerMock.resetRequests();
spanner.close();
server.tryShutdown(() => {});
});

it('run', done => {
const instance = spanner.instance('instance');
const database = instance.database('database');
database.getTransaction((err, tx) => {
assert.ifError(err);

tx!.run('SELECT 1', async () => {
tx!.end();
let metadataCountWithE2EHeader = 0;
let metadataCountWithTraceParent = 0;
spannerMock.getMetadata().forEach(metadata => {
if (metadata.get(END_TO_END_TRACING_HEADER)[0] !== undefined) {
metadataCountWithE2EHeader++;
assert.strictEqual(
metadata.get(END_TO_END_TRACING_HEADER)[0],
'true'
);
}
if (metadata.get('traceparent')[0] !== undefined) {
metadataCountWithTraceParent++;
}
});

// Batch Create Session request and Select 1 request.
assert.strictEqual(spannerMock.getRequests().length, 2);
assert.strictEqual(metadataCountWithE2EHeader, 2);
assert.strictEqual(metadataCountWithTraceParent, 2);
done();
});
});
});
});
7 changes: 5 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const LEADER_AWARE_ROUTING_HEADER = 'x-goog-spanner-route-to-leader';
/*
* END TO END TRACING header.
*/
const END_TO_END_TRACING_HEADER = 'x-goog-spanner-end-to-end-tracing';
export const END_TO_END_TRACING_HEADER = 'x-goog-spanner-end-to-end-tracing';

/**
* Add Leader aware routing header to existing header list.
Expand All @@ -104,7 +104,10 @@ export function getCommonHeaders(
) {
const headers: {[k: string]: string} = {};

if (process.env.SPANNER_ENABLE_EXTENDED_TRACING === 'true' || enableTracing) {
if (
process.env.SPANNER_ENABLE_END_TO_END_TRACING === 'true' ||
enableTracing
) {
headers[END_TO_END_TRACING_HEADER] = 'true';
}

Expand Down

0 comments on commit 35e68fd

Please sign in to comment.