פריסת Google-Built OpenTelemetry Collector ב-Cloud Run

כדי לאסוף נתוני OTLP של יומנים, מדדים ומעקבים מאפליקציות עם אינסטרומנטציה ולייצא את הנתונים האלה אל Google Cloud, אתם יכולים להריץ את Google-Built OpenTelemetry Collector ב-Cloud Run כקונטיינר משני (sidecar).

לפני שמתחילים

כדי להריץ את Google-Built OpenTelemetry Collector, צריך את המשאבים הבאים:

  1. 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

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

  3. Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs, if any are not already enabled.

    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

  4. שירות Cloud Run. אם אין לכם שירות Cloud Run, פועלים לפי ההוראות במאמר תכנון והכנה של השירות.
  5. התקנה של gcloud. מידע נוסף על התקנת gcloud זמין במאמר התקנת ה-CLI של gcloud.

הגדרת הרשאות ל-Collector

כברירת מחדל, משימות ושירותים של Cloud Run משתמשים בחשבון השירות שמוגדר כברירת מחדל של Compute Engine, ‏PROJECT_NUMBER-compute@. בדרך כלל, לחשבון השירות הזה יש את התפקידים בניהול הזהויות והרשאות הגישה (IAM) שנדרשים כדי לכתוב את המדדים והיומנים שמתוארים במאמר הזה:

בקשו מהאדמין להקצות לכם בפרויקט את תפקידי ה-IAM הבאים:

אתם יכולים גם להגדיר חשבון שירות שמנוהל על ידי משתמש בשביל Cloud Run. גם לחשבון שירות שמנוהל על ידי משתמש צריכים להיות התפקידים האלה. מידע נוסף על חשבונות שירות ל-Cloud Run זמין במאמר מבוא לזהות שירות.

פריסת ה-Collector

כדי להתקין את Google-Built OpenTelemetry Collector כ-sidecar ל-Cloud Run, קודם צריך ליצור סוד לאחסון ההגדרות של ה-collector.

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

בשלב הבא, מוסיפים את Google-Built OpenTelemetry Collector כ-sidecar ל-service.yaml:

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.165.2-r.1"
        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'

הגדרת ה-Collector

אנחנו מספקים הגדרה של OpenTelemetry Collector לשימוש עם ה-Collector ש-Google פיתחה. ההגדרה הזו נועדה להעביר כמויות גדולות של מדדים, יומנים ומעקבים של OTLP. ההגדרה הזו גם נועדה למנוע בעיות נפוצות בהטמעה. אפשר להוסיף להגדרות, אבל מומלץ מאוד לא להסיר רכיבים.

בקטע הזה מתוארים ההגדרות שסופקו, הרכיבים העיקריים כמו מייצאים, מעבדים, מקלטים ורכיבים זמינים אחרים.

הגדרת Collector שסופקה

אפשר למצוא את הגדרות ה-Collector בספרייה google-built-opentelemetry-collector במאגר 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 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:
  # Dedicated OTLP gRPC exporters are configured per signal (logs, metrics, traces)
  # so each sending queue and batch size matches the Cloud Telemetry API quotas:
  # https://docs.cloud.google.com/stackdriver/quotas
  #
  # Note: In addition to per-request item limits (8,192 for logs/traces, 200 for
  # metrics), the API enforces a 5 MiB request size limit. An 8,192-item batch
  # stays under 5 MiB only when the average log or span is under ~640 bytes. If
  # your payloads are larger, reduce `max_size` or set `sending_queue.batch.sizer: bytes`
  # and update `min_size` and `max_size` with byte limits that make sense for your deployment.
  #
  # For `telemetry.googleapis.com:443`, `sending_queue.num_consumers` can be increased up to 100
  # per exporter if you have the CPU and network bandwidth to increase request concurrency.
  #
  # Docs:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter
  otlp_grpc/logs:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 8192
        max_size: 8192
        flush_timeout: 1s

  otlp_grpc/metrics:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 200
        max_size: 200
        flush_timeout: 1s

  otlp_grpc/traces:
    endpoint: telemetry.googleapis.com:443
    balancer_name: pick_first
    auth:
      authenticator: googleclientauth
    sending_queue:
      enabled: true
      sizer: bytes
      queue_size: 20971520 # 20 MiB queue bound
      block_on_overflow: true
      num_consumers: 25
      batch:
        sizer: items
        min_size: 8192
        max_size: 8192
        flush_timeout: 1s

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
      exporters:
      - otlp_grpc/logs
    metrics/otlp:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - transform/collision
      - transform/prometheus_unknown_types
      - metricstarttime
      - memory_limiter
      exporters:
      - otlp_grpc/metrics
    traces:
      receivers:
      - otlp
      processors:
      - resourcedetection
      - resource/gcp_project_id
      - memory_limiter
      exporters:
      - otlp_grpc/traces
  # 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

