Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add server timing header injection #70

Merged
merged 9 commits into from
Apr 6, 2021
Merged

feat: add server timing header injection #70

merged 9 commits into from
Apr 6, 2021

Conversation

seemk
Copy link
Contributor

@seemk seemk commented Apr 1, 2021

Description

Enable injection of Server-Timing response headers. This also works if an user provides their own HttpInstrumentation with an existing response hook, so no additional code changes are needed for them except enabling the feature.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Tested manually with express and koa.

  • Tested manually
  • Added automated tests

Checklist:

  • Changelogs have been updated
  • Unit tests have been added/updated
  • Documentation has been updated

@seemk seemk requested a review from owais April 1, 2021 14:30
Copy link
Contributor

@owais owais left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some minor comments.

README.md Outdated
@@ -68,6 +68,7 @@ You can also instrument your app with code as described [here](#instrument-with-
| SPLUNK_SERVICE_NAME | serviceName | `unnamed-node-service` | The service name of this Node service. |
| SPLUNK_ACCESS_TOKEN | acceessToken | | | The optional organization access token for trace submission requests. |
| SPLUNK_MAX_ATTR_LENGTH | maxAttrLength | 1200 | Maximum length of string attribute value in characters. Longer values are truncated. |
| SPLUNK_SERVER_TIMING_ENABLED | serverTimingEnabled | false | Enable injection of `Server-Timing` header to HTTP responses. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was decided to default to true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java and SFX Py use SPLUNK_CONTEXT_SERVER_TIMING_ENABLED so we should use that in this PR.

I think we should rename it to something more generic. More here: signalfx/gdi-specification#27

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed! And agree with your proposal 🙂

src/tracing.ts Outdated Show resolved Hide resolved
src/tracing.ts Outdated
if (response instanceof ServerResponse) {
const { traceId, spanId } = span.context();
response.setHeader('Access-Control-Expose-Headers', 'Server-Timing');
response.setHeader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will setHeader append or overwrite any existing values? We should be appending in order to not break any services that might use these two headers in a different way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current http instrumentation, the response hook is called before users have access to the response object (https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-plugin-http/src/http.ts#L316)

However if this gets changed in future versions so it's called somewhere later down the line, the header could be overwritten 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added appending of headers: ddccfb7#diff-8e40ce0d3dab91400c9a4d6c3c2577a8222c65dc14f886a2b570b564909bef01R81-R97

Although this will take the first branch 100% of the time, since response is not available for users to modify before the hook is called.

src/tracing.ts Show resolved Hide resolved
src/tracing.ts Outdated
}
};

let config = instrumentation._getConfig() as HttpInstrumentationConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this result in runtime error? can we add guards in case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess only if the logic of getConfig changes (e.g. adding required arguments), currently it only returns an existing dictionary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. As long as we have a test case that detect such breaking I think we should be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be covered by tests in test/servertiming.test.ts

test/options.test.ts Show resolved Hide resolved
@seemk
Copy link
Contributor Author

seemk commented Apr 5, 2021

Added a check for sampling flags (as discussed on Slack)

@seemk
Copy link
Contributor Author

seemk commented Apr 5, 2021

Server-Timing is now added by default

@iNikem
Copy link
Contributor

iNikem commented Apr 6, 2021

@owais @seemk Anything else needed before it can be merged?

@owais
Copy link
Contributor

owais commented Apr 6, 2021

Nothing from my side.

@seemk seemk merged commit 50d3bb0 into main Apr 6, 2021
@seemk seemk deleted the server-timings branch April 6, 2021 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants