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

Commit

Permalink
Allow specifying apiEndpoint for stackdriver exporters (#985)
Browse files Browse the repository at this point in the history
* feat: allow specifying apiEndpoint for stackdriver exporters
  • Loading branch information
dashpole committed May 26, 2022
1 parent 1c12e6f commit 3ee9422
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@opencensus/core';
import { logger, Logger } from '@opencensus/core';
import { auth as globalAuth, GoogleAuth, JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { google, cloudtrace_v2 } from 'googleapis';
import { getDefaultResource } from './common-utils';
import {
createAttributes,
Expand All @@ -39,7 +39,6 @@ import {
} from './types';

google.options({ headers: { 'x-opencensus-outgoing-request': 0x1 } });
const cloudTrace = google.cloudtrace('v2');
let auth = globalAuth;

/** Format and sends span information to Stackdriver */
Expand All @@ -49,6 +48,7 @@ export class StackdriverTraceExporter implements Exporter {
logger: Logger;
failBuffer: SpanContext[] = [];
private RESOURCE_LABELS: Promise<Record<string, AttributeValue>>;
private cloudTrace: cloudtrace_v2.Cloudtrace;

constructor(options: StackdriverExporterOptions) {
this.projectId = options.projectId;
Expand All @@ -63,6 +63,11 @@ export class StackdriverTraceExporter implements Exporter {
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
}
this.cloudTrace = google.cloudtrace({
version: 'v2',
rootUrl:
'https://' + (options.apiEndpoint || 'cloudtrace.googleapis.com'),
});
}

/**
Expand Down Expand Up @@ -165,7 +170,7 @@ export class StackdriverTraceExporter implements Exporter {
// data to backend :
// https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.
// cloudtrace.v2#google.devtools.cloudtrace.v2.TraceService
cloudTrace.projects.traces.batchWrite(spans, (err: Error | null) => {
this.cloudTrace.projects.traces.batchWrite(spans, (err: Error | null) => {
if (err) {
err.message = `batchWriteSpans error: ${err.message}`;
this.logger.error(err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
View,
} from '@opencensus/core';
import { auth as globalAuth, GoogleAuth, JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { google, monitoring_v3 } from 'googleapis';
import { getDefaultResource } from './common-utils';
import {
createMetricDescriptorData,
Expand All @@ -50,7 +50,6 @@ const OC_HEADER = {
};

google.options({ headers: OC_HEADER });
const monitoring = google.monitoring('v3');
let auth = globalAuth;

/** Format and sends Stats to Stackdriver */
Expand All @@ -71,6 +70,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
> = new Map();
private DEFAULT_RESOURCE: Promise<MonitoredResource>;
logger: Logger;
private monitoring: monitoring_v3.Monitoring;

constructor(options: StackdriverExporterOptions) {
this.period =
Expand All @@ -93,6 +93,11 @@ export class StackdriverStatsExporter implements StatsEventListener {
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
}
this.monitoring = google.monitoring({
version: 'v3',
rootUrl:
'https://' + (options.apiEndpoint || 'monitoring.googleapis.com'),
});
}

/**
Expand Down Expand Up @@ -195,7 +200,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
};

return new Promise((resolve, reject) => {
monitoring.projects.timeSeries.create(
this.monitoring.projects.timeSeries.create(
request,
{ headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT] },
(err: Error | null) => {
Expand Down Expand Up @@ -224,7 +229,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
};

return new Promise((resolve, reject) => {
monitoring.projects.metricDescriptors.create(
this.monitoring.projects.metricDescriptors.create(
request,
{ headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT] },
(err: Error | null) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/opencensus-exporter-stackdriver/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export interface StackdriverExporterOptions extends ExporterConfig {
* instead of your application default credentials. Optional
*/
credentials?: JWTInput;
/**
* The endpoint of the service. Defaults to cloudtrace.googleapis.com
* for trace, and monitoring.googleapis.com for monitoring.
*/
apiEndpoint?: string;

/**
* Is called whenever the exporter fails to upload metrics to stackdriver.
Expand Down

0 comments on commit 3ee9422

Please sign in to comment.