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 semconv generator for semantic-conventions-package #2083

Merged
merged 17 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c131413
feat: add semconv generator for `semantic-conventions`-package
tapico-weyert Apr 8, 2021
4724305
feat: update `semantic-conventations`-package by generating using new…
tapico-weyert Apr 8, 2021
310d124
fix: update packages to use the new `SemanticAttribute`-class
tapico-weyert Apr 8, 2021
5b40934
fix: improve the `semconv`-template to better match eslint prettier r…
tapico-weyert Apr 8, 2021
3e9f64e
chore: run `lint:fix`-task after generating source files
tapico-weyert Apr 8, 2021
3302972
chore: move argument to its own line in `generate.sh`
tapico-weyert Apr 8, 2021
9b889ef
fix: ensure we generate against v1.1.0 of the spec
tapico-weyert Apr 8, 2021
55d2c9b
fix: removed the out of spec semantic conventions from package
tapico-weyert Apr 8, 2021
3ff5b47
chore: merge branch 'main' into chore-otel-add-semconv-generator
tapico-weyert Apr 8, 2021
a8b4115
style: ran `npm run lint:fix` on the code base
tapico-weyert Apr 8, 2021
c2fb34a
fix: use `RPC_GRPC_STATUS_CODE` from `semantic-convention` that a cus…
tapico-weyert Apr 8, 2021
4b16cff
test: fix Span test to ensure attribute value is a string
tapico-weyert Apr 8, 2021
79c9720
chore: merge branch 'upstream-main' into chore-otel-add-semconv-gener…
tapico-weyert Apr 9, 2021
8eda347
chore: attempt to correct fix failed merge
tapico-weyert Apr 9, 2021
5b45d5b
fix: remove unwanted `@deprecated`-attribute in semconv template file
tapico-weyert Apr 10, 2021
50ce031
fix: don't fetch the whole otel specification repo
tapico-weyert Apr 10, 2021
628bb71
Merge branch 'main' into chore-otel-add-semconv-generator
weyert Apr 10, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
* https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md
*/
export enum AttributeNames {
COMPONENT = 'component',
COMPONENT = "component",
HTTP_ERROR_NAME = "http.error_name",
HTTP_STATUS_TEXT = "http.status_text",
}
71 changes: 34 additions & 37 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
* limitations under the License.
*/

import * as api from '@opentelemetry/api';
import * as api from "@opentelemetry/api";
dyladan marked this conversation as resolved.
Show resolved Hide resolved
import {
isWrapped,
InstrumentationBase,
InstrumentationConfig,
} from '@opentelemetry/instrumentation';
import * as core from '@opentelemetry/core';
import * as web from '@opentelemetry/web';
import { AttributeNames } from './enums/AttributeNames';
import { SemanticAttribute } from '@opentelemetry/semantic-conventions';
import { FetchError, FetchResponse, SpanData } from './types';
import { VERSION } from './version';
} from "@opentelemetry/instrumentation";
import * as core from "@opentelemetry/core";
import * as web from "@opentelemetry/web";
import { AttributeNames } from "./enums/AttributeNames";
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
import { FetchError, FetchResponse, SpanData } from "./types";
import { VERSION } from "./version";

