Men-deploy OpenTelemetry Collector Buatan Google di Cloud Run

Dokumen ini menunjukkan cara menjalankan Pengumpul OpenTelemetry Buatan Google di Cloud Run untuk mengumpulkan log, metrik, dan trace OTLP dari aplikasi yang diinstrumentasikan, lalu mengekspor data tersebut ke Google Cloud.

Sebelum memulai

Menjalankan OpenTelemetry Collector Buatan Google memerlukan resource berikut:

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  8. Layanan Cloud Run. Jika Anda tidak memiliki layanan Cloud Run, ikuti petunjuk di Merencanakan dan menyiapkan layanan Anda.
  9. Penginstalan gcloud. Untuk mengetahui informasi tentang cara menginstal gcloud, lihat Menginstal gcloud CLI.

Mengonfigurasi izin untuk Pengumpul

Secara default, tugas dan layanan Cloud Run menggunakan akun layanan default Compute Engine, PROJECT_NUMBER-compute@developer.gserviceaccount.com. Akun layanan ini biasanya memiliki peran Identity and Access Management (IAM) yang diperlukan untuk menulis metrik dan log yang dijelaskan dalam dokumen ini:

Minta administrator Anda untuk memberi Anda peran IAM berikut di project Anda:

Anda juga dapat mengonfigurasi akun layanan yang dikelola pengguna untuk Cloud Run. Akun layanan yang dikelola pengguna juga harus memiliki peran ini. Untuk mengetahui informasi selengkapnya tentang akun layanan untuk Cloud Run, lihat Pengantar identitas layanan.

Men-deploy Pengumpul

Untuk menginstal OpenTelemetry Collector Buatan Google sebagai file bantuan untuk Cloud Run, buat terlebih dahulu secret untuk menyimpan konfigurasi pengumpul.

gcloud secrets create SECRET_NAME --data-file=config.yaml --project=PROJECT_ID

Selanjutnya, tambahkan Google-Built OpenTelemetry Collector sebagai sidecar ke service.yaml Anda:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  annotations:
    run.googleapis.com/launch-stage: ALPHA
  name: google-otel-cloud-run-sample
spec:
  template:
    metadata:
      annotations:
        # [REQUIRED] set the collector as a parent to the app
        run.googleapis.com/container-dependencies: "{app:[collector]}"
        run.googleapis.com/secrets: 'SECRET_NAME:projects/PROJECT_ID/secrets/SECRET_NAME'
    spec:
      containers:
      - image: my-app
        name: app
        ports:
        - containerPort: 8080
        env:
        - name: "OTEL_EXPORTER_OTLP_ENDPOINT"
          value: "http://localhost:4317"
      - image: "us-docker.pkg.dev/cloud-ops-agents-artifacts/google-cloud-opentelemetry-collector/otelcol-google:0.159.0"
        args:
        - --config=/etc/otelcol-google/config.yaml
        name: collector
        startupProbe:
          httpGet:
            path: /
            port: 13133
          timeoutSeconds: 30
          periodSeconds: 30
        livenessProbe:
          httpGet:
            path: /
            port: 13133
          timeoutSeconds: 30
          periodSeconds: 30
        volumeMounts:
        - mountPath: /etc/otelcol-google/
          name: config
      volumes:
      - name: config
        secret:
          items:
          - key: latest
            path: config.yaml
          secretName: 'SECRET_NAME'

Mengonfigurasi Pengumpul

Kami menyediakan konfigurasi OpenTelemetry Collector untuk Anda gunakan dengan Collector buatan Google. Konfigurasi ini dirancang untuk mengirimkan metrik, log, dan rekaman aktivitas OTLP dalam volume tinggi. Konfigurasi ini juga dirancang untuk mencegah masalah penyerapan umum. Anda dapat menambahkan ke konfigurasi, tetapi sebaiknya jangan menghapus elemen.

Bagian ini menjelaskan konfigurasi yang disediakan, komponen utama seperti eksportir, pemroses, penerima, dan komponen lain yang tersedia.

Konfigurasi Pengumpul yang disediakan

Anda dapat menemukan konfigurasi Pengumpul di direktori google-built-opentelemetry-collector pada repositori opentelemetry-operations-collector:

