| 1 |
# GCP Resource detector |
| 2 |
|
| 3 |
The GCP resource detector supports detecting resources on: |
| 4 |
|
| 5 |
* Google Compute Engine (GCE) |
| 6 |
* Google Kubernetes Engine (GKE) |
| 7 |
* Google App Engine (GAE) |
| 8 |
* Cloud Run |
| 9 |
* Cloud Run jobs |
| 10 |
* Cloud Functions |
| 11 |
|
| 12 |
## Usage |
| 13 |
|
| 14 |
```golang |
| 15 |
ctx := context.Background() |
| 16 |
// Detect your resources |
| 17 |
res, err := resource.New(ctx, |
| 18 |
// Use the GCP resource detector! |
| 19 |
resource.WithDetectors(gcp.NewDetector()), |
| 20 |
// Keep the default detectors |
| 21 |
resource.WithTelemetrySDK(), |
| 22 |
// Add your own custom attributes to identify your application |
| 23 |
resource.WithAttributes( |
| 24 |
semconv.ServiceNameKey.String("my-application"), |
| 25 |
semconv.ServiceNamespaceKey.String("my-company-frontend-team"), |
| 26 |
), |
| 27 |
) |
| 28 |
if err != nil { |
| 29 |
// Handle err |
| 30 |
} |
| 31 |
// Use the resource in your tracerprovider (or meterprovider) |
| 32 |
tp := trace.NewTracerProvider( |
| 33 |
// ... other options |
| 34 |
trace.WithResource(res), |
| 35 |
) |
| 36 |
``` |
| 37 |
|
| 38 |
## Setting Kubernetes attributes |
| 39 |
|
| 40 |
Previous iterations of GCP resource detection attempted to detect |
| 41 |
`container.name`, `k8s.pod.name` and `k8s.namespace.name`. When using this detector, |
| 42 |
you should use this in your Pod Spec to set these using |
| 43 |
[`OTEL_RESOURCE_ATTRIBUTES`](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable): |
| 44 |
|
| 45 |
```yaml |
| 46 |
env: |
| 47 |
- name: POD_NAME |
| 48 |
valueFrom: |
| 49 |
fieldRef: |
| 50 |
fieldPath: metadata.name |
| 51 |
- name: NAMESPACE_NAME |
| 52 |
valueFrom: |
| 53 |
fieldRef: |
| 54 |
fieldPath: metadata.namespace |
| 55 |
- name: CONTAINER_NAME |
| 56 |
value: my-container-name |
| 57 |
- name: OTEL_RESOURCE_ATTRIBUTES |
| 58 |
value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME) |
| 59 |
|
| 60 |
``` |
| 61 |
To have a detector unpack the `OTEL_RESOURCE_ATTRIBUTES` envvar, use the `WithFromEnv` option: |
| 62 |
|
| 63 |
```golang |
| 64 |
... |
| 65 |
// Detect your resources |
| 66 |
res, err := resource.New(ctx, |
| 67 |
resource.WithDetectors(gcp.NewDetector()), |
| 68 |
resource.WithTelemetrySDK(), |
| 69 |
resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES |
| 70 |
// Add your own custom attributes to identify your application |
| 71 |
resource.WithAttributes( |
| 72 |
semconv.ServiceNameKey.String("my-application"), |
| 73 |
semconv.ServiceNamespaceKey.String("my-company-frontend-team"), |
| 74 |
), |
| 75 |
) |
| 76 |
... |
| 77 |
``` |