יצואנים

הגדרת ה-Collector משתמשת בפרטים הבאים:

מעבדים

ההגדרות של Collector כוללות את המעבדים הבאים:

  • ‫batch: מוגדר לאסוף בקשות טלמטריה באצווה לפי Google Cloud המספר המקסימלי של רשומות לכל בקשה, או לפי Google Cloud המרווח המינימלי של כל 5 שניות (המוקדם מביניהם).

  • ‫memory_limiter: מגדירה את מגבלת השימוש בזיכרון של ה-Collector כדי למנוע קריסות בגלל מחסור בזיכרון, על ידי השמטת נקודות על הגרף כשחורגים מהמגבלה.

  • ‫resourcedetection: זיהוי אוטומטי של תוויות משאבים ב- Google Cloud , כמו project_id.

מקלטים

ההגדרות של Collector כוללות רק את המקלט otlp. במאמר בחירת גישה למדידת ביצועים מוסבר איך להגדיר את האפליקציות כך שישלחו עקבות ומדדים של OTLP לנקודת הקצה של ה-Collector.

רכיבים זמינים

ה-Google-Built OpenTelemetry Collector מכיל את הרכיבים שרוב המשתמשים צריכים כדי להפעיל חוויה עשירה ב-Google Cloud Observability. רשימה מלאה של הרכיבים הזמינים מופיעה בקטע Components במאגר opentelemetry-operations-collector.

כדי לבקש שינויים או תוספות לרכיבים הזמינים, הגישו בקשה להוספת תכונה במאגר opentelemetry-operations-collector.

יצירת טלמטריה

כדי ליצור נתוני טלמטריה, יוצרים אפליקציית Cloud Run עם אוסף קובצי עזר. במסמך כתיבת מדדי OTLP באמצעות קובץ צדדי של OpenTelemetry מוסבר איך להשתמש ב-Google-Built OpenTelemetry Collector כקובץ צדדי. אתם יכולים להשתמש במדריך הזה כדי ליצור נתוני טלמטריה באמצעות ה-Collector שנוצר על ידי Google.

הצגת נתוני הטלמטריה

הכלי Google-Built OpenTelemetry Collector שולח מדדים, יומנים ומעקבים מהאפליקציות עם האינסטרומנטציה אל Google Cloud Observability. ה-Collector גם שולח מדדי תצפיתיות עצמית. בקטעים הבאים מוסבר איך לצפות בנתוני הטלמטריה האלה.

איך רואים את המדדים

הכלי Google-Built OpenTelemetry Collector אוסף מדדים של Prometheus, שאפשר לראות באמצעות Metrics Explorer. המדדים שנאספים תלויים במכשור של האפליקציה, אבל ה-Collector שנוצר על ידי Google כותב גם כמה מדדים עצמיים.

כדי לראות את המדדים שנאספו על ידי Google-Built OpenTelemetry Collector:
  1. במסוף Google Cloud , נכנסים לדף  Metrics explorer:

    כניסה לדף Metrics Explorer

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה שכותרת המשנה שלה היא Monitoring.

  2. בסרגל הכלים של מסוף Google Cloud , בוחרים את הפרויקט ב- Google Cloud . בהגדרות של App Hub, בוחרים את הפרויקט המארח של App Hub או את פרויקט הניהול של התיקייה לניהול אפליקציות.
  3. ברכיב Metric, מרחיבים את התפריט Select a metric, מזינים Prometheus Target בסרגל הסינון ומשתמשים בתפריטי המשנה כדי לבחור את סוג המשאב הספציפי ואת המדד:
    1. בתפריט Active resources בוחרים באפשרות Prometheus Target.
    2. כדי לבחור מדד, משתמשים בתפריטים Active metric categories ו-Active metrics. למדדים שנאספים על ידי Google-Built OpenTelemetry Collector יש את הקידומת prometheus.googleapis.com.
    3. לוחצים על אישור.
  4. כדי להוסיף מסננים שמסירים סדרות זמנים מתוצאות השאילתה, משתמשים ברכיב Filter.

  5. מגדירים את אופן התצוגה של הנתונים.

    כשהמדידות של מדד מסוים הן מצטברות, הכלי Metrics Explorer מנרמל באופן אוטומטי את הנתונים שנמדדו לפי תקופת ההתאמה, וכתוצאה מכך בתרשים מוצג שיעור. למידע נוסף, ראו סוגים, טיפוסים והמרות.

    כשמודדים ערכים של מספרים שלמים או ערכים כפולים, כמו במדדי counter, הכלי Metrics Explorer מסכם באופן אוטומטי את כל סדרות הזמנים. כדי לשנות את ההתנהגות הזו, מגדירים את התפריט הראשון של הרשומה Aggregation לערך None.

    מידע נוסף על הגדרת תרשים זמין במאמר איך בוחרים מדדים כשמשתמשים ב-Metrics Explorer.