receivers:
  # Open two OTLP servers:
  # - On port 4317, open an OTLP GRPC server
  # - On port 4318, open an OTLP HTTP server
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver
  otlp:
    protocols:
      grpc:
        endpoint: localhost:4317
      http:
        cors:
          # This effectively allows any origin
          # to make requests to the HTTP server.
          allowed_origins:
          - http://*
          - https://*
        endpoint: localhost:4318

processors:
  # The batch processor is in place to regulate both the number of requests
  # being made and the size of those requests.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/batchprocessor
  batch:
    send_batch_max_size: 200
    send_batch_size: 200
    timeout: 5s

  # The memorylimiter will check the memory usage of the collector process.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/memorylimiterprocessor
  memory_limiter:
    check_interval: 1s
    limit_percentage: 65
    spike_limit_percentage: 20

  # The resourcedetection processor is configured to detect GCP resources.
  # Resource attributes that represent the GCP resource the collector is
  # running on will be attached to all telemetry that goes through this
  # processor.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourcedetectionprocessor#gcp-metadata
  resourcedetection:
    detectors: [gcp]
    timeout: 10s

  # The resource/gcp_project_id processor sets the GCP project ID attribute on telemetry data exported via OTLP.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/resourceprocessor
  resource/gcp_project_id:
    attributes:
    - action: insert
      key: gcp.project_id
      value: ${GOOGLE_CLOUD_PROJECT}

  # The metricstarttime processor sets start time on delta metrics for OTLP export.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/metricstarttimeprocessor
  metricstarttime:
    strategy: subtract_initial_point

  transform/collision:
    metric_statements:
    - context: datapoint
      statements:
      - set(attributes["exported_location"], attributes["location"])
      - delete_key(attributes, "location")
      - set(attributes["exported_cluster"], attributes["cluster"])
      - delete_key(attributes, "cluster")
      - set(attributes["exported_namespace"], attributes["namespace"])
      - delete_key(attributes, "namespace")
      - set(attributes["exported_job"], attributes["job"])
      - delete_key(attributes, "job")
      - set(attributes["exported_instance"], attributes["instance"])
      - delete_key(attributes, "instance")
      - set(attributes["exported_project_id"], attributes["project_id"])
      - delete_key(attributes, "project_id")

  # For each Prometheus unknown-typed metric, which is a gauge, create a counter that is an exact copy of this metric.
  # The GCP OTLP endpoint will add appropriate suffixes for the counter and gauge.
  transform/prometheus_unknown_types:
    error_mode: ignore
    metric_statements:
    - context: metric
      statements:
      - copy_metric(Concat([metric.name, "unknowncounter"], ":")) where metric.metadata["prometheus.type"] == "unknown" and not HasSuffix(metric.name, ":unknowncounter")
      - convert_gauge_to_sum("cumulative", true) where HasSuffix(metric.name, ":unknowncounter")
      - set(metric.name, Substring(metric.name, 0, Len(metric.name)-Len(":unknowncounter"))) where HasSuffix(metric.name, ":unknowncounter")

  transform/otlp_grpc/preserve_instrumentation_source_version:
    error_mode: ignore
    log_statements:
    - context: log
      statements:
      - set(attributes["instrumentation_source"], instrumentation_scope.name) where instrumentation_scope.name != ""
      - set(attributes["instrumentation_version"], instrumentation_scope.version) where instrumentation_scope.version != ""

  transform/otlp_grpc/copy_service_resource_labels:
    error_mode: ignore
    log_statements:
    - context: log
      statements:
      - set(attributes["service.name"], resource.attributes["service.name"]) where resource.attributes["service.name"] != nil
      - set(attributes["service.namespace"], resource.attributes["service.namespace"]) where resource.attributes["service.namespace"] != nil
      - set(attributes["service.instance.id"], resource.attributes["service.instance.id"]) where resource.attributes["service.instance.id"] != nil

exporters:
  # The otlp_grpc exporter is used to send telemetry (logs, metrics, traces) to GCP Telemetry API using OTLP gRPC.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter
  otlp_grpc:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth

extensions:
  # Opens an endpoint on 13133 that can be used to check the
  # status of the collector. Since this does not configure the
  # `path` config value, the endpoint will default to `/`.
  #
  # When running on Cloud Run, this extension is required and not optional.
  # In other environments it is recommended but may not be required for operation
  # (i.e. in Container-Optimized OS or other GCE environments).
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/healthcheckextension
  health_check:
    endpoint: 0.0.0.0:13133
  googleclientauth:

service:
  extensions:
  - health_check
  - googleclientauth
  pipelines:
    logs:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - transform/otlp_grpc/preserve_instrumentation_source_version
      - transform/otlp_grpc/copy_service_resource_labels
      - memory_limiter
      - batch
      exporters:
      - otlp_grpc
    metrics/otlp:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - transform/collision
      - transform/prometheus_unknown_types
      - metricstarttime
      - memory_limiter
      - batch
      exporters:
      - otlp_grpc
    traces:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - memory_limiter
      - batch
      exporters:
      - otlp_grpc
  # Internal telemetry for the collector supports both push and pull-based telemetry data transmission.
  # Leveraging the pre-configured OTLP receiver eliminates the need for an additional port.
  #
  # Docs:
  # https://opentelemetry.io/docs/collector/internal-telemetry/
  telemetry:
    metrics:
      readers:
        - periodic:
            exporter:
              otlp:
                protocol: grpc
                endpoint: http://localhost:4317
                insecure: true

Pengekspor

Konfigurasi Pengumpul menggunakan hal berikut:

Prosesor

Konfigurasi Pengumpul mencakup pemroses berikut:

  • batch: Dikonfigurasi untuk mengelompokkan permintaan telemetri pada Google Cloud jumlah maksimum entri per permintaan, atau pada Google Cloud interval minimum setiap 5 detik (mana saja yang lebih dulu).

  • memory_limiter: Membatasi penggunaan memori Pengumpul untuk mencegah error kehabisan memori dengan menghapus titik data saat batas terlampaui.

  • resourcedetection: Mendeteksi Google Cloud label resource secara otomatis seperti project_id.

Penerima

Konfigurasi Pengumpul hanya mencakup penerima otlp. Untuk mengetahui informasi tentang cara menginstrumentasi aplikasi Anda untuk mengirim metrik dan rekaman aktivitas OTLP ke endpoint OTLP Collector, lihat Memilih pendekatan instrumentasi.

Komponen yang tersedia

Pengumpul OpenTelemetry Buatan Google berisi komponen yang dibutuhkan sebagian besar pengguna untuk mengaktifkan pengalaman yang kaya dalam Google Cloud Observability. Untuk mengetahui daftar lengkap komponen yang tersedia, lihat Komponen di repositori opentelemetry-operations-collector.

Untuk meminta perubahan atau penambahan pada komponen yang tersedia, buka permintaan fitur di repositori opentelemetry-operations-collector.

Membuat telemetri

Untuk menghasilkan telemetri, Anda membuat aplikasi Cloud Run dengan pengumpul container bantuan. Dokumen Menulis metrik OTLP menggunakan file bantuan OpenTelemetry memberikan tutorial untuk menggunakan Google-Built OpenTelemetry Collector sebagai file bantuan. Anda dapat menggunakan tutorial ini untuk membuat telemetri dengan Pengumpul yang dibuat Google.

Melihat telemetri

Google-Built OpenTelemetry Collector mengirim metrik, log, dan trace dari aplikasi yang diinstrumentasikan ke Google Cloud Observability. Kolektor juga mengirimkan metrik pengamatan mandiri. Bagian berikut menjelaskan cara melihat telemetri ini.

Melihat metrik Anda

Pengumpul OpenTelemetry Buatan Google mengumpulkan metrik Prometheus yang dapat Anda lihat menggunakan Metrics Explorer. Metrik yang dikumpulkan bergantung pada instrumentasi aplikasi, meskipun Pengumpul yang dibuat Google juga menulis beberapa metrik mandiri.

