Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/3436-nodejs-autosintrumentation-buckets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support overriding the default histogram bucket list via the `spec.nodejs.histogramBuckets` field on the Instrumentation Custom Resource.

# One or more tracking issues related to the change
issues: [3436]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
9 changes: 9 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ type NodeJS struct {
// Resources describes the compute resource requirements.
// +optional
Resources corev1.ResourceRequirements `json:"resourceRequirements,omitempty"`

// HistogramBuckets defines the histogram buckets for metrics generated by the NodeJS SDK.
// +optional
HistogramBuckets []HistogramBucketsItem `json:"histogramBuckets,omitempty"`
}

type HistogramBucketsItem struct {
Name string `json:"instrumentName"`
Buckets []float64 `json:"buckets"`
}

// Python defines Python SDK and instrumentation configuration.
Expand Down
27 changes: 27 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion autoinstrumentation/nodejs/src/autoinstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/expor
import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { ExplicitBucketHistogramAggregation, PeriodicExportingMetricReader, View } from "@opentelemetry/sdk-metrics";
import { alibabaCloudEcsDetector } from '@opentelemetry/resource-detector-alibaba-cloud';
import { awsEc2Detector, awsEksDetector } from '@opentelemetry/resource-detector-aws';
import { containerDetector } from '@opentelemetry/resource-detector-container';
Expand Down Expand Up @@ -50,11 +50,39 @@ function getMetricReader() {
}
}

function getViews() {
const histogramBuckets = process.env.OTEL_OPERATOR_NODEJS_HISTOGRAM_BUCKETS;
if (histogramBuckets) {
try {
let views = [];
for (const viewCfg of JSON.parse(histogramBuckets)) {
views.push(
new View({
instrumentName: viewCfg["instrumentName"],
aggregation: new ExplicitBucketHistogramAggregation(
viewCfg["buckets"],
),
}),
);
}
return views;
} catch (error) {
diag.error(
"Failed to parse OTEL_OPERATOR_NODEJS_HISTOGRAM_BUCKETS:",
error,
);
}
return [];
}
return [];
}

const sdk = new NodeSDK({
autoDetectResources: true,
instrumentations: [getNodeAutoInstrumentations()],
traceExporter: getTraceExporter(),
metricReader: getMetricReader(),
views: getViews(),
resourceDetectors:
[
// Standard resource detectors.
Expand All @@ -75,3 +103,4 @@ const sdk = new NodeSDK({
});

sdk.start();

11 changes: 11 additions & 0 deletions config/crd/bases/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,17 @@ spec:
type: object
nodejs:
properties:
histogramBuckets:
type: array
items:
type: object
properties:
instrumentName:
type: string
buckets:
type: array
items:
type: number
env:
items:
properties:
Expand Down
18 changes: 17 additions & 1 deletion pkg/instrumentation/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package instrumentation

import (
corev1 "k8s.io/api/core/v1"

"encoding/json"
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)

Expand All @@ -26,9 +26,25 @@ const (
nodejsInitContainerName = initContainerName + "-nodejs"
nodejsVolumeName = volumeName + "-nodejs"
nodejsInstrMountPath = "/otel-auto-instrumentation-nodejs"
histogramBucketsEnvVarName = "OTEL_OPERATOR_NODEJS_HISTOGRAM_BUCKETS"
)

func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (corev1.Pod, error) {
if len(nodeJSSpec.HistogramBuckets) > 0 {
bucketsJSON, err := json.Marshal(nodeJSSpec.HistogramBuckets)
if err != nil {
// handle error, possibly log and skip injection for this resource
return pod, err
}

// Inject as an environment variable in the pod
bucketsJSONEnvVar := corev1.EnvVar{
Name: histogramBucketsEnvVarName,
Value: string(bucketsJSON),
}
nodeJSSpec.Env = append(nodeJSSpec.Env, bucketsJSONEnvVar)
}

volume := instrVolume(nodeJSSpec.VolumeClaimTemplate, nodejsVolumeName, nodeJSSpec.VolumeSizeLimit)

// caller checks if there is at least one container.
Expand Down