// how long to wait for observer to collect information about resources
// this is needed as event "load" is called before observer
Expand All @@ -37,7 +37,7 @@ const OBSERVER_WAIT_TIME_MS = 300;
let a: HTMLAnchorElement | undefined;
const getUrlNormalizingAnchor = () => {
if (!a) {
a = document.createElement('a');
a = document.createElement("a");
}

return a;
Expand Down Expand Up @@ -69,15 +69,15 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
export class FetchInstrumentation extends InstrumentationBase<
Promise<Response>
> {
readonly component: string = 'fetch';
readonly component: string = "fetch";
readonly version: string = VERSION;
moduleName = this.component;
private _usedResources = new WeakSet<PerformanceResourceTiming>();
private _tasksCount = 0;

constructor(config: FetchInstrumentationConfig = {}) {
super(
'@opentelemetry/instrumentation-fetch',
"@opentelemetry/instrumentation-fetch",
VERSION,
Object.assign({}, config)
);
Expand All @@ -99,7 +99,7 @@ export class FetchInstrumentation extends InstrumentationBase<
corsPreFlightRequest: PerformanceResourceTiming
): void {
const childSpan = this.tracer.startSpan(
'CORS Preflight',
"CORS Preflight",
{
startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],
},
Expand All @@ -121,19 +121,16 @@ export class FetchInstrumentation extends InstrumentationBase<
response: FetchResponse
): void {
const parsedUrl = web.parseUrl(response.url);
span.setAttribute(SemanticAttribute.HTTP_STATUS_CODE, response.status);
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
if (response.statusText != null) {
span.setAttribute(
SemanticAttribute.HTTP_STATUS_TEXT,
response.statusText
);
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
}
span.setAttribute(SemanticAttribute.HTTP_HOST, parsedUrl.host);
span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
span.setAttribute(
SemanticAttribute.HTTP_SCHEME,
parsedUrl.protocol.replace(':', '')
SemanticAttributes.HTTP_SCHEME,
parsedUrl.protocol.replace(":", "")
);
span.setAttribute(SemanticAttribute.HTTP_USER_AGENT, navigator.userAgent);
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);
}

/**
Expand All @@ -153,7 +150,7 @@ export class FetchInstrumentation extends InstrumentationBase<

if (options instanceof Request) {
api.propagation.inject(api.context.active(), options.headers, {
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
set: (h, k, v) => h.set(k, typeof v === "string" ? v : String(v)),
});
} else {
const headers: Partial<Record<string, unknown>> = {};
Expand Down Expand Up @@ -185,17 +182,17 @@ export class FetchInstrumentation extends InstrumentationBase<
options: Partial<Request | RequestInit> = {}
): api.Span | undefined {
if (core.isUrlIgnored(url, this._getConfig().ignoreUrls)) {
api.diag.debug('ignoring span as url matches ignored url');
api.diag.debug("ignoring span as url matches ignored url");
return;
}
const method = (options.method || 'GET').toUpperCase();
const method = (options.method || "GET").toUpperCase();
const spanName = `HTTP ${method}`;
return this.tracer.startSpan(spanName, {
kind: api.SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: this.moduleName,
[SemanticAttribute.HTTP_METHOD]: method,
[SemanticAttribute.HTTP_URL]: url,
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_URL]: url,
},
});
}
Expand All @@ -220,7 +217,7 @@ export class FetchInstrumentation extends InstrumentationBase<
// then OBSERVER_WAIT_TIME_MS and observer didn't collect enough
// information
resources = performance.getEntriesByType(
'resource'
"resource"
) as PerformanceResourceTiming[];
}
const resource = web.getResource(
Expand All @@ -229,7 +226,7 @@ export class FetchInstrumentation extends InstrumentationBase<
endTime,
resources,
this._usedResources,
'fetch'
"fetch"
);

if (resource.mainRequest) {
Expand Down Expand Up @@ -366,25 +363,25 @@ export class FetchInstrumentation extends InstrumentationBase<
private _prepareSpanData(spanUrl: string): SpanData {
const startTime = core.hrTime();
const entries: PerformanceResourceTiming[] = [];
if (typeof window.PerformanceObserver === 'undefined') {
if (typeof window.PerformanceObserver === "undefined") {
return { entries, startTime, spanUrl };
}

const observer: PerformanceObserver = new PerformanceObserver(list => {
const observer: PerformanceObserver = new PerformanceObserver((list) => {
const perfObsEntries = list.getEntries() as PerformanceResourceTiming[];
const urlNormalizingAnchor = getUrlNormalizingAnchor();
urlNormalizingAnchor.href = spanUrl;
perfObsEntries.forEach(entry => {
perfObsEntries.forEach((entry) => {
if (
entry.initiatorType === 'fetch' &&
entry.initiatorType === "fetch" &&
entry.name === urlNormalizingAnchor.href
) {
entries.push(entry);
}
});
});
observer.observe({
entryTypes: ['resource'],
entryTypes: ["resource"],
});
return { entries, observer, startTime, spanUrl };
}
Expand All @@ -394,17 +391,17 @@ export class FetchInstrumentation extends InstrumentationBase<
*/
enable() {
if (isWrapped(window.fetch)) {
this._unwrap(window, 'fetch');
api.diag.debug('removing previous patch for constructor');
this._unwrap(window, "fetch");
api.diag.debug("removing previous patch for constructor");
}
this._wrap(window, 'fetch', this._patchConstructor());
this._wrap(window, "fetch", this._patchConstructor());
}

/**
* implements unpatch function
*/
disable() {
this._unwrap(window, 'fetch');
this._unwrap(window, "fetch");
this._usedResources = new WeakSet<PerformanceResourceTiming>();
}
}
Loading