Untuk melihat metrik yang dikumpulkan oleh OpenTelemetry Collector Buatan Google, lakukan langkah-langkah berikut:
  1. Di konsol Google Cloud , buka halaman  Metrics explorer:

    Buka Metrics explorer

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Monitoring.

  2. Di toolbar konsol Google Cloud , pilih project Google Cloud Anda. Untuk konfigurasi Pusat Aplikasi, pilih project host Pusat Aplikasi atau project pengelolaan folder khusus aplikasi.
  3. Pada elemen Metrik, luaskan menu Pilih metrik, masukkan Prometheus Target di panel filter, lalu gunakan submenu untuk memilih jenis dan metrik resource tertentu:
    1. Di menu Active resources, pilih Prometheus Target.
    2. Untuk memilih metrik, gunakan menu Kategori metrik aktif dan Metrik aktif. Metrik yang dikumpulkan oleh OpenTelemetry Collector Buatan Google memiliki awalan prometheus.googleapis.com.
    3. Klik Terapkan.
  4. Untuk menambahkan filter, yang menghapus deret waktu dari hasil kueri, gunakan elemen Filter.

  5. Konfigurasi cara data dilihat.

    Jika pengukuran untuk metrik bersifat kumulatif, Metrics Explorer akan otomatis menormalisasi data yang diukur berdasarkan periode perataan, sehingga diagram menampilkan rasio. Untuk mengetahui informasi selengkapnya, lihat Jenis, tipe, dan konversi.

    Saat nilai bilangan bulat atau ganda diukur, seperti dengan metrik counter, Metrics Explorer akan otomatis menjumlahkan semua deret waktu. Untuk mengubah perilaku ini, tetapkan menu pertama entri Aggregation ke None.

    Untuk informasi selengkapnya tentang cara mengonfigurasi diagram, lihat Memilih metrik saat menggunakan Metrics Explorer.

Melihat trace Anda

Untuk melihat data rekaman aktivitas, lakukan hal berikut:

  1. Di konsol Google Cloud , buka halaman Trace explorer:

    Buka Trace Explorer

    Anda juga dapat menemukan halaman ini dengan menggunakan kotak penelusuran.

  2. Di toolbar konsol Google Cloud , pilih project Google Cloud Anda. Untuk konfigurasi Pusat Aplikasi, pilih project host Pusat Aplikasi atau project pengelolaan.
  3. Di bagian tabel halaman, pilih baris.
  4. Pada diagram Gantt di panel Trace details, pilih rentang.

    Panel yang menampilkan informasi tentang permintaan yang dilacak akan terbuka. Detail ini mencakup metode, kode status, jumlah byte, dan agen pengguna pemanggil.

  5. Untuk melihat log yang terkait dengan rekaman aktivitas ini, pilih tab Logs & Events.

    Tab ini menampilkan log individual. Untuk melihat detail entri log, luaskan entri log. Anda juga dapat mengklik Lihat Log dan melihat log menggunakan Logs Explorer.

Untuk mengetahui informasi selengkapnya tentang cara menggunakan penjelajah Cloud Trace, lihat Menemukan dan menjelajahi rekaman aktivitas.

Melihat log

Dari Logs Explorer, Anda dapat memeriksa log, dan Anda juga dapat melihat rekaman aktivitas terkait, jika ada.

  1. Di konsol Google Cloud , buka halaman Logs Explorer:

    Buka Logs Explorer

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Logging.

  2. Temukan entri log dari aplikasi yang diinstrumentasi. Untuk melihat detailnya, luaskan entri log.

  3. Klik Traces pada entri log dengan pesan aktivitas, lalu pilih View trace details.

    Panel Trace details akan terbuka dan menampilkan rekaman aktivitas yang dipilih.

Untuk mengetahui informasi selengkapnya tentang cara menggunakan Logs Explorer, lihat Melihat log menggunakan Logs Explorer.

Mengamati dan men-debug Pengumpul

Pengumpul OpenTelemetry Buatan Google secara otomatis menyediakan metrik kemampuan pengamatan mandiri untuk membantu Anda memantau performanya dan memastikan pipeline penyerapan OTLP terus beroperasi.

Untuk memantau Pengumpul, instal contoh dasbor untuk Pengumpul. Dasbor ini menawarkan insight sekilas tentang beberapa metrik dari Pengumpul, termasuk waktu aktif, penggunaan memori, dan panggilan API ke Google Cloud Observability.

Untuk menginstal dasbor, lakukan hal berikut:

  1. Di konsol Google Cloud , buka halaman  Dashboards:

    Buka Dasbor

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Monitoring.

  2. Klik Dashboard Templates.
  3. Telusuri dasbor OpenTelemetry Collector.
  4. Opsional: Untuk melihat pratinjau dasbor, pilih dasbor.
  5. Klik Tambahkan dasbor ke daftar Anda, lalu selesaikan dialog.

    Dialog ini memungkinkan Anda memilih nama dasbor, dan menambahkan label ke dasbor.

Untuk mengetahui informasi selengkapnya tentang cara menginstal dasbor, lihat Menginstal template dasbor.