This document illustrates how you can send metrics directly from applications
to the OTLP endpoint by using an SDK and the gRPC exporter, which is
the recommended exporter for use with SDKs.
The OTLP endpoint supports all of the OTLP protocols, including
http/proto, http/json, and grpc. For more information, see
Protocol support.
When you send application metrics directly to the OTLP endpoint, the OpenTelemetry Collector does not handle authentication and enrichment for you. When using SDKs to send metrics from applications, you need to do the following:
- Configure credentials for the OTLP gRPC exporter to use Google Application Default Credentials, as described in Get authentication credentials.
- Detect Google Cloud and Kubernetes resources from the application. How you do this depends on the application you're running.
Before you begin
To run the examples, you must enable the necessary APIs and acquire authentication credentials.
Enable APIs
Enable the Cloud Monitoring API and the Telemetry API in your Google Cloud project by running the following command:
gcloud services enable monitoring.googleapis.com telemetry.googleapis.com
Get authentication credentials
Configure credentials for the OTLP gRPC exporter to use Google Application Default Credentials by running the following command:
gcloud auth application-default login
Language-specific examples
This section provides a selection of language-specific samples that create and write a counter metric. Select a tab for information about running the sample and to view the code.
Go
For information about configuring and running this example, see the
README file for the
Go sample.
Java
For information about configuring and running this example, see the
README file for the
Java sample.
NodeJS
For information about configuring and running this example, see the
README file for the
NodeJS sample.
Python
For information about running this example, see the README file for the
README file for the
Python sample.
Use the downward API for applications running as pods
If you are running the application as a pod in Kubernetes and are not sending through the OpenTelemetry Collector, make sure to use the downward API to set Kubernetes resource attributes:
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NAMESPACE_NAME
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONTAINER_NAME
value: my-container-name
- name: OTEL_RESOURCE_ATTRIBUTES
value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME)
What's next
- For information about using an OpenTelemetry Collector and the Telemetry API with OpenTelemetry zero-code instrumentation, see Use OpenTelemetry zero-code instrumentation for Java.
- For information about migrating to the
otlphttpexporter from another exporter, see Migrate to the OTLP exporter. - To learn more about the Telemetry API, see Telemetry (OTLP) API overview.