Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit b2cf987

Browse files
committed
Initial release
1 parent 5cea6d9 commit b2cf987

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ The repository also includes [sample policies](src/main/apigee/apiproxies/W3C-Tr
2121
**The documentation requires fluency with Apigee.**
2222

2323
## Installation
24-
1. Copy the JavaScript resources into the `resources/jsc` folder
25-
2. Copy `newrelic-sample.properties` file to `resources/properties/newrelic.properties`
24+
1. Copy the [Javascript Resources](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/resources/jsc) into the `resources/jsc` folder of the Proxy
25+
2. Copy [newrelic-sample.properties](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/resources/properties/newrelic-sample.properties) file to `resources/properties/newrelic.properties` in the Proxy
2626
3. Create a Request/Response Policy pair for each Flow step measured. At a minimum one pair is required to ensure that a Trace Context is present for a Target.
27-
- [Sample Request Policy](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/policies/Trace-Context-Request.xml)
28-
- [Sample Response Policy](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/policies/Trace-Context-Response.xml)
27+
- [Sample Request Policy](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/policies/Trace-Context-Request.xml)
28+
- [Sample Response Policy](src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/policies/Trace-Context-Response.xml)
2929

3030
## Configuration
3131
All Resource and Policy files include complete in-file documentation, see them for details

src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/policies/Target-Trace-Context-Request.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
<!-- OPTIONAL
2121
A comma separated list of key=value pairs propagated in the tracestate header.
22-
https://www.w3.org/TR/trace-context/#tracestate-header -->
22+
https://www.w3.org/TR/trace-context/#tracestate-header
23+
Default: no default value -->
2324
<Property name="AdditionalTracestateValues"></Property>
2425

2526
</Properties>

src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/resources/jsc/Trace-Context-Request.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
'use strict';
22

3-
// TODO validate Policy properties
3+
var logPrefix = 'TraceContextRequest: ';
4+
// <!-- OPTIONAL
5+
// The odds of a Trace being sampled, 0-100. 0 = tracing off, 100 = trace everything.
6+
// Default: 100 -->
7+
// <Property name="SampleOddsPercentage">100</Property>
8+
var SampleOddsPercentage = contextGetVariable(properties.SampleOddsPercentage, 100);
9+
// <!-- REQUIRED
10+
// The name of the Request/Response pair. Must be unique within a Proxy.
11+
// Default: no default value -->
12+
// <Property name="RequestResponsePairName">Target-Trace-Context-Flow</Property>
413

5-
var startTime = contextGetVariable(properties.TraceStartTimestamp, Date.now());
614
var RequestResponsePairName = properties.RequestResponsePairName;
7-
var logPrefix = RequestResponsePairName + '.TraceContextRequest: ';
15+
if(isNil(RequestResponsePairName)){
16+
print(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
17+
logMsg(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
18+
throw (logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
19+
}
20+
logPrefix = RequestResponsePairName + '.TraceContextRequest: ';
21+
22+
// <!-- OPTIONAL
23+
// How to determine the start time for this Trace. Any Flow Variable (https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference) ending in '.timestamp' is valid.
24+
// Default:`Date.now()'-->
25+
// <Property name="TraceStartTimestamp"></Property>
26+
var startTime = contextGetVariable(properties.TraceStartTimestamp, Date.now());
27+
28+
// <!-- OPTIONAL
29+
// A comma separated list of key=value pairs propagated in the tracestate header.
30+
// https://www.w3.org/TR/trace-context/#tracestate-header
31+
// Default: no default value -->
32+
// <Property name="AdditionalTracestateValues"></Property>
33+
var additionalStates = properties.AdditionalTracestateValues;
34+
835
logMsg(logPrefix + 'enter');
936
var ctxPrefix = 'traceContext.' + RequestResponsePairName;
1037

@@ -63,15 +90,14 @@ function generateSampled() {
6390
// Give us a number 0 - 99 inclusive
6491
var n = Math.floor(Math.random() * 100);
6592
n = n + 1;
66-
if (n <= properties.SampleOddsPercentage) {
93+
if (n <= SampleOddsPercentage) {
6794
return '01';
6895
}
6996
return '00';
7097
}
7198

7299
var traceparent = context.getVariable('request.header.traceparent');
73100
var tracestate = context.getVariable('request.header.tracestate.values.string');
74-
var additionalStates = properties.AdditionalTracestateValues;
75101
logMsg(logPrefix + 'traceparent: ' + traceparent + ' type: ' + typeof (traceparent));
76102
logMsg(logPrefix + 'tracestate: ' + tracestate + ' type: ' + typeof (tracestate));
77103
logMsg(logPrefix + 'additionalStates: ' + additionalStates + ' type: ' + typeof (additionalStates));

src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/resources/jsc/Trace-Context-Response.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
'use strict';
22

3-
// TODO validate Policy properties
4-
3+
var logPrefix = 'TraceContextResponse: ';
4+
// <!-- REQUIRED
5+
// The name of the Request/Response pair. Must be unique within a Proxy.
6+
// Default: no default value
7+
// -->
8+
// <Property name="RequestResponsePairName">Target-Trace-Context-Flow</Property>
59
var RequestResponsePairName = properties.RequestResponsePairName;
6-
var logPrefix = RequestResponsePairName + '.TraceContextResponse: ';
10+
if(isNil(RequestResponsePairName)){
11+
print(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
12+
logMsg(logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
13+
throw (logPrefix + "ERROR: invalid RequestResponsePairName: " + RequestResponsePairName);
14+
}
15+
logPrefix = RequestResponsePairName + '.TraceContextResponse: ';
16+
717
logMsg(logPrefix + 'enter');
818
var ctxPrefix = 'traceContext.' + RequestResponsePairName;
919

@@ -72,7 +82,7 @@ if ((traceFlags & 0x01) == 1) {
7282
}]
7383

7484
var endpoint = context.getVariable('propertyset.newrelic.ENDPOINT');
75-
var licenseKey = context.getVariable('propertyset.newrelic.LICENSE_KEY');
85+
var licenseKey = contextGetVariable('propertyset.newrelic.LICENSE_KEY', 'https://trace-api.newrelic.com/trace/v1');
7686
if (isNil(licenseKey)) {
7787
print('ERROR: ' + logPrefix + ' missing New Relic LICENSE_KEY: ' + licenseKey);
7888
logMsg(logPrefix + ' ERROR missing New Relic LICENSE_KEY: ' + licenseKey);

src/main/apigee/apiproxies/W3C-Trace-Context/apiproxy/resources/properties/newrelic-sample.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LICENSE_KEY=<YOUR_LICENSE_KEY_HERE>
1212
#
1313
# API reporting endpoint.
1414
# See here: https://docs.newrelic.com/docs/distributed-tracing/trace-api/trace-api-general-requirements-limits/#requirements for other choices
15+
# Default value: https://trace-api.newrelic.com/trace/v1
1516
ENDPOINT=https://trace-api.newrelic.com/trace/v1
1617

1718
# Enable DEBUG to get runtime 'print' statements

0 commit comments

Comments
 (0)