Skip to content

Commit

Permalink
feat: add support overriding nodejs buckets using .spec.nodejs.histog…
Browse files Browse the repository at this point in the history
…ramBuckets
  • Loading branch information
CCOLLOT committed Nov 14, 2024
1 parent 4e2c5fd commit f3962ff
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
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

0 comments on commit f3962ff

Please sign in to comment.