File: __init__.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,063 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from typing import Optional
import os
import socket
import sys
import pathlib

from opentelemetry import context as otel_context_api
from opentelemetry import trace as otel_trace_api
from opentelemetry.sdk import (
    resources as otel_resources,
    trace as otel_trace_sdk,
)
from opentelemetry.sdk.trace import export as otel_export
from opentelemetry.util import types as otel_types

from . import config
from . import clearcut_span_exporter
from . import detector

DEFAULT_BANNER = """
===============================================================================
To help improve the quality of this product, we collect usage data and
stacktraces from googlers. This includes a uuid generated weekly to identify
invocation from the same users as well as metrics as described in
go/chrome-infra-telemetry-readme. You may choose to opt out of this collection
at any time by setting the flag `enabled = False` under [trace] section in
{config_file}
or by executing from your depot_tools checkout:

vpython3 third_party/depot_tools/infra_lib/telemetry --disable

This notice will be displayed {run_count} more times.
===============================================================================
"""

# This does not include Googlers' physical machines/laptops
_GOOGLE_HOSTNAME_SUFFIX = ('.google.com', '.googler.com', '.googlers.com')

# The version keeps track of telemetry changes.
_TELEMETRY_VERSION = '3'


def get_host_name(fully_qualified: bool = False) -> str:
    """Return hostname of current machine, with domain if |fully_qualified|."""
    hostname = socket.gethostname()
    try:
        hostname = socket.gethostbyaddr(hostname)[0]
    except (socket.gaierror, socket.herror) as e:
        logging.warning(
            'please check your /etc/hosts file; resolving your hostname'
            ' (%s) failed: %s',
            hostname,
            e,
        )

    if fully_qualified:
        return hostname
    return hostname.partition('.')[0]


def is_google_host() -> bool:
    """Checks if the code is running on google host."""

    hostname = get_host_name(fully_qualified=True)
    return hostname.endswith(_GOOGLE_HOSTNAME_SUFFIX)


def initialize(service_name,
               notice=DEFAULT_BANNER,
               cfg_file=config.DEFAULT_CONFIG_FILE):
    if not is_google_host():
        return

    # TODO(326277821): Add support for other platforms
    if not sys.platform == 'linux':
        return

    cfg = config.Config(cfg_file)

    if not cfg.trace_config.has_enabled():
        if cfg.root_config.notice_countdown > -1:
            print(notice.format(run_count=cfg.root_config.notice_countdown,
                                config_file=cfg_file),
                  file=sys.stderr)
            cfg.root_config.update(
                notice_countdown=cfg.root_config.notice_countdown - 1)
        else:
            cfg.trace_config.update(enabled=True, reason='AUTO')

        cfg.flush()

    if not cfg.trace_config.enabled:
        return

    default_resource = otel_resources.Resource.create({
        otel_resources.SERVICE_NAME:
        service_name,
        'telemetry.version':
        _TELEMETRY_VERSION,
    })

    detected_resource = otel_resources.get_aggregated_resources([
        otel_resources.ProcessResourceDetector(),
        otel_resources.OTELResourceDetector(),
        detector.ProcessDetector(),
        detector.SystemDetector(),
    ])

    resource = detected_resource.merge(default_resource)
    trace_provider = otel_trace_sdk.TracerProvider(resource=resource)
    otel_trace_api.set_tracer_provider(trace_provider)
    trace_provider.add_span_processor(
        otel_export.BatchSpanProcessor(
            # Replace with ConsoleSpanExporter() to debug spans on the console
            clearcut_span_exporter.ClearcutSpanExporter()))


def get_tracer(name: str, version: Optional[str] = None):
    return otel_trace_api.get_tracer(name, version)