הצגת המעקבים

כדי להציג את נתוני המעקב:

  1. במסוף Google Cloud , נכנסים לדף Trace explorer:

    כניסה לדף Trace Explorer

    אפשר גם למצוא את הדף הזה באמצעות שורת החיפוש.

  2. בסרגל הכלים של מסוף Google Cloud , בוחרים את הפרויקט ב- Google Cloud . בהגדרות של App Hub, בוחרים את הפרויקט המארח או את פרויקט הניהול של App Hub.
  3. בקטע של הטבלה בדף, בוחרים שורה.
  4. בתרשים הגאנט שבחלונית Trace details, בוחרים span.

    נפתח חלון שבו מוצג מידע על הבקשה שנבדקה. הפרטים האלה כוללים את השיטה, קוד הסטטוס, מספר הבייטים וסוכן המשתמש של מבצע הקריאה.

  5. כדי לראות את היומנים שמשויכים למעקב הזה, בוחרים בכרטיסייה Logs & Events.

    בכרטיסייה מוצגים יומנים נפרדים. כדי לראות את הפרטים של הרשומה ביומן, מרחיבים אותה. אפשר גם ללחוץ על View Logs כדי לראות את היומן באמצעות Logs Explorer.

מידע נוסף על השימוש ב-Cloud Trace Explorer זמין במאמר חיפוש ועיון במעקבים.

הצגת רישומי היומן

ב-Logs Explorer אתם יכולים לבדוק את היומנים, וגם לראות את המעקבים שמשויכים אליהם, אם יש כאלה.

  1. במסוף Google Cloud , נכנסים לדף Logs Explorer של :

    נכנסים ל-Logs Explorer.

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה עם כותרת המשנה Logging.

  2. מאתרים רשומת יומן מהאפליקציה עם כלי המדידה ומרחיבים את הרשומה כדי לראות את הפרטים.

  3. לוחצים על Traces ברשומת יומן עם הודעת מעקב, ובוחרים באפשרות View trace details.

    החלונית Trace details נפתחת ומוצג בה המעקב שנבחר.

למידע נוסף על השימוש ב-Logs Explorer, קראו את המאמר הצגת יומנים באמצעות Logs Explorer.

מעקב אחרי ה-Collector וניפוי באגים

הכלי Google-Built OpenTelemetry Collector מספק באופן אוטומטי מדדי תצפית עצמית, כדי לעזור לכם לעקוב אחרי הביצועים שלו ולוודא שצינור עיבוד הנתונים של הטמעת OTLP פועל באופן תקין.

כדי לעקוב אחרי ה-Collector, מתקינים את לוח הבקרה לדוגמה של ה-Collector. בלוח הבקרה הזה מוצגות תובנות במבט מהיר על כמה מדדים מה-Collector, כולל זמן פעולה תקינה, שימוש בזיכרון וקריאות ל-API אל Google Cloud Observability.

כדי להתקין את מרכז הבקרה:

  1. נכנסים לדף  Dashboards במסוף Google Cloud :

    עוברים אל מרכזי בקרה.

    אם אתם משתמשים בשורת החיפוש כדי למצוא את הדף הזה, בחרו בתוצאה שכותרת המשנה שלה היא Monitoring.

  2. לוחצים על תבניות של מרכזי בקרה.
  3. מחפשים את מרכז הבקרה OpenTelemetry Collector.
  4. אופציונלי: כדי לראות תצוגה מקדימה של מרכז הבקרה, בוחרים אותו.
  5. לוחצים על Add dashboard to your list וממלאים את הפרטים בתיבת הדו-שיח.

    בתיבת הדו-שיח אפשר לבחור את שם לוח הבקרה ולהוסיף לו תוויות.

מידע נוסף על התקנת מרכזי בקרה זמין במאמר התקנת תבנית של מרכז בקרה.