question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stackdriver Trace Integration

See original GitHub issue

Stackdriver Trace Integration

Add Stackdriver Trace support to the Functions Framework.

Library: https://github.com/googleapis/cloud-trace-nodejs Google Issue: https://issuetracker.google.com/issues/124761993

@stew-r Can you add details as to what this would do? Would this be a tutorial or part of the actual framework?

Example Code

"@google-cloud/debug-agent": "^4.0.1",
"@google-cloud/profiler": "^2.0.2",
"@google-cloud/trace-agent": "^4.1.0",
import * as debugAgent from '@google-cloud/debug-agent';
import * as profiler from '@google-cloud/profiler';
import * as trace from '@google-cloud/trace-agent';

let toolsAreSetup = false;

/**
 * Starts Google Cloud tooling.
 * - StackDriver Trace
 * - StackDriver Profiler
 * - StackDriver Debugger
 */
export function startTooling() {
  // Ensure tools are only setup once per process.
  if (toolsAreSetup) {
    return null;
  } else {
    toolsAreSetup = true;
  }

  // Start Trace
  trace.start();

  // Start profiler
  profiler
    .start({
      serviceContext: {
        service: getServiceName(),
        version: 'v1',
      },
    })
    .catch(err => {
      console.error('Failed to start profiler', err);
    });

  // Start debugger
  const debug = debugAgent.start({
    serviceContext: {
      service: getServiceName(),
      version: 'v1',
      // version: functions.config().tsc.version
    },
  });

  // Return all tools
  return {
    trace,
    profiler,
    debug,
  };
}

/**
 * Gets the name of the function for logging.
 * @returns {string} The name of the function with a '_function' suffix.
 */
function getServiceName(): string {
  const serviceName = process.env.FUNCTION_TARGET || 'unknown_cloud';
  return `${serviceName}_function`;
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
sk-commented, Sep 25, 2020

@grant Below is the config helper we are using in our codebase, with some comments to clarify what each config means. Note that it’s not necessary to explicitly add the plugins, as those will be autodetected. Note also that this code is using OpenTelemetry 0.10, as there are some breaking changes in the last version and the google pacakges have not been updated yet.

const opentelemetry = require('@opentelemetry/api');
const {
  TraceExporter,
} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { BatchSpanProcessor } = require('@opentelemetry/tracing');
const {
  CompositePropagator,
  HttpCorrelationContext,
  HttpTraceContext,
  ProbabilitySampler,
} = require('@opentelemetry/core');
const {
  CloudPropagator,
} = require('@google-cloud/opentelemetry-cloud-trace-propagator');

exports.initTracing = () => {
  const provider = new NodeTracerProvider({
    // This is important to keep the cost reasonable
    sampler: new ProbabilitySampler(0.01),
  });
  provider.register({
    // Use CloudPropagator + defaults
    propagator: new CompositePropagator({
      propagators: [
        new HttpCorrelationContext(),
        new HttpTraceContext(),
        // The first 2 propagators are the default ones, and the CloudPropagator is the one used to extract Google defined headers.
        new CloudPropagator(),
      ],
    }),
  });

  // This is the exporter used to send the traces to Google Cloud
  const exporter = new TraceExporter();

  provider.addSpanProcessor(new BatchSpanProcessor(exporter));
  opentelemetry.trace.setGlobalTracerProvider(provider);
};
1reaction
kintelcommented, Aug 1, 2020

@grant I think your suggestion will work, but keep in mind that @google-cloud/trace-agent is will be deprecated in favour of OpenTelemetry sometime the next 2-6 months.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloud Trace | Google Cloud
Cloud Trace is a distributed tracing system that collects latency data from your applications and finds performance bottlenecks in production.
Read more >
Introduction to Stackdriver Distributed Tracing (Part 1) - Medium
Introduction to Stackdriver Distributed Tracing (Part 1) · 1 — gRPC Server · 2 — gRPC Client · 3 — Trace Instrumentation ·...
Read more >
Micronaut GCP
Sets the location to the service account credential key file. 3 Stackdriver Trace. Improve this doc. The micronaut-gcp-tracing integrates Micronaut with Cloud ...
Read more >
10. Spring Cloud Sleuth
You must enable Stackdriver Trace API from the Google Cloud Console in order to ... This integration enables Brave to use the StackdriverTracePropagation...
Read more >
Distributed tracing with Spring Cloud Sleuth and Cloud Trace
Use a Stackdriver Trace Zipkin Proxy and simply configure Spring Cloud ... Or, use Spring Cloud GCP Trace, which seamlessly integrates with Spring...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found